]> andersk Git - moira.git/blame - clients/userreg/disable.c
invoke prs & fs with explicit path names;
[moira.git] / clients / userreg / disable.c
CommitLineData
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>
17
18#define LISTS (2*BUFSIZ)
19#define MAXLIN BUFSIZ
20
21#define EXACT 100
22#define ANY 101
23#define LIST 102
24#define RANGE 103
25#define EOS 104
26
27#define FILENAME "disabled.times"
28
29time_t itime, time();
30struct tm *loct;
31struct tm *localtime();
32char *malloc();
33char *realloc();
34int flag;
35char *list;
36char *listend;
37unsigned listsize;
38
39char *cmp();
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
2c307498 46char *disabled(msg)
47char **msg;
659a4a98 48{
49 register char *cp;
50 int hit;
51
2c307498 52 *msg = 0;
659a4a98 53 init();
54 append(FILENAME);
55 *listend++ = EOS;
56 *listend++ = EOS;
57
58 (void) time(&itime);
59 itime -= localtime(&itime)->tm_sec;
60 loct = localtime(&itime);
61 loct->tm_mon++; /* 1-12 for month */
62 if (loct->tm_wday == 0)
63 loct->tm_wday = 7; /* sunday is 7, not 0 */
64 hit = 0;
65 for(cp = list; *cp != EOS;) {
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) {
2c307498 73 *msg = cp;
659a4a98 74 hit++;
75 break;
76 }
77 while(*cp++ != 0)
78 ;
79 }
80 if (!hit)
81 return(NULL);
82 while (hit) {
83 itime += 60; /* add a minute */
84 loct = localtime(&itime);
85 loct->tm_mon++; /* 1-12 for month */
86 if (loct->tm_wday == 0)
87 loct->tm_wday = 7; /* sunday is 7, not 0 */
88 hit = 0;
89 for(cp = list; *cp != EOS;) {
90 flag = 0;
91 cp = cmp(cp, loct->tm_min);
92 cp = cmp(cp, loct->tm_hour);
93 cp = cmp(cp, loct->tm_mday);
94 cp = cmp(cp, loct->tm_mon);
95 cp = cmp(cp, loct->tm_wday);
96 if(flag == 0) {
97 hit++;
98 break;
99 }
100 while(*cp++ != 0)
101 ;
102 }
103 }
104 return(ctime(&itime));
105}
106
107static char *
108cmp(p, v)
109char *p;
110{
111 register char *cp;
112
113 cp = p;
114 switch(*cp++) {
115
116 case EXACT:
117 if (*cp++ != v)
118 flag++;
119 return(cp);
120
121 case ANY:
122 return(cp);
123
124 case LIST:
125 while(*cp != LIST)
126 if(*cp++ == v) {
127 while(*cp++ != LIST)
128 ;
129 return(cp);
130 }
131 flag++;
132 return(cp+1);
133
134 case RANGE:
135 if(*cp > v || cp[1] < v)
136 flag++;
137 return(cp+2);
138 }
139 if(cp[-1] != v)
140 flag++;
141 return(cp);
142}
143
144static init()
145{
146 /*
147 * Don't free in case was longer than LISTS. Trades off
148 * the rare case of crontab shrinking vs. the common case of
149 * extra realloc's needed in append() for a large crontab.
150 */
151 if (list == 0) {
152 list = malloc(LISTS);
153 listsize = LISTS;
154 }
155 listend = list;
156}
157
158static append(fn)
159char *fn;
160{
161 register i, c;
162 register char *cp;
163 register char *ocp;
164 register int n;
2c307498 165 FILE *f, *fopen();
659a4a98 166
2c307498 167 if ((f = fopen(fn, "r")) == (FILE *) NULL)
659a4a98 168 return;
169 cp = listend;
170loop:
171 if(cp > list+listsize-MAXLIN) {
172 int length = cp - list;
173
174 listsize += LISTS;
175 list = realloc(list, listsize);
176 cp = list + length;
177 }
178 ocp = cp;
179 for(i=0;; i++) {
180 do
2c307498 181 c = getc(f);
659a4a98 182 while(c == ' ' || c == '\t')
183 ;
184 if(c == EOF || c == '\n')
185 goto ignore;
186 if(i == 5)
187 break;
188 if(c == '*') {
189 *cp++ = ANY;
190 continue;
191 }
2c307498 192 if ((n = number(c, f)) < 0)
659a4a98 193 goto ignore;
2c307498 194 c = getc(f);
659a4a98 195 if(c == ',')
196 goto mlist;
197 if(c == '-')
198 goto mrange;
199 if(c != '\t' && c != ' ')
200 goto ignore;
201 *cp++ = EXACT;
202 *cp++ = n;
203 continue;
204
205 mlist:
206 *cp++ = LIST;
207 *cp++ = n;
208 do {
2c307498 209 if ((n = number(getc(f), f)) < 0)
659a4a98 210 goto ignore;
211 *cp++ = n;
2c307498 212 c = getc(f);
659a4a98 213 } while (c==',');
214 if(c != '\t' && c != ' ')
215 goto ignore;
216 *cp++ = LIST;
217 continue;
218
219 mrange:
220 *cp++ = RANGE;
221 *cp++ = n;
2c307498 222 if ((n = number(getc(f), f)) < 0)
659a4a98 223 goto ignore;
2c307498 224 c = getc(f);
659a4a98 225 if(c != '\t' && c != ' ')
226 goto ignore;
227 *cp++ = n;
228 }
229 while(c != '\n') {
230 if(c == EOF)
231 goto ignore;
232 if(c == '%')
233 c = '\n';
234 *cp++ = c;
2c307498 235 c = getc(f);
659a4a98 236 }
237 *cp++ = '\n';
238 *cp++ = 0;
239 goto loop;
240
241ignore:
242 cp = ocp;
243 while(c != '\n') {
244 if(c == EOF) {
2c307498 245 (void) fclose(f);
659a4a98 246 listend = cp;
247 return;
248 }
2c307498 249 c = getc(f);
659a4a98 250 }
251 goto loop;
252}
253
2c307498 254static number(c, f)
659a4a98 255register c;
2c307498 256FILE *f;
659a4a98 257{
258 register n = 0;
259
260 while (isdigit(c)) {
261 n = n*10 + c - '0';
2c307498 262 c = getc(f);
659a4a98 263 }
2c307498 264 (void) ungetc(c, f);
659a4a98 265 if (n>=100)
266 return(-1);
267 return(n);
268}
269
This page took 0.092522 seconds and 5 git commands to generate.