2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
35 * @(#)cmds.c 8.2 (Berkeley) 4/28/95
36 * $FreeBSD: src/usr.sbin/lpr/lpc/cmds.c,v 1.14.2.16 2002/07/25 23:29:39 gad Exp $
40 * lpc -- line printer control program -- commands:
43 #include <sys/param.h>
61 #include "pathnames.h"
64 * Return values from kill_qtask().
66 #define KQT_LFERROR -2
67 #define KQT_KILLFAIL -1
68 #define KQT_NODAEMON 0
71 static char *args2line(int argc, char **argv);
72 static int doarg(char *_job);
73 static int doselect(const struct dirent *_d);
74 static int kill_qtask(const char *lf);
75 static int sortq(const struct dirent **_a, const struct dirent **_b);
76 static int touch(struct jobqueue *_jq);
77 static void unlinkf(char *_name);
78 static void upstat(struct printer *_pp, const char *_msg, int _notify);
79 static void wrapup_clean(int _laststatus);
82 * generic framework for commands which operate on all or a specified
85 enum qsel_val { /* how a given ptr was selected */
86 QSEL_UNKNOWN = -1, /* ... not selected yet */
87 QSEL_BYNAME = 0, /* ... user specifed it by name */
88 QSEL_ALL = 1 /* ... user wants "all" printers */
89 /* (with more to come) */
92 static enum qsel_val generic_qselect; /* indicates how ptr was selected */
93 static int generic_initerr; /* result of initrtn processing */
94 static char *generic_cmdname;
95 static char *generic_msg; /* if a -msg was specified */
96 static char *generic_nullarg;
97 static void (*generic_wrapup)(int _last_status); /* perform rtn wrap-up */
100 generic(void (*specificrtn)(struct printer *_pp), int cmdopts,
101 void (*initrtn)(int _argc, char **_argv), int argc, char **argv)
103 int cmdstatus, more, targc;
104 struct printer myprinter, *pp;
105 char **margv, **targv;
109 * Usage needs a special case for 'down': The user must
110 * either include `-msg', or only the first parameter
111 * that they give will be processed as a printer name.
113 printf("usage: %s {all | printer ...}", argv[0]);
114 if (strcmp(argv[0], "down") == 0) {
115 printf(" -msg [<text> ...]\n");
116 printf(" or: down {all | printer} [<text> ...]");
117 } else if (cmdopts & LPC_MSGOPT)
118 printf(" [-msg <text> ...]");
123 /* The first argument is the command name. */
124 generic_cmdname = *argv++;
128 * The initialization routine for a command might set a generic
129 * "wrapup" routine, which should be called after processing all
130 * the printers in the command. This might print summary info.
132 * Note that the initialization routine may also parse (and
133 * nullify) some of the parameters given on the command, leaving
134 * only the parameters which have to do with printer names.
137 generic_wrapup = NULL;
138 generic_qselect = QSEL_UNKNOWN;
140 /* this just needs to be a distinct value of type 'char *' */
141 if (generic_nullarg == NULL)
142 generic_nullarg = strdup("");
145 * Some commands accept a -msg argument, which indicates that
146 * all remaining arguments should be combined into a string.
149 if (cmdopts & LPC_MSGOPT) {
152 for (; targc > 0; targc--, targv++) {
153 if (strcmp(*targv, "-msg") == 0) {
155 generic_msg = args2line(targc - 1, targv + 1);
161 /* call initialization routine, if there is one for this cmd */
162 if (initrtn != NULL) {
164 (*initrtn)(argc, argv);
168 * The initrtn may have null'ed out some of the parameters.
169 * Compact the parameter list to remove those nulls, and
170 * correct the arg-count.
176 for (; targc > 0; targc--, targv++) {
177 if (*targv != generic_nullarg) {
186 if (argc == 1 && strcmp(*argv, "all") == 0) {
187 generic_qselect = QSEL_ALL;
188 more = firstprinter(pp, &cmdstatus);
194 more = nextprinter(pp, &cmdstatus);
198 printf("warning: %s: unresolved "
201 case PCAPERR_SUCCESS:
204 fatal(pp, "%s", pcaperr(cmdstatus));
206 } while (more && cmdstatus);
211 generic_qselect = QSEL_BYNAME; /* specifically-named ptrs */
212 for (; argc > 0; argc--, argv++) {
214 cmdstatus = getprintcap(*argv, pp);
217 fatal(pp, "%s", pcaperr(cmdstatus));
218 case PCAPERR_NOTFOUND:
219 printf("unknown printer %s\n", *argv);
222 printf("warning: %s: unresolved tc= reference(s)\n",
225 case PCAPERR_SUCCESS:
232 if (generic_wrapup) {
233 (*generic_wrapup)(cmdstatus);
241 * Convert an argv-array of character strings into a single string.
244 args2line(int argc, char **argv)
254 cend = buf + sizeof(buf) - 1; /* save room for '\0' */
255 while (--argc >= 0) {
257 while ((cp1 < cend) && (*cp1++ = *cp2++))
267 * Kill the current daemon, to stop printing of the active job.
270 kill_qtask(const char *lf)
274 int errsav, killres, lockres, res;
283 * If there is no lock file, then there is no daemon to
284 * kill. Any other error return means there is some
285 * kind of problem with the lock file.
287 if (errsav != ENOENT)
292 /* If the lock file is empty, then there is no daemon to kill */
293 if (getline(fp) == 0)
297 * If the file can be locked without blocking, then there
298 * no daemon to kill, or we should not try to kill it.
300 * XXX - not sure I understand the reasoning behind this...
302 lockres = flock(fileno(fp), LOCK_SH|LOCK_NB);
310 * If we got a negative pid, then the contents of the
311 * lock file is not valid.
318 killres = kill(pid, SIGTERM);
323 printf("\tdaemon (pid %d) killed\n", pid);
324 } else if (errno == ESRCH) {
328 printf("\tWarning: daemon (pid %d) not killed:\n", pid);
329 printf("\t %s\n", strerror(errsav));
335 printf("\tcannot open lock file: %s\n",
339 printf("\tno daemon to abort\n");
343 /* These two already printed messages to the user. */
346 printf("\t<internal error in kill_qtask>\n");
354 * Write a message into the status file.
357 upstat(struct printer *pp, const char *msg, int notifyuser)
360 char statfile[MAXPATHLEN];
362 status_file_name(pp, statfile, sizeof statfile);
365 fd = open(statfile, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
368 printf("\tcannot create status file: %s\n", strerror(errno));
375 write(fd, msg, strlen(msg));
378 if ((msg == NULL) || (strcmp(msg, "\n") == 0))
379 printf("\tstatus message is now set to nothing.\n");
381 printf("\tstatus message is now: %s", msg);
386 * kill an existing daemon and disable printing.
389 abort_q(struct printer *pp)
394 lock_file_name(pp, lf, sizeof lf);
395 printf("%s:\n", pp->printer);
398 * Turn on the owner execute bit of the lock file to disable printing.
400 setres = set_qstate(SQS_STOPP, lf);
403 * If set_qstate found that there already was a lock file, then
404 * call a routine which will read that lock file and kill the
405 * lpd-process which is listed in that lock file. If the lock
406 * file did not exist, then either there is no daemon running
407 * for this queue, or there is one running but *it* could not
408 * write a lock file (which means we can not determine the
409 * process id of that lpd-process).
414 /* Kill the process */
415 killres = kill_qtask(lf);
419 printf("\tno daemon to abort\n");
422 printf("\tassuming no daemon to abort\n");
425 printf("\t<unexpected result (%d) from set_qstate>\n",
431 upstat(pp, "printing disabled\n", 0);
435 * "global" variables for all the routines related to 'clean' and 'tclean'
437 static time_t cln_now; /* current time */
438 static double cln_minage; /* minimum age before file is removed */
439 static long cln_sizecnt; /* amount of space freed up */
440 static int cln_debug; /* print extra debugging msgs */
441 static int cln_filecnt; /* number of files destroyed */
442 static int cln_foundcore; /* found a core file! */
443 static int cln_queuecnt; /* number of queues checked */
444 static int cln_testonly; /* remove-files vs just-print-info */
447 doselect(const struct dirent *d)
449 int c = d->d_name[0];
451 if ((c == 'c' || c == 'd' || c == 'r' || c == 't') &&
455 if (!strcmp(d->d_name, "core"))
459 if (!strncmp(d->d_name, "errs.", 5))
466 * Comparison routine that clean_q() uses for scandir.
468 * The purpose of this sort is to have all `df' files end up immediately
469 * after the matching `cf' file. For files matching `cf', `df', `rf', or
470 * `tf', it sorts by job number and machine, then by `cf', `df', `rf', or
471 * `tf', and then by the sequence letter (which is A-Z, or a-z). This
472 * routine may also see filenames which do not start with `cf', `df', `rf',
473 * or `tf' (such as `errs.*'), and those are simply sorted by the full
477 * This assumes that all control files start with `cfA*', and it turns
478 * out there are a few implementations of lpr which will create `cfB*'
479 * filenames (they will have datafile names which start with `dfB*').
482 sortq(const struct dirent **a, const struct dirent **b)
484 const int a_lt_b = -1, a_gt_b = 1, cat_other = 10;
485 const char *fname_a, *fname_b, *jnum_a, *jnum_b;
486 int cat_a, cat_b, ch, res, seq_a, seq_b;
488 fname_a = (*a)->d_name;
489 fname_b = (*b)->d_name;
492 * First separate filenames into cagatories. Catagories are
493 * legitimate `cf', `df', `rf' & `tf' filenames, and "other" - in
494 * that order. It is critical that the mapping be exactly the
495 * same for 'a' vs 'b', so define a macro for the job.
497 * [aside: the standard `cf' file has the jobnumber start in
498 * position 4, but some implementations have that as an extra
499 * file-sequence letter, and start the job number in position 5.]
501 #define MAP_TO_CAT(fname_X,cat_X,jnum_X,seq_X) do { \
503 ch = *(fname_X + 2); \
504 jnum_X = fname_X + 3; \
506 if ((*(fname_X + 1) == 'f') && (isalpha(ch))) { \
508 if (*fname_X == 'c') \
510 else if (*fname_X == 'd') \
512 else if (*fname_X == 'r') \
514 else if (*fname_X == 't') \
516 if (cat_X != cat_other) { \
518 if (!isdigit(ch)) { \
522 seq_X = (seq_X << 8) + ch; \
531 MAP_TO_CAT(fname_a, cat_a, jnum_a, seq_a);
532 MAP_TO_CAT(fname_b, cat_b, jnum_b, seq_b);
536 /* First handle all cases which have "other" files */
537 if ((cat_a >= cat_other) || (cat_b >= cat_other)) {
538 /* for two "other" files, just compare the full name */
540 res = strcmp(fname_a, fname_b);
541 else if (cat_a < cat_b)
549 * At this point, we know both files are legitimate `cf', `df', `rf',
550 * or `tf' files. Compare them by job-number and machine name.
552 res = strcmp(jnum_a, jnum_b);
557 * We have two files which belong to the same job. Sort based
558 * on the catagory of file (`c' before `d', etc).
563 } else if (cat_a > cat_b) {
569 * Two files in the same catagory for a single job. Sort based
570 * on the sequence letter(s). (usually `A' thru `Z', etc).
575 } else if (seq_a > seq_b) {
581 * Given that the filenames in a directory are unique, this SHOULD
582 * never happen (unless there are logic errors in this routine).
583 * But if it does happen, we must return "is equal" or the caller
584 * might see inconsistent results in the sorting order, and that
585 * can trigger other problems.
587 printf("\t*** Error in sortq: %s == %s !\n", fname_a, fname_b);
588 printf("\t*** cat %d == %d ; seq = %d %d\n", cat_a, cat_b,
597 * Remove all spool files and temporaries from the spooling area.
599 * Remove incomplete jobs from spooling area.
603 clean_gi(int argc, char **argv)
606 /* init some fields before 'clean' is called for each queue */
608 cln_now = time(NULL);
609 cln_minage = 3600.0; /* only delete files >1h old */
614 generic_wrapup = &wrapup_clean;
616 /* see if there are any options specified before the ptr list */
617 for (; argc > 0; argc--, argv++) {
620 if (strcmp(*argv, "-d") == 0) {
621 /* just an example of an option... */
623 *argv = generic_nullarg; /* "erase" it */
625 printf("Invalid option '%s'\n", *argv);
634 tclean_gi(int argc, char **argv)
637 /* only difference between 'clean' and 'tclean' is one value */
638 /* (...and the fact that 'clean' is priv and 'tclean' is not) */
639 clean_gi(argc, argv);
646 clean_q(struct printer *pp)
649 struct dirent **queue;
651 int didhead, i, n, nitems, rmcp;
656 if (generic_qselect == QSEL_BYNAME) {
657 printf("%s:\n", pp->printer);
663 while (lp < &line[sizeof(line) - 1]) {
664 if ((*lp++ = *cp++) == 0)
668 linerem = sizeof(line) - (lp - line);
672 nitems = scandir(pp->spool_dir, &queue, doselect, sortq);
676 printf("%s:\n", pp->printer);
679 printf("\tcannot examine spool directory\n");
684 printf("%s:\n", pp->printer);
687 printf("\t** found a core file in %s !\n", pp->spool_dir);
692 printf("%s:\n", pp->printer);
694 printf("\t** ----- Sorted list of files being checked:\n");
697 cp = queue[i]->d_name;
698 printf("\t** [%3d] = %s\n", i, cp);
699 } while (++i < nitems);
700 printf("\t** ----- end of sorted list\n");
704 cp = queue[i]->d_name;
708 * A control file. Look for matching data-files.
711 * Note the logic here assumes that the hostname
712 * part of cf-filenames match the hostname part
713 * in df-filenames, and that is not necessarily
714 * true (eg: for multi-homed hosts). This needs
715 * some further thought...
718 while (i + 1 < nitems) {
719 cp1 = queue[i + 1]->d_name;
720 if (*cp1 != 'd' || strcmp(cp + 3, cp1 + 3))
728 } else if (*cp == 'e') {
730 * Must be an errrs or email temp file.
735 * Must be a df with no cf (otherwise, it would have
736 * been skipped above) or an rf or tf file (which can
737 * always be removed if it is old enough).
742 if (strlen(cp) >= linerem) {
743 printf("\t** internal error: 'line' overflow!\n");
744 printf("\t** spooldir = %s\n", pp->spool_dir);
745 printf("\t** cp = %s\n", cp);
748 strlcpy(lp, cp, linerem);
751 } while (++i < nitems);
755 wrapup_clean(int laststatus __unused)
758 printf("Checked %d queues, and ", cln_queuecnt);
759 if (cln_filecnt < 1) {
760 printf("no cruft was found\n");
764 printf("would have ");
766 printf("removed %d files (%ld bytes).\n", cln_filecnt, cln_sizecnt);
773 double agemod, agestat;
775 char linkbuf[BUFSIZ];
778 * We have to use lstat() instead of stat(), in case this is a df*
779 * "file" which is really a symlink due to 'lpr -s' processing. In
780 * that case, we need to check the last-mod time of the symlink, and
781 * not the file that the symlink is pointed at.
784 res = lstat(name, &stbuf);
787 printf("\terror return from stat(%s):\n", name);
788 printf("\t %s\n", strerror(errno));
792 agemod = difftime(cln_now, stbuf.st_mtime);
793 agestat = difftime(cln_now, stbuf.st_ctime);
795 /* this debugging-aid probably is not needed any more... */
796 printf("\t\t modify age=%g secs, stat age=%g secs\n",
799 if ((agemod <= cln_minage) && (agestat <= cln_minage))
803 * if this file is a symlink, then find out the target of the
804 * symlink before unlink-ing the file itself
806 if (S_ISLNK(stbuf.st_mode)) {
808 res = readlink(name, linkbuf, sizeof(linkbuf));
811 printf("\terror return from readlink(%s):\n", name);
812 printf("\t %s\n", strerror(errno));
815 if (res == sizeof(linkbuf))
821 cln_sizecnt += stbuf.st_size;
824 printf("\twould remove %s\n", name);
825 if (S_ISLNK(stbuf.st_mode)) {
826 printf("\t (which is a symlink to %s)\n", linkbuf);
833 printf("\tcannot remove %s (!)\n", name);
835 printf("\tremoved %s\n", name);
837 * Note that for a df* file, this code should also check to see
838 * if it is a symlink to some other file, and if the original
839 * lpr command included '-r' ("remove file"). Of course, this
840 * code would not be removing the df* file unless there was no
841 * matching cf* file, and without the cf* file it is currently
842 * impossible to determine if '-r' had been specified...
844 * As a result of this quandry, we may be leaving behind a
845 * user's file that was supposed to have been removed after
846 * being printed. This may effect services such as CAP or
847 * samba, if they were configured to use 'lpr -r', and if
848 * datafiles are not being properly removed.
850 if (S_ISLNK(stbuf.st_mode)) {
851 printf("\t (which was a symlink to %s)\n", linkbuf);
857 * Enable queuing to the printer (allow lpr to add new jobs to the queue).
860 enable_q(struct printer *pp)
865 lock_file_name(pp, lf, sizeof lf);
866 printf("%s:\n", pp->printer);
868 setres = set_qstate(SQS_ENABLEQ, lf);
875 disable_q(struct printer *pp)
880 lock_file_name(pp, lf, sizeof lf);
881 printf("%s:\n", pp->printer);
883 setres = set_qstate(SQS_DISABLEQ, lf);
887 * Disable queuing and printing and put a message into the status file
888 * (reason for being down). If the user specified `-msg', then use
889 * everything after that as the message for the status file. If the
890 * user did NOT specify `-msg', then the command should take the first
891 * parameter as the printer name, and all remaining parameters as the
892 * message for the status file. (This is to be compatible with the
893 * original definition of 'down', which was implemented long before
894 * `-msg' was around).
897 down_gi(int argc, char **argv)
900 /* If `-msg' was specified, then this routine has nothing to do. */
901 if (generic_msg != NULL)
905 * If the user only gave one parameter, then use a default msg.
906 * (if argc == 1 at this point, then *argv == name of printer).
909 generic_msg = strdup("printing disabled\n");
914 * The user specified multiple parameters, and did not specify
915 * `-msg'. Build a message from all the parameters after the
916 * first one (and nullify those parameters so generic-processing
917 * will not process them as printer-queue names).
921 generic_msg = args2line(argc, argv);
922 for (; argc > 0; argc--, argv++)
923 *argv = generic_nullarg; /* "erase" it */
927 down_q(struct printer *pp)
932 lock_file_name(pp, lf, sizeof lf);
933 printf("%s:\n", pp->printer);
935 setres = set_qstate(SQS_DISABLEQ+SQS_STOPP, lf);
937 upstat(pp, generic_msg, 1);
944 quit(int argc __unused, char **argv __unused)
950 * Kill and restart the daemon.
953 restart_q(struct printer *pp)
955 int killres, setres, startok;
958 lock_file_name(pp, lf, sizeof lf);
959 printf("%s:\n", pp->printer);
961 killres = kill_qtask(lf);
964 * XXX - if the kill worked, we should probably sleep for
965 * a second or so before trying to restart the queue.
968 /* make sure the queue is set to print jobs */
969 setres = set_qstate(SQS_STARTP, lf);
972 startok = startdaemon(pp);
975 printf("\tcouldn't restart daemon\n");
977 printf("\tdaemon restarted\n");
981 * Set the status message of each queue listed. Requires a "-msg"
982 * parameter to indicate the end of the queue list and start of msg text.
985 setstatus_gi(int argc __unused, char **argv __unused)
988 if (generic_msg == NULL) {
989 printf("You must specify '-msg' before the text of the new status message.\n");
995 setstatus_q(struct printer *pp)
999 lock_file_name(pp, lf, sizeof lf);
1000 printf("%s:\n", pp->printer);
1002 upstat(pp, generic_msg, 1);
1006 * Enable printing on the specified printer and startup the daemon.
1009 start_q(struct printer *pp)
1011 int setres, startok;
1012 char lf[MAXPATHLEN];
1014 lock_file_name(pp, lf, sizeof lf);
1015 printf("%s:\n", pp->printer);
1017 setres = set_qstate(SQS_STARTP, lf);
1020 startok = startdaemon(pp);
1023 printf("\tcouldn't start daemon\n");
1025 printf("\tdaemon started\n");
1030 * Print the status of the printer queue.
1033 status(struct printer *pp)
1039 char file[MAXPATHLEN];
1041 printf("%s:\n", pp->printer);
1042 lock_file_name(pp, file, sizeof file);
1043 if (stat(file, &stbuf) >= 0) {
1044 printf("\tqueuing is %s\n",
1045 ((stbuf.st_mode & LFM_QUEUE_DIS) ? "disabled"
1047 printf("\tprinting is %s\n",
1048 ((stbuf.st_mode & LFM_PRINT_DIS) ? "disabled"
1051 printf("\tqueuing is enabled\n");
1052 printf("\tprinting is enabled\n");
1054 if ((dirp = opendir(pp->spool_dir)) == NULL) {
1055 printf("\tcannot examine spool directory\n");
1059 while ((dp = readdir(dirp)) != NULL) {
1060 if (*dp->d_name == 'c' && dp->d_name[1] == 'f')
1065 printf("\tno entries in spool area\n");
1067 printf("\t1 entry in spool area\n");
1069 printf("\t%d entries in spool area\n", i);
1070 fd = open(file, O_RDONLY);
1071 if (fd < 0 || flock(fd, LOCK_SH|LOCK_NB) == 0) {
1072 close(fd); /* unlocks as well */
1073 printf("\tprinter idle\n");
1077 /* print out the contents of the status file, if it exists */
1078 status_file_name(pp, file, sizeof file);
1079 fd = open(file, O_RDONLY|O_SHLOCK);
1082 if (stbuf.st_size > 0) {
1084 while ((i = read(fd, line, sizeof(line))) > 0)
1085 fwrite(line, 1, i, stdout);
1087 close(fd); /* unlocks as well */
1092 * Stop the specified daemon after completing the current job and disable
1096 stop_q(struct printer *pp)
1099 char lf[MAXPATHLEN];
1101 lock_file_name(pp, lf, sizeof lf);
1102 printf("%s:\n", pp->printer);
1104 setres = set_qstate(SQS_STOPP, lf);
1107 upstat(pp, "printing disabled\n", 0);
1110 struct jobqueue **queue;
1115 * Put the specified jobs at the top of printer queue.
1118 topq(int argc, char **argv)
1122 int cmdstatus, changed;
1123 struct printer myprinter, *pp = &myprinter;
1126 printf("usage: topq printer [jobnum ...] [user ...]\n");
1133 cmdstatus = getprintcap(*argv, pp);
1136 fatal(pp, "%s", pcaperr(cmdstatus));
1137 case PCAPERR_NOTFOUND:
1138 printf("unknown printer %s\n", *argv);
1140 case PCAPERR_TCOPEN:
1141 printf("warning: %s: unresolved tc= reference(s)", *argv);
1143 case PCAPERR_SUCCESS:
1146 printf("%s:\n", pp->printer);
1149 if (chdir(pp->spool_dir) < 0) {
1150 printf("\tcannot chdir to %s\n", pp->spool_dir);
1154 nitems = getq(pp, &queue);
1158 mtime = queue[0]->job_time;
1159 for (i = argc; --i; ) {
1160 if (doarg(argv[i]) == 0) {
1161 printf("\tjob %s is not in the queue\n", argv[i]);
1166 for (i = 0; i < nitems; i++)
1170 printf("\tqueue order unchanged\n");
1174 * Turn on the public execute bit of the lock file to
1175 * get lpd to rebuild the queue after the current job.
1178 if (changed && stat(pp->lock_file, &stbuf) >= 0)
1179 chmod(pp->lock_file, stbuf.st_mode | LFM_RESET_QUE);
1186 * Reposition the job by changing the modification time of
1190 touch(struct jobqueue *jq)
1192 struct timeval tvp[2];
1195 tvp[0].tv_sec = tvp[1].tv_sec = --mtime;
1196 tvp[0].tv_usec = tvp[1].tv_usec = 0;
1198 ret = utimes(jq->job_cfname, tvp);
1204 * Checks if specified job name is in the printer's queue.
1205 * Returns: negative (-1) if argument name is not in the queue.
1210 struct jobqueue **qq;
1217 * Look for a job item consisting of system name, colon, number
1218 * (example: ucbarpa:114)
1220 if ((cp = strchr(job, ':')) != NULL) {
1228 * Check for job specified by number (example: 112 or 235ucbarpa).
1230 if (isdigit(*job)) {
1233 jobnum = jobnum * 10 + (*job++ - '0');
1234 while (isdigit(*job));
1235 for (qq = queue + nitems; --qq >= queue; ) {
1237 for (cp = (*qq)->job_cfname+3; isdigit(*cp); )
1238 n = n * 10 + (*cp++ - '0');
1241 if (*job && strcmp(job, cp) != 0)
1243 if (machine != NULL && strcmp(machine, cp) != 0)
1245 if (touch(*qq) == 0) {
1246 printf("\tmoved %s\n", (*qq)->job_cfname);
1253 * Process item consisting of owner's name (example: henry).
1255 for (qq = queue + nitems; --qq >= queue; ) {
1257 fp = fopen((*qq)->job_cfname, "r");
1261 while (getline(fp) > 0)
1265 if (line[0] != 'P' || strcmp(job, line+1) != 0)
1267 if (touch(*qq) == 0) {
1268 printf("\tmoved %s\n", (*qq)->job_cfname);
1276 * Enable both queuing & printing, and start printer (undo `down').
1279 up_q(struct printer *pp)
1281 int setres, startok;
1282 char lf[MAXPATHLEN];
1284 lock_file_name(pp, lf, sizeof lf);
1285 printf("%s:\n", pp->printer);
1287 setres = set_qstate(SQS_ENABLEQ+SQS_STARTP, lf);
1290 startok = startdaemon(pp);
1293 printf("\tcouldn't start daemon\n");
1295 printf("\tdaemon started\n");