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