]>
Commit | Line | Data |
---|---|---|
659a4a98 | 1 | /* $Header$ |
2 | * | |
3 | * disabled: check to see if registration is enabled right now. Most of this | |
4 | * code is stolen from the cron daemon. | |
7189310c | 5 | * |
6 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. | |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
659a4a98 | 9 | */ |
10 | ||
7189310c | 11 | #include <mit-copyright.h> |
659a4a98 | 12 | #include <stdio.h> |
13 | #include <ctype.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/time.h> | |
16 | #include <sys/file.h> | |
623165b4 | 17 | #include "files.h" |
659a4a98 | 18 | |
5eaef520 | 19 | #define LISTS (2 * BUFSIZ) |
659a4a98 | 20 | #define MAXLIN BUFSIZ |
21 | ||
22 | #define EXACT 100 | |
23 | #define ANY 101 | |
24 | #define LIST 102 | |
25 | #define RANGE 103 | |
26 | #define EOS 104 | |
27 | ||
659a4a98 | 28 | time_t itime, time(); |
29 | struct tm *loct; | |
30 | struct tm *localtime(); | |
31 | char *malloc(); | |
32 | char *realloc(); | |
33 | int flag; | |
34 | char *list; | |
35 | char *listend; | |
36 | unsigned listsize; | |
37 | ||
38 | char *cmp(); | |
44d12d58 | 39 | int number(char c, FILE *f); |
659a4a98 | 40 | |
41 | /* This routine will determine if registration is enabled at this time. If | |
42 | * NULL is returned, registration is OK. Otherwise, the string returned | |
43 | * will indicate the time that registration will be re-enabled. | |
44 | */ | |
45 | ||
5eaef520 | 46 | char *disabled(char **msg) |
659a4a98 | 47 | { |
44d12d58 | 48 | char *cp; |
5eaef520 | 49 | int hit; |
659a4a98 | 50 | |
5eaef520 | 51 | *msg = 0; |
52 | init(); | |
53 | append(DISABLE_FILE); | |
54 | *listend++ = EOS; | |
55 | *listend++ = EOS; | |
659a4a98 | 56 | |
5eaef520 | 57 | time(&itime); |
58 | itime -= localtime(&itime)->tm_sec; | |
59 | loct = localtime(&itime); | |
60 | loct->tm_mon++; /* 1-12 for month */ | |
61 | if (loct->tm_wday == 0) | |
62 | loct->tm_wday = 7; /* sunday is 7, not 0 */ | |
63 | hit = 0; | |
64 | for (cp = list; *cp != EOS;) | |
65 | { | |
66 | flag = 0; | |
67 | cp = cmp(cp, loct->tm_min); | |
68 | cp = cmp(cp, loct->tm_hour); | |
69 | cp = cmp(cp, loct->tm_mday); | |
70 | cp = cmp(cp, loct->tm_mon); | |
71 | cp = cmp(cp, loct->tm_wday); | |
72 | if (flag == 0) | |
73 | { | |
74 | *msg = cp; | |
75 | hit++; | |
76 | break; | |
659a4a98 | 77 | } |
5eaef520 | 78 | while (*cp++) |
79 | ; | |
659a4a98 | 80 | } |
5eaef520 | 81 | if (!hit) |
82 | return NULL; | |
83 | while (hit) | |
84 | { | |
85 | itime += 60; /* add a minute */ | |
86 | loct = localtime(&itime); | |
87 | loct->tm_mon++; /* 1-12 for month */ | |
88 | if (loct->tm_wday == 0) | |
89 | loct->tm_wday = 7; /* sunday is 7, not 0 */ | |
90 | hit = 0; | |
91 | for(cp = list; *cp != EOS;) | |
92 | { | |
93 | flag = 0; | |
94 | cp = cmp(cp, loct->tm_min); | |
95 | cp = cmp(cp, loct->tm_hour); | |
96 | cp = cmp(cp, loct->tm_mday); | |
97 | cp = cmp(cp, loct->tm_mon); | |
98 | cp = cmp(cp, loct->tm_wday); | |
99 | if (!flag) | |
100 | { | |
101 | hit++; | |
102 | break; | |
659a4a98 | 103 | } |
5eaef520 | 104 | while(*cp++ != 0) |
105 | ; | |
659a4a98 | 106 | } |
107 | } | |
5eaef520 | 108 | return ctime(&itime); |
659a4a98 | 109 | } |
110 | ||
5eaef520 | 111 | char *cmp(char *p, int v) |
659a4a98 | 112 | { |
44d12d58 | 113 | char *cp; |
659a4a98 | 114 | |
5eaef520 | 115 | cp = p; |
116 | switch (*cp++) | |
117 | { | |
118 | case EXACT: | |
119 | if (*cp++ != v) | |
120 | flag++; | |
121 | return cp; | |
659a4a98 | 122 | |
5eaef520 | 123 | case ANY: |
124 | return cp; | |
659a4a98 | 125 | |
5eaef520 | 126 | case LIST: |
127 | while (*cp != LIST) | |
128 | { | |
129 | if (*cp++ == v) | |
130 | { | |
131 | while (*cp++ != LIST) | |
132 | ; | |
133 | return cp; | |
134 | } | |
659a4a98 | 135 | } |
5eaef520 | 136 | flag++; |
137 | return cp + 1; | |
138 | ||
139 | case RANGE: | |
140 | if (*cp > v || cp[1] < v) | |
141 | flag++; | |
142 | return cp + 2; | |
143 | } | |
144 | if (cp[-1] != v) | |
145 | flag++; | |
146 | return cp; | |
659a4a98 | 147 | } |
148 | ||
5eaef520 | 149 | init(void) |
659a4a98 | 150 | { |
5eaef520 | 151 | /* |
152 | * Don't free in case was longer than LISTS. Trades off | |
153 | * the rare case of crontab shrinking vs. the common case of | |
154 | * extra realloc's needed in append() for a large crontab. | |
155 | */ | |
156 | if (list == 0) | |
157 | { | |
158 | list = malloc(LISTS); | |
159 | listsize = LISTS; | |
160 | } | |
161 | listend = list; | |
659a4a98 | 162 | } |
163 | ||
5eaef520 | 164 | append(char *fn) |
659a4a98 | 165 | { |
44d12d58 | 166 | int i, c; |
167 | char *cp; | |
168 | char *ocp; | |
169 | int n; | |
5eaef520 | 170 | FILE *f, *fopen(); |
659a4a98 | 171 | |
5eaef520 | 172 | if (!(f = fopen(fn, "r"))) |
173 | return; | |
174 | cp = listend; | |
659a4a98 | 175 | loop: |
5eaef520 | 176 | if (cp > list + listsize - MAXLIN) |
177 | { | |
178 | int length = cp - list; | |
659a4a98 | 179 | |
5eaef520 | 180 | listsize += LISTS; |
181 | list = realloc(list, listsize); | |
182 | cp = list + length; | |
183 | } | |
184 | ocp = cp; | |
185 | for (i = 0; ; i++) | |
186 | { | |
187 | do | |
188 | c = getc(f); | |
189 | while(c == ' ' || c == '\t'); | |
190 | if (c == EOF || c == '\n') | |
191 | goto ignore; | |
192 | if (i == 5) | |
193 | break; | |
194 | if (c == '*') | |
195 | { | |
196 | *cp++ = ANY; | |
197 | continue; | |
659a4a98 | 198 | } |
5eaef520 | 199 | if ((n = number(c, f)) < 0) |
200 | goto ignore; | |
201 | c = getc(f); | |
202 | if(c == ',') | |
203 | goto mlist; | |
204 | if(c == '-') | |
205 | goto mrange; | |
206 | if(c != '\t' && c != ' ') | |
207 | goto ignore; | |
208 | *cp++ = EXACT; | |
209 | *cp++ = n; | |
210 | continue; | |
659a4a98 | 211 | |
5eaef520 | 212 | mlist: |
213 | *cp++ = LIST; | |
214 | *cp++ = n; | |
215 | do | |
216 | { | |
217 | if ((n = number(getc(f), f)) < 0) | |
218 | goto ignore; | |
219 | *cp++ = n; | |
220 | c = getc(f); | |
659a4a98 | 221 | } |
5eaef520 | 222 | while (c == ','); |
223 | if (c != '\t' && c != ' ') | |
224 | goto ignore; | |
225 | *cp++ = LIST; | |
226 | continue; | |
227 | ||
228 | mrange: | |
229 | *cp++ = RANGE; | |
230 | *cp++ = n; | |
231 | if ((n = number(getc(f), f)) < 0) | |
232 | goto ignore; | |
233 | c = getc(f); | |
234 | if (c != '\t' && c != ' ') | |
235 | goto ignore; | |
236 | *cp++ = n; | |
237 | } | |
238 | while (c != '\n') | |
239 | { | |
240 | if (c == EOF) | |
241 | goto ignore; | |
242 | if (c == '%') | |
243 | c = '\n'; | |
244 | *cp++ = c; | |
245 | c = getc(f); | |
246 | } | |
247 | *cp++ = '\n'; | |
248 | *cp++ = 0; | |
249 | goto loop; | |
659a4a98 | 250 | |
251 | ignore: | |
5eaef520 | 252 | cp = ocp; |
253 | while (c != '\n') | |
254 | { | |
255 | if (c == EOF) | |
256 | { | |
257 | fclose(f); | |
258 | listend = cp; | |
259 | return; | |
659a4a98 | 260 | } |
5eaef520 | 261 | c = getc(f); |
262 | } | |
263 | goto loop; | |
659a4a98 | 264 | } |
265 | ||
44d12d58 | 266 | int number(char c, FILE *f) |
659a4a98 | 267 | { |
44d12d58 | 268 | int n = 0; |
659a4a98 | 269 | |
5eaef520 | 270 | while (isdigit(c)) |
271 | { | |
272 | n = n * 10 + c - '0'; | |
273 | c = getc(f); | |
274 | } | |
275 | ungetc(c, f); | |
276 | if (n >= 100) | |
277 | return -1; | |
278 | return n; | |
659a4a98 | 279 | } |
280 |