fix comments
[unix-history] / usr / src / usr.bin / at / atrun / atrun.c
CommitLineData
a9f28345 1static char *sccsid = "@(#)atrun.c 4.2 (Berkeley) %G%";
cf2bca11
BJ
2/*
3 * Run programs submitted by at.
4 */
5#include <stdio.h>
6#include <sys/types.h>
7#include <sys/dir.h>
8#include <time.h>
9#include <sys/stat.h>
10
11# define DIR "/usr/spool/at"
12# define PDIR "past"
13# define LASTF "/usr/spool/at/lasttimedone"
14
15int nowtime;
16int nowdate;
17int nowyear;
18
19main(argc, argv)
20char **argv;
21{
22 int tt, day, year, uniq;
23 struct direct dirent;
24 FILE *dirf;
25 char file[DIRSIZ+1];
26
27 chdir(DIR);
28 makenowtime();
29 if ((dirf = fopen(".", "r")) == NULL) {
30 fprintf(stderr, "Cannot read at directory\n");
31 exit(1);
32 }
33 while (fread((char *)&dirent, sizeof(dirent), 1, dirf) == 1) {
34 if (dirent.d_ino==0)
35 continue;
36 strcpyn(file, dirent.d_name, DIRSIZ);
37 file[DIRSIZ] = '\0';
38 if (sscanf(file, "%2d.%3d.%4d.%2d", &year, &day, &tt, &uniq) != 4)
39 continue;
40 if (nowyear < year)
41 continue;
42 if (nowyear==year && nowdate < day)
43 continue;
44 if (nowyear==year && nowdate==day && nowtime < tt)
45 continue;
46 run(file);
47 }
48 fclose(dirf);
49 updatetime(nowtime);
50 exit(0);
51}
52
53makenowtime()
54{
55 long t;
56 struct tm *localtime();
57 register struct tm *tp;
58
59 time(&t);
60 tp = localtime(&t);
61 nowtime = tp->tm_hour*100 + tp->tm_min;
62 nowdate = tp->tm_yday;
63 nowyear = tp->tm_year;
64}
65
66updatetime(t)
67{
68 FILE *tfile;
69
70 tfile = fopen(LASTF, "w");
71 if (tfile == NULL) {
72 fprintf(stderr, "can't write lastfile\n");
73 exit(1);
74 }
75 fprintf(tfile, "%04d\n", t);
76}
77
78run(file)
79char *file;
80{
81 struct stat stbuf;
82 register pid, i;
83 char sbuf[64];
84
85 /* printf("running %s\n", file); */
86 if (fork()!=0)
87 return;
88 for (i=0; i<15; i++)
89 close(i);
90 dup(dup(open("/dev/null", 0)));
91 sprintf(sbuf, "%s/%s", PDIR, file);
92 link(file, sbuf);
93 unlink(file);
94 chdir(PDIR);
95 if (stat(file, &stbuf) == -1)
96 exit(1);
97 if (pid = fork()) {
98 if (pid == -1)
99 exit(1);
100 wait((int *)0);
101 unlink(file);
102 exit(0);
103 }
104 setgid(stbuf.st_gid);
105 setuid(stbuf.st_uid);
cf2bca11
BJ
106 execl("/bin/sh", "sh", file, 0);
107 execl("/usr/bin/sh", "sh", file, 0);
108 fprintf(stderr, "Can't execl shell\n");
109 exit(1);
110}