]> git.saurik.com Git - apple/system_cmds.git/blob - at.tproj/at.c
system_cmds-336.1.2.tar.gz
[apple/system_cmds.git] / at.tproj / at.c
1 /*
2 * at.c : Put file into atrun queue
3 * Copyright (C) 1993, 1994 Thomas Koenig
4 *
5 * Atrun & Atq modifications
6 * Copyright (C) 1993 David Parsons
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author(s) may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: src/usr.bin/at/at.c,v 1.29 2002/07/22 11:32:16 robert Exp $");
31
32 #define _USE_BSD 1
33
34 /* System Headers */
35
36 #include <sys/param.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/wait.h>
40 #include <ctype.h>
41 #include <dirent.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #ifndef __FreeBSD__
46 #include <getopt.h>
47 #endif
48 #include <glob.h>
49 #ifdef __FreeBSD__
50 #include <locale.h>
51 #endif
52 #include <pwd.h>
53 #include <signal.h>
54 #include <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 #ifdef __APPLE__
62 #include <get_compat.h>
63 #else /* !__APPLE */
64 #define COMPAT_MODE(a,b) (1)
65 #endif /* __APPLE__ */
66
67 /* Local headers */
68
69 #include "at.h"
70 #include "panic.h"
71 #include "parsetime.h"
72 #include "pathnames.h"
73 #include "perm.h"
74
75 #define MAIN
76 #include "privs.h"
77
78 /* Macros */
79
80 #ifndef ATJOB_DIR
81 #define ATJOB_DIR _PATH_ATJOBS
82 #endif
83
84 #ifndef LFILE
85 #define LFILE ATJOB_DIR ".lockfile"
86 #endif
87
88 #ifndef ATJOB_MX
89 #define ATJOB_MX 255
90 #endif
91
92 #define ALARMC 10 /* Number of seconds to wait for timeout */
93
94 #define SIZE 255
95 #define TIMESIZE 50
96
97 enum { ATQ, ATRM, AT, BATCH, CAT }; /* what program we want to run */
98
99 /* File scope variables */
100
101 const char *no_export[] =
102 {
103 "TERM", "TERMCAP", "DISPLAY", "_"
104 } ;
105 static int send_mail = 0;
106
107 /* External variables */
108
109 extern char **environ;
110 int fcreated;
111 char atfile[] = ATJOB_DIR "12345678901234";
112
113 char *atinput = (char*)0; /* where to get input from */
114 char atqueue = 0; /* which queue to examine for jobs (atq) */
115 char atverify = 0; /* verify time instead of queuing job */
116 char *namep;
117 int posixly_correct; /* Behave as per POSIX */
118 /* http://www.opengroup.org/onlinepubs/009695399/utilities/at.html */
119
120 /* Function declarations */
121
122 static void sigc(int signo);
123 static void alarmc(int signo);
124 static char *cwdname(void);
125 static void writefile(time_t runtimer, char queue);
126 static void list_jobs(long *, int);
127 static long nextjob(void);
128 static time_t ttime(const char *arg);
129 static int in_job_list(long, long *, int);
130 static long *get_job_list(int, char *[], int *);
131
132 /* Signal catching functions */
133
134 static void sigc(int signo __unused)
135 {
136 /* If the user presses ^C, remove the spool file and exit
137 */
138 if (fcreated)
139 {
140 PRIV_START
141 unlink(atfile);
142 PRIV_END
143 }
144
145 _exit(EXIT_FAILURE);
146 }
147
148 static void alarmc(int signo __unused)
149 {
150 char buf[1024];
151
152 /* Time out after some seconds. */
153 strlcpy(buf, namep, sizeof(buf));
154 strlcat(buf, ": file locking timed out\n", sizeof(buf));
155 write(STDERR_FILENO, buf, strlen(buf));
156 sigc(0);
157 }
158
159 /* Local functions */
160
161 static char *cwdname(void)
162 {
163 /* Read in the current directory; the name will be overwritten on
164 * subsequent calls.
165 */
166 static char *ptr = NULL;
167 static size_t size = SIZE;
168
169 if (ptr == NULL)
170 if ((ptr = malloc(size)) == NULL)
171 errx(EXIT_FAILURE, "virtual memory exhausted");
172
173 while (1)
174 {
175 if (ptr == NULL)
176 panic("out of memory");
177
178 if (getcwd(ptr, size-1) != NULL)
179 return ptr;
180
181 if (errno != ERANGE)
182 perr("cannot get directory");
183
184 free (ptr);
185 size += SIZE;
186 if ((ptr = malloc(size)) == NULL)
187 errx(EXIT_FAILURE, "virtual memory exhausted");
188 }
189 }
190
191 static long
192 nextjob()
193 {
194 long jobno;
195 FILE *fid;
196
197 if ((fid = fopen(ATJOB_DIR ".SEQ", "r+")) != (FILE*)0) {
198 if (fscanf(fid, "%5lx", &jobno) == 1) {
199 rewind(fid);
200 jobno = (1+jobno) % 0xfffff; /* 2^20 jobs enough? */
201 fprintf(fid, "%05lx\n", jobno);
202 }
203 else
204 jobno = EOF;
205 fclose(fid);
206 return jobno;
207 }
208 else if ((fid = fopen(ATJOB_DIR ".SEQ", "w")) != (FILE*)0) {
209 fprintf(fid, "%05lx\n", jobno = 1);
210 fclose(fid);
211 return 1;
212 }
213 return EOF;
214 }
215
216 static void
217 writefile(time_t runtimer, char queue)
218 {
219 /* This does most of the work if at or batch are invoked for writing a job.
220 */
221 long jobno;
222 char *ap, *ppos, *mailname;
223 struct passwd *pass_entry;
224 struct stat statbuf;
225 int fdes, lockdes, fd2;
226 FILE *fp, *fpin;
227 struct sigaction act;
228 char **atenv;
229 int ch;
230 mode_t cmask;
231 struct flock lock;
232
233 #ifdef __FreeBSD__
234 (void) setlocale(LC_TIME, "");
235 #endif
236
237 /* Install the signal handler for SIGINT; terminate after removing the
238 * spool file if necessary
239 */
240 act.sa_handler = sigc;
241 sigemptyset(&(act.sa_mask));
242 act.sa_flags = 0;
243
244 sigaction(SIGINT, &act, NULL);
245
246 ppos = atfile + strlen(ATJOB_DIR);
247
248 /* Loop over all possible file names for running something at this
249 * particular time, see if a file is there; the first empty slot at any
250 * particular time is used. Lock the file LFILE first to make sure
251 * we're alone when doing this.
252 */
253
254 PRIV_START
255
256 if ((lockdes = open(LFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
257 perr("cannot open lockfile " LFILE);
258
259 lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
260 lock.l_len = 0;
261
262 act.sa_handler = alarmc;
263 sigemptyset(&(act.sa_mask));
264 act.sa_flags = 0;
265
266 /* Set an alarm so a timeout occurs after ALARMC seconds, in case
267 * something is seriously broken.
268 */
269 sigaction(SIGALRM, &act, NULL);
270 alarm(ALARMC);
271 fcntl(lockdes, F_SETLKW, &lock);
272 alarm(0);
273
274 if ((jobno = nextjob()) == EOF)
275 perr("cannot generate job number");
276
277 sprintf(ppos, "%c%5lx%8lx", queue,
278 jobno, (unsigned long) (runtimer/60));
279
280 for(ap=ppos; *ap != '\0'; ap ++)
281 if (*ap == ' ')
282 *ap = '0';
283
284 if (stat(atfile, &statbuf) != 0)
285 if (errno != ENOENT)
286 perr("cannot access " ATJOB_DIR);
287
288 /* Create the file. The x bit is only going to be set after it has
289 * been completely written out, to make sure it is not executed in the
290 * meantime. To make sure they do not get deleted, turn off their r
291 * bit. Yes, this is a kluge.
292 */
293 cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
294 if ((fdes = creat(atfile, O_WRONLY)) == -1)
295 perr("cannot create atjob file");
296
297 if ((fd2 = dup(fdes)) <0)
298 perr("error in dup() of job file");
299
300 if(fchown(fd2, real_uid, real_gid) != 0)
301 perr("cannot give away file");
302
303 PRIV_END
304
305 /* We no longer need suid root; now we just need to be able to write
306 * to the directory, if necessary.
307 */
308
309 REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
310
311 /* We've successfully created the file; let's set the flag so it
312 * gets removed in case of an interrupt or error.
313 */
314 fcreated = 1;
315
316 /* Now we can release the lock, so other people can access it
317 */
318 lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0;
319 lock.l_len = 0;
320 fcntl(lockdes, F_SETLKW, &lock);
321 close(lockdes);
322
323 if((fp = fdopen(fdes, "w")) == NULL)
324 panic("cannot reopen atjob file");
325
326 /* Get the userid to mail to, first by trying getlogin(),
327 * then from LOGNAME, finally from getpwuid().
328 */
329 mailname = getlogin();
330 if (mailname == NULL)
331 mailname = getenv("LOGNAME");
332
333 if ((mailname == NULL) || (mailname[0] == '\0')
334 || (strlen(mailname) >= MAXLOGNAME) || (getpwnam(mailname)==NULL))
335 {
336 pass_entry = getpwuid(real_uid);
337 if (pass_entry != NULL)
338 mailname = pass_entry->pw_name;
339 }
340
341 if (atinput != (char *) NULL)
342 {
343 fpin = freopen(atinput, "r", stdin);
344 if (fpin == NULL)
345 perr("cannot open input file");
346 }
347 fprintf(fp, "#!/bin/sh\n# atrun uid=%ld gid=%ld\n# mail %.*s %d\n",
348 (long) real_uid, (long) real_gid, MAXLOGNAME - 1, mailname,
349 send_mail);
350
351 /* Write out the umask at the time of invocation
352 */
353 fprintf(fp, "umask %lo\n", (unsigned long) cmask);
354
355 /* Write out the environment. Anything that may look like a
356 * special character to the shell is quoted, except for \n, which is
357 * done with a pair of "'s. Don't export the no_export list (such
358 * as TERM or DISPLAY) because we don't want these.
359 */
360 for (atenv= environ; *atenv != NULL; atenv++)
361 {
362 int export = 1;
363 char *eqp;
364
365 eqp = strchr(*atenv, '=');
366 if (ap == NULL)
367 eqp = *atenv;
368 else
369 {
370 size_t i;
371 for (i=0; i<sizeof(no_export)/sizeof(no_export[0]); i++)
372 {
373 export = export
374 && (strncmp(*atenv, no_export[i],
375 (size_t) (eqp-*atenv)) != 0);
376 }
377 eqp++;
378 }
379
380 if (export)
381 {
382 fwrite(*atenv, sizeof(char), eqp-*atenv, fp);
383 for(ap = eqp;*ap != '\0'; ap++)
384 {
385 if (*ap == '\n')
386 fprintf(fp, "\"\n\"");
387 else
388 {
389 if (!isalnum(*ap)) {
390 switch (*ap) {
391 case '%': case '/': case '{': case '[':
392 case ']': case '=': case '}': case '@':
393 case '+': case '#': case ',': case '.':
394 case ':': case '-': case '_':
395 break;
396 default:
397 fputc('\\', fp);
398 break;
399 }
400 }
401 fputc(*ap, fp);
402 }
403 }
404 fputs("; export ", fp);
405 fwrite(*atenv, sizeof(char), eqp-*atenv -1, fp);
406 fputc('\n', fp);
407
408 }
409 }
410 /* Cd to the directory at the time and write out all the
411 * commands the user supplies from stdin.
412 */
413 fprintf(fp, "cd ");
414 for (ap = cwdname(); *ap != '\0'; ap++)
415 {
416 if (*ap == '\n')
417 fprintf(fp, "\"\n\"");
418 else
419 {
420 if (*ap != '/' && !isalnum(*ap))
421 fputc('\\', fp);
422
423 fputc(*ap, fp);
424 }
425 }
426 /* Test cd's exit status: die if the original directory has been
427 * removed, become unreadable or whatever
428 */
429 fprintf(fp, " || {\n\t echo 'Execution directory "
430 "inaccessible' >&2\n\t exit 1\n}\n");
431
432 while((ch = getchar()) != EOF)
433 fputc(ch, fp);
434
435 fprintf(fp, "\n");
436 if (ferror(fp))
437 panic("output error");
438
439 if (ferror(stdin))
440 panic("input error");
441
442 fclose(fp);
443
444 /* Set the x bit so that we're ready to start executing
445 */
446
447 if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
448 perr("cannot give away file");
449
450 close(fd2);
451 if (posixly_correct) {
452 struct tm runtime;
453 char timestr[TIMESIZE];
454 runtime = *localtime(&runtimer);
455 strftime(timestr, TIMESIZE, "%a %b %e %T %Y", &runtime);
456 fprintf(stderr, "job %ld at %s\n", jobno, timestr);
457 } else
458 fprintf(stderr, "Job %ld will be executed using /bin/sh\n", jobno);
459 }
460
461 static int
462 in_job_list(long job, long *joblist, int len)
463 {
464 int i;
465
466 for (i = 0; i < len; i++)
467 if (job == joblist[i])
468 return 1;
469
470 return 0;
471 }
472
473 static void
474 list_one_job(char *name, long *joblist, int len, int *first)
475 {
476 struct stat buf;
477 struct tm runtime;
478 unsigned long ctm;
479 char queue;
480 long jobno;
481 time_t runtimer;
482 char timestr[TIMESIZE];
483
484 if (stat(name, &buf) != 0)
485 perr("cannot stat in " ATJOB_DIR);
486
487 /* See it's a regular file and has its x bit turned on and
488 * is the user's
489 */
490 if (!S_ISREG(buf.st_mode)
491 || ((buf.st_uid != real_uid) && ! (real_uid == 0))
492 || !(S_IXUSR & buf.st_mode || atverify))
493 return;
494
495 if(sscanf(name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3)
496 return;
497
498 /* If jobs are given, only list those jobs */
499 if (joblist && !in_job_list(jobno, joblist, len))
500 return;
501
502 if (atqueue && (queue != atqueue))
503 return;
504
505 runtimer = 60*(time_t) ctm;
506 runtime = *localtime(&runtimer);
507 strftime(timestr, TIMESIZE, "%a %b %e %T %Y", &runtime);
508 if (*first) {
509 if (!posixly_correct)
510 printf("Date\t\t\t\tOwner\t\tQueue\tJob#\n");
511 *first=0;
512 }
513 if (posixly_correct)
514 printf("%ld\t%s\n", jobno, timestr);
515 else {
516 struct passwd *pw = getpwuid(buf.st_uid);
517
518 printf("%s\t%s\t%c%s\t%s\n",
519 timestr,
520 pw ? pw->pw_name : "???",
521 queue,
522 (S_IXUSR & buf.st_mode) ? "":"(done)",
523 name);
524 }
525 }
526
527 static void
528 list_jobs(long *joblist, int len)
529 {
530 /* List all a user's jobs in the queue, by looping through ATJOB_DIR,
531 * or everybody's if we are root
532 */
533 DIR *spool;
534 struct dirent *dirent;
535 int first=1;
536
537 #ifdef __FreeBSD__
538 (void) setlocale(LC_TIME, "");
539 #endif
540
541 PRIV_START
542
543 if (chdir(ATJOB_DIR) != 0)
544 perr("cannot change to " ATJOB_DIR);
545
546 if (joblist) { /* Force order to match POSIX */
547 char jobglob[32];
548 glob_t g;
549 int i;
550
551 sprintf(jobglob, "?%05lx*", joblist[0]);
552 g.gl_offs = 0;
553 glob(jobglob, GLOB_DOOFFS, NULL, &g);
554 for (i = 1; i < len; i++) {
555 sprintf(jobglob, "?%05lx*", joblist[i]);
556 glob(jobglob, GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
557 }
558 for (i = 0; i < g.gl_pathc; i++) {
559 list_one_job(g.gl_pathv[i], joblist, len, &first);
560 }
561 globfree(&g);
562 } else {
563 if ((spool = opendir(".")) == NULL)
564 perr("cannot open " ATJOB_DIR);
565
566 /* Loop over every file in the directory
567 */
568 while((dirent = readdir(spool)) != NULL) {
569 list_one_job(dirent->d_name, joblist, len, &first);
570 }
571 closedir(spool);
572 }
573 PRIV_END
574 }
575
576 static void
577 process_jobs(int argc, char **argv, int what)
578 {
579 /* Delete every argument (job - ID) given
580 */
581 int i;
582 struct stat buf;
583 DIR *spool;
584 struct dirent *dirent;
585 unsigned long ctm;
586 char queue;
587 long jobno;
588
589 PRIV_START
590
591 if (chdir(ATJOB_DIR) != 0)
592 perr("cannot change to " ATJOB_DIR);
593
594 if ((spool = opendir(".")) == NULL)
595 perr("cannot open " ATJOB_DIR);
596
597 PRIV_END
598
599 /* Loop over every file in the directory
600 */
601 while((dirent = readdir(spool)) != NULL) {
602
603 PRIV_START
604 if (stat(dirent->d_name, &buf) != 0)
605 perr("cannot stat in " ATJOB_DIR);
606 PRIV_END
607
608 if(sscanf(dirent->d_name, "%c%5lx%8lx", &queue, &jobno, &ctm)!=3)
609 continue;
610
611 for (i=optind; i < argc; i++) {
612 if (atoi(argv[i]) == jobno || strcmp(argv[i], dirent->d_name)==0) {
613 if ((buf.st_uid != real_uid) && !(real_uid == 0))
614 errx(EXIT_FAILURE, "%s: not owner", argv[i]);
615 switch (what) {
616 case ATRM:
617
618 PRIV_START
619
620 if (unlink(dirent->d_name) != 0)
621 perr(dirent->d_name);
622
623 PRIV_END
624
625 break;
626
627 case CAT:
628 {
629 FILE *fp;
630 int ch;
631
632 PRIV_START
633
634 fp = fopen(dirent->d_name,"r");
635
636 PRIV_END
637
638 if (!fp) {
639 perr("cannot open file");
640 }
641 while((ch = getc(fp)) != EOF) {
642 putchar(ch);
643 }
644 }
645 break;
646
647 default:
648 errx(EXIT_FAILURE, "internal error, process_jobs = %d",
649 what);
650 }
651 }
652 }
653 }
654 } /* process_jobs */
655
656 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
657
658 static time_t
659 ttime(const char *arg)
660 {
661 /*
662 * This is pretty much a copy of stime_arg1() from touch.c. I changed
663 * the return value and the argument list because it's more convenient
664 * (IMO) to do everything in one place. - Joe Halpin
665 */
666 struct timeval tv[2];
667 time_t now;
668 struct tm *t;
669 int yearset;
670 char *p;
671
672 if (gettimeofday(&tv[0], NULL))
673 panic("Cannot get current time");
674
675 /* Start with the current time. */
676 now = tv[0].tv_sec;
677 if ((t = localtime(&now)) == NULL)
678 panic("localtime");
679 /* [[CC]YY]MMDDhhmm[.SS] */
680 if ((p = strchr(arg, '.')) == NULL)
681 t->tm_sec = 0; /* Seconds defaults to 0. */
682 else {
683 if (strlen(p + 1) != 2)
684 goto terr;
685 *p++ = '\0';
686 t->tm_sec = ATOI2(p);
687 }
688
689 yearset = 0;
690 switch(strlen(arg)) {
691 case 12: /* CCYYMMDDhhmm */
692 t->tm_year = ATOI2(arg);
693 t->tm_year *= 100;
694 yearset = 1;
695 /* FALLTHROUGH */
696 case 10: /* YYMMDDhhmm */
697 if (yearset) {
698 yearset = ATOI2(arg);
699 t->tm_year += yearset;
700 } else {
701 yearset = ATOI2(arg);
702 t->tm_year = yearset + 2000;
703 }
704 t->tm_year -= 1900; /* Convert to UNIX time. */
705 /* FALLTHROUGH */
706 case 8: /* MMDDhhmm */
707 t->tm_mon = ATOI2(arg);
708 --t->tm_mon; /* Convert from 01-12 to 00-11 */
709 t->tm_mday = ATOI2(arg);
710 t->tm_hour = ATOI2(arg);
711 t->tm_min = ATOI2(arg);
712 break;
713 default:
714 goto terr;
715 }
716
717 t->tm_isdst = -1; /* Figure out DST. */
718 tv[0].tv_sec = tv[1].tv_sec = mktime(t);
719 if (tv[0].tv_sec != -1)
720 return tv[0].tv_sec;
721 else
722 terr:
723 panic(
724 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
725 }
726
727 static long *
728 get_job_list(int argc, char *argv[], int *joblen)
729 {
730 int i, len;
731 long *joblist;
732 char *ep;
733
734 joblist = NULL;
735 len = argc;
736 if (len > 0) {
737 if ((joblist = malloc(len * sizeof(*joblist))) == NULL)
738 panic("out of memory");
739
740 for (i = 0; i < argc; i++) {
741 errno = 0;
742 if ((joblist[i] = strtol(argv[i], &ep, 10)) < 0 ||
743 ep == argv[i] || *ep != '\0' || errno)
744 panic("invalid job number");
745 }
746 }
747
748 *joblen = len;
749 return joblist;
750 }
751
752 int
753 main(int argc, char **argv)
754 {
755 int c;
756 char queue = DEFAULT_AT_QUEUE;
757 char queue_set = 0;
758 char *pgm;
759
760 int program = AT; /* our default program */
761 const char *options = "q:f:t:rmvldbc"; /* default options for at */
762 time_t timer;
763 long *joblist;
764 int joblen;
765
766 posixly_correct = COMPAT_MODE("bin/at", "Unix2003");
767 joblist = NULL;
768 joblen = 0;
769 timer = -1;
770 RELINQUISH_PRIVS
771
772 /* Eat any leading paths
773 */
774 if ((pgm = strrchr(argv[0], '/')) == NULL)
775 pgm = argv[0];
776 else
777 pgm++;
778
779 namep = pgm;
780
781 /* find out what this program is supposed to do
782 */
783 if (strcmp(pgm, "atq") == 0) {
784 program = ATQ;
785 options = "q:v";
786 }
787 else if (strcmp(pgm, "atrm") == 0) {
788 program = ATRM;
789 options = "";
790 }
791 else if (strcmp(pgm, "batch") == 0) {
792 program = BATCH;
793 options = "f:q:mv";
794 }
795
796 /* process whatever options we can process
797 */
798 opterr=1;
799 while ((c=getopt(argc, argv, options)) != -1)
800 switch (c) {
801 case 'v': /* verify time settings */
802 atverify = 1;
803 break;
804
805 case 'm': /* send mail when job is complete */
806 send_mail = 1;
807 break;
808
809 case 'f':
810 atinput = optarg;
811 break;
812
813 case 'q': /* specify queue */
814 if (strlen(optarg) > 1)
815 usage();
816
817 atqueue = queue = *optarg;
818 if (!(islower(queue)||isupper(queue)))
819 usage();
820
821 queue_set = 1;
822 break;
823
824 case 'd':
825 warnx("-d is deprecated; use -r instead");
826 /* fall through to 'r' */
827
828 case 'r':
829 if (program != AT)
830 usage();
831
832 program = ATRM;
833 options = "";
834 break;
835
836 case 't':
837 if (program != AT)
838 usage();
839 timer = ttime(optarg);
840 break;
841
842 case 'l':
843 if (program != AT)
844 usage();
845
846 program = ATQ;
847 options = "q:";
848 break;
849
850 case 'b':
851 if (program != AT)
852 usage();
853
854 program = BATCH;
855 options = "f:q:mv";
856 break;
857
858 case 'c':
859 program = CAT;
860 options = "";
861 break;
862
863 default:
864 usage();
865 break;
866 }
867 /* end of options eating
868 */
869
870 /* select our program
871 */
872 if(!check_permission())
873 errx(EXIT_FAILURE, "you do not have permission to use this program");
874 switch (program) {
875 case ATQ:
876
877 REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
878
879 if (queue_set == 0)
880 joblist = get_job_list(argc - optind, argv + optind, &joblen);
881 list_jobs(joblist, joblen);
882 break;
883
884 case ATRM:
885
886 REDUCE_PRIV(DAEMON_UID, DAEMON_GID)
887
888 process_jobs(argc, argv, ATRM);
889 break;
890
891 case CAT:
892
893 process_jobs(argc, argv, CAT);
894 break;
895
896 case AT:
897 /*
898 * If timer is > -1, then the user gave the time with -t. In that
899 * case, it's already been set. If not, set it now.
900 */
901 if (timer == -1)
902 timer = parsetime(argc, argv);
903
904 if (atverify)
905 {
906 struct tm *tm = localtime(&timer);
907 fprintf(stderr, "%s\n", asctime(tm));
908 }
909 writefile(timer, queue);
910 break;
911
912 case BATCH:
913 if (queue_set)
914 queue = toupper(queue);
915 else
916 queue = DEFAULT_BATCH_QUEUE;
917
918 if (argc > optind)
919 timer = parsetime(argc, argv);
920 else
921 timer = time(NULL);
922
923 if (atverify)
924 {
925 struct tm *tm = localtime(&timer);
926 fprintf(stderr, "%s\n", asctime(tm));
927 }
928
929 writefile(timer, queue);
930 break;
931
932 default:
933 panic("internal error");
934 break;
935 }
936 exit(EXIT_SUCCESS);
937 }