#include "lp.cdefs.h"
#include <sys/param.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include "lp.h"
#include "lp.local.h"
#include "pathnames.h"
int requ[MAXREQUESTS];
int requests;
char *user[MAXUSERS];
int users;
uid_t uid, euid;
static int ckqueue(const struct printer *_pp);
static void usage(void);
int main(int _argc, char **_argv);
int
main(int argc, char **argv)
{
int ch, aflag, lflag;
const char *printer;
struct printer myprinter, *pp = &myprinter;
printer = NULL;
euid = geteuid();
uid = getuid();
PRIV_END
progname = *argv;
if (gethostname(local_host, sizeof(local_host)))
err(1, "gethostname");
openlog("lpd", 0, LOG_LPR);
aflag = lflag = 0;
while ((ch = getopt(argc, argv, "alP:")) != -1)
switch((char)ch) {
case 'a':
++aflag;
break;
case 'l':
++lflag;
break;
case 'P':
printer = optarg;
break;
case '?':
default:
usage();
}
if (!aflag && printer == NULL && (printer = getenv("PRINTER")) == NULL)
printer = DEFLP;
for (argc -= optind, argv += optind; argc; --argc, ++argv)
if (isdigit(argv[0][0])) {
if (requests >= MAXREQUESTS)
fatal(0, "too many requests");
requ[requests++] = atoi(*argv);
}
else {
if (users >= MAXUSERS)
fatal(0, "too many users");
user[users++] = *argv;
}
if (aflag) {
int more, status;
more = firstprinter(pp, &status);
if (status)
goto looperr;
while (more) {
if (ckqueue(pp) > 0) {
printf("%s:\n", pp->printer);
displayq(pp, lflag);
printf("\n");
}
do {
more = nextprinter(pp, &status);
looperr:
switch (status) {
case PCAPERR_TCOPEN:
printf("warning: %s: unresolved "
"tc= reference(s) ",
pp->printer);
case PCAPERR_SUCCESS:
break;
default:
fatal(pp, "%s", pcaperr(status));
}
} while (more && status);
}
} else {
int status;
init_printer(pp);
status = getprintcap(printer, pp);
if (status < 0)
fatal(pp, "%s", pcaperr(status));
displayq(pp, lflag);
}
exit(0);
}
static int
ckqueue(const struct printer *pp)
{
register struct dirent *d;
DIR *dirp;
char *spooldir;
spooldir = pp->spool_dir;
if ((dirp = opendir(spooldir)) == NULL)
return (-1);
while ((d = readdir(dirp)) != NULL) {
if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
continue;
closedir(dirp);
return (1);
}
closedir(dirp);
return (0);
}
static void
usage(void)
{
fprintf(stderr,
"usage: lpq [-a] [-l] [-Pprinter] [user ...] [job ...]\n");
exit(1);
}