#include <cups/cups-private.h>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
static int create_job(http_t *http, const char *dest, const char *title,
const char *docname, const char *user,
int num_options, cups_option_t *options);
static int get_printer(http_t *http, const char *name, char *dest,
int destsize, cups_option_t **options,
int *accepting, int *shared, ipp_pstate_t *state);
static int print_file(http_t *http, int id, const char *filename,
const char *docname, const char *user,
const char *format, int last);
static int recv_print_job(const char *name, int num_defaults,
cups_option_t *defaults);
static int remove_jobs(const char *name, const char *agent,
const char *list);
static int send_state(const char *name, const char *list,
int longstatus);
static char *smart_gets(char *s, int len, FILE *fp);
int
main(int argc,
char *argv[])
{
int i;
int num_defaults;
cups_option_t *defaults;
char line[256],
command,
*dest,
*list,
*agent,
status;
socklen_t hostlen;
http_addr_t hostaddr;
char hostname[256],
hostip[256],
*hostfamily;
int hostlookups;
setbuf(stdout, NULL);
openlog("cups-lpd", LOG_PID, LOG_LPR);
num_defaults = 0;
defaults = NULL;
hostlookups = 1;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case 'h' :
if (argv[i][2])
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i < argc)
cupsSetServer(argv[i]);
else
syslog(LOG_WARNING, "Expected hostname string after -h option!");
}
break;
case 'o' :
if (argv[i][2])
num_defaults = cupsParseOptions(argv[i] + 2, num_defaults,
&defaults);
else
{
i ++;
if (i < argc)
num_defaults = cupsParseOptions(argv[i], num_defaults,
&defaults);
else
syslog(LOG_WARNING, "Expected option string after -o option!");
}
break;
case 'n' :
hostlookups = 0;
break;
default :
syslog(LOG_WARNING, "Unknown option \"%c\" ignored!", argv[i][1]);
break;
}
}
else
syslog(LOG_WARNING, "Unknown command-line option \"%s\" ignored!",
argv[i]);
hostlen = sizeof(hostaddr);
if (getpeername(0, (struct sockaddr *)&hostaddr, &hostlen))
{
syslog(LOG_WARNING, "Unable to get client address - %s", strerror(errno));
strcpy(hostname, "unknown");
}
else
{
httpAddrString(&hostaddr, hostip, sizeof(hostip));
if (hostlookups)
httpAddrLookup(&hostaddr, hostname, sizeof(hostname));
else
strlcpy(hostname, hostip, sizeof(hostname));
#ifdef AF_INET6
if (hostaddr.addr.sa_family == AF_INET6)
hostfamily = "IPv6";
else
#endif
hostfamily = "IPv4";
syslog(LOG_INFO, "Connection from %s (%s %s)", hostname, hostfamily,
hostip);
}
num_defaults = cupsAddOption("job-originating-host-name", hostname,
num_defaults, &defaults);
if (smart_gets(line, sizeof(line), stdin) == NULL)
{
syslog(LOG_ERR, "Unable to get command line from client!");
putchar(1);
return (1);
}
command = line[0];
dest = line + 1;
if (command == 0x02)
list = NULL;
else
{
for (list = dest + 1; *list && !isspace(*list & 255); list ++);
while (isspace(*list & 255))
*list++ = '\0';
}
switch (command)
{
default :
syslog(LOG_ERR, "Unknown LPD command 0x%02X!", command);
syslog(LOG_ERR, "Command line = %s", line + 1);
putchar(1);
status = 1;
break;
case 0x01 :
syslog(LOG_INFO, "Print waiting jobs (no-op)");
putchar(0);
status = 0;
break;
case 0x02 :
syslog(LOG_INFO, "Receive print job for %s", dest);
status = recv_print_job(dest, num_defaults, defaults);
break;
case 0x03 :
syslog(LOG_INFO, "Send queue state (short) for %s %s", dest, list);
status = send_state(dest, list, 0);
break;
case 0x04 :
syslog(LOG_INFO, "Send queue state (long) for %s %s", dest, list);
status = send_state(dest, list, 1);
break;
case 0x05 :
if (list)
{
agent = list;
for (; *list && !isspace(*list & 255); list ++);
while (isspace(*list & 255))
*list++ = '\0';
syslog(LOG_INFO, "Remove jobs %s on %s by %s", list, dest, agent);
status = remove_jobs(dest, agent, list);
}
else
status = 1;
putchar(status);
break;
}
syslog(LOG_INFO, "Closing connection");
closelog();
return (status);
}
static int
create_job(http_t *http,
const char *dest,
const char *title,
const char *docname,
const char *user,
int num_options,
cups_option_t *options)
{
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
int id;
request = ippNewRequest(IPP_CREATE_JOB);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", dest);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
if (title[0])
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
NULL, title);
if (docname[0])
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
NULL, docname);
cupsEncodeOptions(request, num_options, options);
snprintf(uri, sizeof(uri), "/printers/%s", dest);
response = cupsDoRequest(http, request, uri);
if (!response || cupsLastError() > IPP_OK_CONFLICT)
{
syslog(LOG_ERR, "Unable to create job - %s", cupsLastErrorString());
ippDelete(response);
return (-1);
}
if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL)
{
id = -1;
syslog(LOG_ERR, "No job-id attribute found in response from server!");
}
else
{
id = attr->values[0].integer;
syslog(LOG_INFO, "Print file - job ID = %d", id);
}
ippDelete(response);
return (id);
}
static int
get_printer(http_t *http,
const char *name,
char *dest,
int destsize,
cups_option_t **options,
int *accepting,
int *shared,
ipp_pstate_t *state)
{
int num_options;
cups_file_t *fp;
char line[1024],
*value,
*optptr;
int linenum;
const char *cups_serverroot;
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
static const char * const requested[] =
{
"printer-info",
"printer-is-accepting-jobs",
"printer-is-shared",
"printer-name",
"printer-state"
};
if (accepting)
*accepting = 0;
if (shared)
*shared = 0;
if (state)
*state = IPP_PRINTER_STOPPED;
if (options)
*options = NULL;
strlcpy(dest, name, destsize);
if ((value = strchr(dest, '/')) != NULL)
*value = '\0';
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", dest);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
(int)(sizeof(requested) / sizeof(requested[0])),
NULL, requested);
response = cupsDoRequest(http, request, "/");
if (!response || cupsLastError() > IPP_OK_CONFLICT)
{
ipp_attribute_t *accepting_attr,
*info_attr,
*name_attr,
*shared_attr,
*state_attr;
ippDelete(response);
request = ippNewRequest(CUPS_GET_PRINTERS);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
(int)(sizeof(requested) / sizeof(requested[0])),
NULL, requested);
response = cupsDoRequest(http, request, "/");
if (!response || cupsLastError() > IPP_OK_CONFLICT)
{
syslog(LOG_ERR, "Unable to get list of printers - %s",
cupsLastErrorString());
ippDelete(response);
return (-1);
}
*dest = '\0';
attr = response->attrs;
while (attr)
{
while (attr && attr->group_tag != IPP_TAG_PRINTER)
attr = attr->next;
if (!attr)
break;
accepting_attr = NULL;
info_attr = NULL;
name_attr = NULL;
shared_attr = NULL;
state_attr = NULL;
while (attr && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
attr->value_tag == IPP_TAG_BOOLEAN)
accepting_attr = attr;
else if (!strcmp(attr->name, "printer-info") &&
attr->value_tag == IPP_TAG_TEXT)
info_attr = attr;
else if (!strcmp(attr->name, "printer-name") &&
attr->value_tag == IPP_TAG_NAME)
name_attr = attr;
else if (!strcmp(attr->name, "printer-is-shared") &&
attr->value_tag == IPP_TAG_BOOLEAN)
shared_attr = attr;
else if (!strcmp(attr->name, "printer-state") &&
attr->value_tag == IPP_TAG_ENUM)
state_attr = attr;
attr = attr->next;
}
if (info_attr && name_attr &&
!_cups_strcasecmp(name, info_attr->values[0].string.text))
{
strlcpy(dest, name_attr->values[0].string.text, destsize);
if (accepting && accepting_attr)
*accepting = accepting_attr->values[0].boolean;
if (shared && shared_attr)
*shared = shared_attr->values[0].boolean;
if (state && state_attr)
*state = (ipp_pstate_t)state_attr->values[0].integer;
break;
}
}
ippDelete(response);
if (!*dest)
{
syslog(LOG_ERR, "Unable to find \"%s\" in list of printers!", name);
return (-1);
}
name = dest;
}
else
{
if (accepting)
{
if ((attr = ippFindAttribute(response, "printer-is-accepting-jobs",
IPP_TAG_BOOLEAN)) == NULL)
syslog(LOG_ERR, "No printer-is-accepting-jobs attribute found in "
"response from server!");
else
*accepting = attr->values[0].boolean;
}
if (shared)
{
if ((attr = ippFindAttribute(response, "printer-is-shared",
IPP_TAG_BOOLEAN)) == NULL)
{
syslog(LOG_ERR, "No printer-is-shared attribute found in "
"response from server!");
*shared = 1;
}
else
*shared = attr->values[0].boolean;
}
if (state)
{
if ((attr = ippFindAttribute(response, "printer-state",
IPP_TAG_ENUM)) == NULL)
syslog(LOG_ERR, "No printer-state attribute found in "
"response from server!");
else
*state = (ipp_pstate_t)attr->values[0].integer;
}
ippDelete(response);
}
num_options = 0;
if (options && shared && accepting)
{
if ((cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
cups_serverroot = CUPS_SERVERROOT;
snprintf(line, sizeof(line), "%s/lpoptions", cups_serverroot);
if ((fp = cupsFileOpen(line, "r")) != NULL)
{
linenum = 0;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
{
if ((_cups_strcasecmp(line, "Dest") && _cups_strcasecmp(line, "Default")) || !value)
continue;
for (optptr = value; *optptr && !isspace(*optptr & 255); optptr ++);
while (*optptr == ' ')
*optptr++ = '\0';
if (!_cups_strcasecmp(value, name))
{
num_options = cupsParseOptions(optptr, num_options, options);
break;
}
}
cupsFileClose(fp);
}
}
else if (options)
*options = NULL;
return (num_options);
}
static int
print_file(http_t *http,
int id,
const char *filename,
const char *docname,
const char *user,
const char *format,
int last)
{
ipp_t *request;
char uri[HTTP_MAX_URI];
request = ippNewRequest(IPP_SEND_DOCUMENT);
snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
if (docname)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"document-name", NULL, docname);
if (format)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, format);
if (last)
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
snprintf(uri, sizeof(uri), "/jobs/%d", id);
ippDelete(cupsDoFileRequest(http, request, uri, filename));
if (cupsLastError() > IPP_OK_CONFLICT)
{
syslog(LOG_ERR, "Unable to send document - %s", cupsLastErrorString());
return (-1);
}
return (0);
}
static int
recv_print_job(
const char *queue,
int num_defaults,
cups_option_t *defaults)
{
http_t *http;
int i;
int status;
int fd;
FILE *fp;
char filename[1024];
int bytes;
char line[256],
command,
*count,
*name;
const char *job_sheets;
int num_data;
char control[1024],
data[100][256],
temp[100][1024];
char user[1024],
title[1024],
docname[1024],
dest[256];
int accepting,
shared,
num_options;
cups_option_t *options;
int id;
int docnumber,
doccount;
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (!http)
{
syslog(LOG_ERR, "Unable to connect to server: %s", strerror(errno));
putchar(1);
return (1);
}
num_options = get_printer(http, queue, dest, sizeof(dest), &options,
&accepting, &shared, NULL);
if (num_options < 0 || !accepting || !shared)
{
if (dest[0])
syslog(LOG_INFO, "Rejecting job because \"%s\" is not %s", dest,
!accepting ? "accepting jobs" : "shared");
else
syslog(LOG_ERR, "Unable to get printer information for \"%s\"", queue);
httpClose(http);
putchar(1);
return (1);
}
putchar(0);
status = 0;
num_data = 0;
fd = -1;
control[0] = '\0';
while (smart_gets(line, sizeof(line), stdin) != NULL)
{
if (strlen(line) < 2)
{
status = 1;
break;
}
command = line[0];
count = line + 1;
for (name = count + 1; *name && !isspace(*name & 255); name ++);
while (isspace(*name & 255))
*name++ = '\0';
switch (command)
{
default :
case 0x01 :
status = 1;
break;
case 0x02 :
if (strlen(name) < 2)
{
syslog(LOG_ERR, "Bad control file name \"%s\"", name);
putchar(1);
status = 1;
break;
}
if (control[0])
{
if ((fd = open(control, O_WRONLY)) < 0)
{
syslog(LOG_ERR,
"Unable to append to temporary control file \"%s\" - %s",
control, strerror(errno));
putchar(1);
status = 1;
break;
}
lseek(fd, 0, SEEK_END);
}
else
{
if ((fd = cupsTempFd(control, sizeof(control))) < 0)
{
syslog(LOG_ERR, "Unable to open temporary control file \"%s\" - %s",
control, strerror(errno));
putchar(1);
status = 1;
break;
}
strcpy(filename, control);
}
break;
case 0x03 :
if (strlen(name) < 2)
{
syslog(LOG_ERR, "Bad data file name \"%s\"", name);
putchar(1);
status = 1;
break;
}
if (num_data >= (int)(sizeof(data) / sizeof(data[0])))
{
syslog(LOG_ERR, "Too many data files (%d)", num_data);
putchar(1);
status = 1;
break;
}
strlcpy(data[num_data], name, sizeof(data[0]));
if ((fd = cupsTempFd(temp[num_data], sizeof(temp[0]))) < 0)
{
syslog(LOG_ERR, "Unable to open temporary data file \"%s\" - %s",
temp[num_data], strerror(errno));
putchar(1);
status = 1;
break;
}
strcpy(filename, temp[num_data]);
num_data ++;
break;
}
putchar(status);
if (status)
break;
for (i = atoi(count); i > 0; i -= bytes)
{
if (i > sizeof(line))
bytes = sizeof(line);
else
bytes = i;
if ((bytes = fread(line, 1, bytes, stdin)) > 0)
bytes = write(fd, line, bytes);
if (bytes < 1)
{
syslog(LOG_ERR, "Error while reading file - %s",
strerror(errno));
status = 1;
break;
}
}
if (!status)
{
if (fread(line, 1, 1, stdin) < 1)
{
status = 1;
syslog(LOG_ERR, "Error while reading trailing nul - %s",
strerror(errno));
}
else if (line[0])
{
status = 1;
syslog(LOG_ERR, "Trailing character after file is not nul (%02X)!",
line[0]);
}
}
close(fd);
putchar(status);
if (status)
break;
}
if (!status)
{
if ((fp = fopen(control, "rb")) == NULL)
status = 1;
else
{
for (i = 0; i < num_defaults; i ++)
num_options = cupsAddOption(defaults[i].name,
defaults[i].value,
num_options, &options);
title[0] = '\0';
user[0] = '\0';
docname[0] = '\0';
doccount = 0;
while (smart_gets(line, sizeof(line), fp) != NULL)
{
switch (line[0])
{
case 'J' :
strlcpy(title, line + 1, sizeof(title));
break;
case 'N' :
strlcpy(docname, line + 1, sizeof(docname));
break;
case 'P' :
strlcpy(user, line + 1, sizeof(user));
break;
case 'L' :
if (cupsGetOption("job-sheets", num_defaults, defaults) == NULL &&
((job_sheets = cupsGetOption("job-sheets", num_options,
options)) == NULL ||
!strcmp(job_sheets, "none,none")))
{
num_options = cupsAddOption("job-sheets", "standard",
num_options, &options);
}
break;
case 'c' :
case 'd' :
case 'f' :
case 'g' :
case 'l' :
case 'n' :
case 'o' :
case 'p' :
case 'r' :
case 't' :
case 'v' :
doccount ++;
if (line[0] == 'l' &&
!cupsGetOption("document-format", num_options, options))
num_options = cupsAddOption("raw", "", num_options, &options);
if (line[0] == 'p')
num_options = cupsAddOption("prettyprint", "", num_options,
&options);
break;
}
if (status)
break;
}
if (!user[0])
{
syslog(LOG_WARNING, "No username specified by client! "
"Using \"anonymous\"...");
strcpy(user, "anonymous");
}
if ((id = create_job(http, dest, title, docname, user, num_options,
options)) < 0)
status = 1;
else
{
rewind(fp);
docname[0] = '\0';
docnumber = 0;
while (smart_gets(line, sizeof(line), fp) != NULL)
{
switch (line[0])
{
case 'N' :
strlcpy(docname, line + 1, sizeof(docname));
break;
case 'c' :
case 'd' :
case 'f' :
case 'g' :
case 'l' :
case 'n' :
case 'o' :
case 'p' :
case 'r' :
case 't' :
case 'v' :
for (i = 0; i < num_data; i ++)
if (!strcmp(data[i], line + 1))
break;
if (i >= num_data)
{
status = 1;
break;
}
docnumber ++;
if (print_file(http, id, temp[i], docname, user,
cupsGetOption("document-format", num_options,
options),
docnumber == doccount))
status = 1;
else
status = 0;
break;
}
if (status)
break;
}
}
fclose(fp);
}
}
cupsFreeOptions(num_options, options);
httpClose(http);
unlink(control);
for (i = 0; i < num_data; i ++)
unlink(temp[i]);
return (status);
}
static int
remove_jobs(const char *dest,
const char *agent,
const char *list)
{
int id;
http_t *http;
ipp_t *request;
char uri[HTTP_MAX_URI];
(void)dest;
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == NULL)
{
syslog(LOG_ERR, "Unable to connect to server %s: %s", cupsServer(),
strerror(errno));
return (1);
}
while ((id = atoi(list)) > 0)
{
while (isdigit(*list & 255))
list ++;
while (isspace(*list & 255))
list ++;
request = ippNewRequest(IPP_CANCEL_JOB);
sprintf(uri, "ipp://localhost/jobs/%d", id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, agent);
ippDelete(cupsDoRequest(http, request, "/jobs"));
if (cupsLastError() > IPP_OK_CONFLICT)
{
syslog(LOG_WARNING, "Cancel of job ID %d failed: %s\n", id,
cupsLastErrorString());
httpClose(http);
return (1);
}
else
syslog(LOG_INFO, "Job ID %d canceled", id);
}
httpClose(http);
return (0);
}
static int
send_state(const char *queue,
const char *list,
int longstatus)
{
int id;
http_t *http;
ipp_t *request,
*response;
ipp_attribute_t *attr;
ipp_pstate_t state;
const char *jobdest,
*jobuser,
*jobname;
ipp_jstate_t jobstate;
int jobid,
jobsize,
jobcount,
jobcopies,
rank;
char rankstr[255];
char namestr[1024];
char uri[HTTP_MAX_URI];
char dest[256];
static const char * const ranks[10] =
{
"th",
"st",
"nd",
"rd",
"th",
"th",
"th",
"th",
"th",
"th"
};
static const char * const requested[] =
{
"job-id",
"job-k-octets",
"job-state",
"job-printer-uri",
"job-originating-user-name",
"job-name",
"copies"
};
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == NULL)
{
syslog(LOG_ERR, "Unable to connect to server %s: %s", cupsServer(),
strerror(errno));
printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno));
return (1);
}
if (get_printer(http, queue, dest, sizeof(dest), NULL, NULL, NULL, &state))
{
syslog(LOG_ERR, "Unable to get printer %s: %s", queue,
cupsLastErrorString());
printf("Unable to get printer %s: %s", queue, cupsLastErrorString());
return (1);
}
switch (state)
{
case IPP_PRINTER_IDLE :
printf("%s is ready\n", dest);
break;
case IPP_PRINTER_PROCESSING :
printf("%s is ready and printing\n", dest);
break;
case IPP_PRINTER_STOPPED :
printf("%s is not ready\n", dest);
break;
}
id = atoi(list);
request = ippNewRequest(id ? IPP_GET_JOB_ATTRIBUTES : IPP_GET_JOBS);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", dest);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
if (id)
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id);
else
{
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, list);
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
}
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(requested) / sizeof(requested[0]),
NULL, requested);
jobcount = 0;
response = cupsDoRequest(http, request, "/");
if (cupsLastError() > IPP_OK_CONFLICT)
{
printf("get-jobs failed: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
for (attr = response->attrs, rank = 1; attr; attr = attr->next)
{
while (attr && (attr->group_tag != IPP_TAG_JOB || !attr->name))
attr = attr->next;
if (!attr)
break;
jobid = 0;
jobsize = 0;
jobstate = IPP_JOB_PENDING;
jobname = "untitled";
jobuser = NULL;
jobdest = NULL;
jobcopies = 1;
while (attr && attr->group_tag == IPP_TAG_JOB)
{
if (!strcmp(attr->name, "job-id") &&
attr->value_tag == IPP_TAG_INTEGER)
jobid = attr->values[0].integer;
if (!strcmp(attr->name, "job-k-octets") &&
attr->value_tag == IPP_TAG_INTEGER)
jobsize = attr->values[0].integer;
if (!strcmp(attr->name, "job-state") &&
attr->value_tag == IPP_TAG_ENUM)
jobstate = (ipp_jstate_t)attr->values[0].integer;
if (!strcmp(attr->name, "job-printer-uri") &&
attr->value_tag == IPP_TAG_URI)
if ((jobdest = strrchr(attr->values[0].string.text, '/')) != NULL)
jobdest ++;
if (!strcmp(attr->name, "job-originating-user-name") &&
attr->value_tag == IPP_TAG_NAME)
jobuser = attr->values[0].string.text;
if (!strcmp(attr->name, "job-name") &&
attr->value_tag == IPP_TAG_NAME)
jobname = attr->values[0].string.text;
if (!strcmp(attr->name, "copies") &&
attr->value_tag == IPP_TAG_INTEGER)
jobcopies = attr->values[0].integer;
attr = attr->next;
}
if (!jobdest || !jobid)
{
if (!attr)
break;
else
continue;
}
if (!longstatus && jobcount == 0)
puts("Rank Owner Job File(s) Total Size");
jobcount ++;
if (jobstate == IPP_JOB_PROCESSING)
strcpy(rankstr, "active");
else
{
snprintf(rankstr, sizeof(rankstr), "%d%s", rank, ranks[rank % 10]);
rank ++;
}
if (longstatus)
{
puts("");
if (jobcopies > 1)
snprintf(namestr, sizeof(namestr), "%d copies of %s", jobcopies,
jobname);
else
strlcpy(namestr, jobname, sizeof(namestr));
printf("%s: %-33.33s [job %d localhost]\n", jobuser, rankstr, jobid);
printf(" %-39.39s %.0f bytes\n", namestr, 1024.0 * jobsize);
}
else
printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr, jobuser,
jobid, jobname, 1024.0 * jobsize);
if (!attr)
break;
}
ippDelete(response);
if (jobcount == 0)
puts("no entries");
httpClose(http);
return (0);
}
static char *
smart_gets(char *s,
int len,
FILE *fp)
{
char *ptr,
*end;
int ch;
ptr = s;
end = s + len - 1;
while ((ch = getc(fp)) != EOF)
{
if (ch == '\n')
break;
else if (ch == '\r')
{
ch = getc(fp);
if (ch != '\n')
ungetc(ch, fp);
break;
}
else if (ptr < end)
*ptr++ = ch;
}
*ptr = '\0';
if (ch == EOF && ptr == s)
return (NULL);
else
return (s);
}