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