1: #include <sys/types.h>
2: #include <stdio.h>
3: #include <ctype.h>
4: #include <signal.h>
5: #include <time.h>
6: #include <sys/stat.h>
7:
8: #define INTER 60 /* seconds between executions */
9:
10: #define LISTS 512
11:
12: #define EXACT 100
13: #define ANY 101
14: #define LIST 102
15: #define RANGE 103
16: #define EOS 104
17: char crontab[] = "/usr/lib/crontab";
18: time_t itime;
19: struct tm *loct;
20: struct tm *localtime();
21: char *malloc();
22: char *realloc();
23: int flag;
24: char *list;
25: unsigned listsize;
26:
27: main()
28: {
29: register char *cp;
30: char *cmp();
31: time_t filetime = 0;
32:
33: setgid(1);
34: setuid(1);
35: if (fork())
36: exit(0);
37: chdir("/");
38: freopen(crontab, "r", stdin);
39: freopen("/", "r", stdout);
40: freopen("/", "r", stderr);
41: signal(SIGHUP, SIG_IGN);
42: signal(SIGINT, SIG_IGN);
43: signal(SIGQUIT, SIG_IGN);
44: time(&itime);
45: itime -= localtime(&itime)->tm_sec;
46: fclose(stdin);
47:
48: for (;; itime+=INTER, slp()) {
49: struct stat cstat;
50:
51: if (stat(crontab, &cstat) == -1)
52: continue;
53: if (cstat.st_mtime > filetime) {
54: filetime = cstat.st_mtime;
55: init();
56: }
57: loct = localtime(&itime);
58: loct->tm_mon++; /* 1-12 for month */
59: for(cp = list; *cp != EOS;) {
60: flag = 0;
61: cp = cmp(cp, loct->tm_min);
62: cp = cmp(cp, loct->tm_hour);
63: cp = cmp(cp, loct->tm_mday);
64: cp = cmp(cp, loct->tm_mon);
65: cp = cmp(cp, loct->tm_wday);
66: if(flag == 0) {
67: slp();
68: ex(cp);
69: }
70: while(*cp++ != 0)
71: ;
72: }
73: }
74: }
75:
76: char *
77: cmp(p, v)
78: char *p;
79: {
80: register char *cp;
81:
82: cp = p;
83: switch(*cp++) {
84:
85: case EXACT:
86: if (*cp++ != v)
87: flag++;
88: return(cp);
89:
90: case ANY:
91: return(cp);
92:
93: case LIST:
94: while(*cp != LIST)
95: if(*cp++ == v) {
96: while(*cp++ != LIST)
97: ;
98: return(cp);
99: }
100: flag++;
101: return(cp+1);
102:
103: case RANGE:
104: if(*cp > v || cp[1] < v)
105: flag++;
106: return(cp+2);
107: }
108: if(cp[-1] != v)
109: flag++;
110: return(cp);
111: }
112:
113: slp()
114: {
115: time_t i;
116: time_t t;
117:
118: time(&t);
119: i = itime - t;
120: if ((i > INTER * 5) || (i < -(INTER * 5))) {
121: /*
122: * The time was changed; resynchronize.
123: */
124: itime = t;
125: itime -= localtime(&t)->tm_sec;
126: return;
127: }
128: if(i > 0)
129: sleep((unsigned int) i);
130: }
131:
132: ex(s)
133: char *s;
134: {
135: int st;
136:
137: if(fork()) {
138: wait(&st);
139: return;
140: }
141: if(fork())
142: exit(0);
143: freopen("/", "r", stdin);
144: execl("/bin/sh", "sh", "-c", s, 0);
145: exit(0);
146: }
147:
148: init()
149: {
150: register i, c;
151: register char *cp;
152: register char *ocp;
153: register int n;
154:
155: freopen(crontab, "r", stdin);
156: if (list) {
157: free(list);
158: list = realloc(list, LISTS);
159: } else
160: list = malloc(LISTS);
161: listsize = LISTS;
162: cp = list;
163:
164: loop:
165: if(cp > list+listsize-100) {
166: char *olist;
167: listsize += LISTS;
168: olist = list;
169: free(list);
170: list = realloc(list, listsize);
171: cp = list + (cp - olist);
172: }
173: ocp = cp;
174: for(i=0;; i++) {
175: do
176: c = getchar();
177: while(c == ' ' || c == '\t')
178: ;
179: if(c == EOF || c == '\n')
180: goto ignore;
181: if(i == 5)
182: break;
183: if(c == '*') {
184: *cp++ = ANY;
185: continue;
186: }
187: if ((n = number(c)) < 0)
188: goto ignore;
189: c = getchar();
190: if(c == ',')
191: goto mlist;
192: if(c == '-')
193: goto mrange;
194: if(c != '\t' && c != ' ')
195: goto ignore;
196: *cp++ = EXACT;
197: *cp++ = n;
198: continue;
199:
200: mlist:
201: *cp++ = LIST;
202: *cp++ = n;
203: do {
204: if ((n = number(getchar())) < 0)
205: goto ignore;
206: *cp++ = n;
207: c = getchar();
208: } while (c==',');
209: if(c != '\t' && c != ' ')
210: goto ignore;
211: *cp++ = LIST;
212: continue;
213:
214: mrange:
215: *cp++ = RANGE;
216: *cp++ = n;
217: if ((n = number(getchar())) < 0)
218: goto ignore;
219: c = getchar();
220: if(c != '\t' && c != ' ')
221: goto ignore;
222: *cp++ = n;
223: }
224: while(c != '\n') {
225: if(c == EOF)
226: goto ignore;
227: if(c == '%')
228: c = '\n';
229: *cp++ = c;
230: c = getchar();
231: }
232: *cp++ = '\n';
233: *cp++ = 0;
234: goto loop;
235:
236: ignore:
237: cp = ocp;
238: while(c != '\n') {
239: if(c == EOF) {
240: *cp++ = EOS;
241: *cp++ = EOS;
242: fclose(stdin);
243: return;
244: }
245: c = getchar();
246: }
247: goto loop;
248: }
249:
250: number(c)
251: register c;
252: {
253: register n = 0;
254:
255: while (isdigit(c)) {
256: n = n*10 + c - '0';
257: c = getchar();
258: }
259: ungetc(c, stdin);
260: if (n>=100)
261: return(-1);
262: return(n);
263: }
Defined functions
cmp
defined in line
76; used 6 times
ex
defined in line
132; used 1 times
init
defined in line
148; used 1 times
main
defined in line
27;
never used
slp
defined in line
113; used 2 times
Defined variables
flag
defined in line
23; used 6 times
itime
defined in line
18; used 8 times
list
defined in line
24; used 13 times
loct
defined in line
19; used 7 times
Defined macros
ANY
defined in line
13; used 1 times
EOS
defined in line
16; used 3 times
EXACT
defined in line
12; used 1 times
INTER
defined in line
8; used 3 times
LIST
defined in line
14; used 4 times
LISTS
defined in line
10; used 4 times
RANGE
defined in line
15; used 1 times