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