]> andersk Git - moira.git/blob - clients/userreg/disable.c
added copyright message
[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 #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
29 time_t  itime, time();
30 struct  tm *loct;
31 struct  tm *localtime();
32 char    *malloc();
33 char    *realloc();
34 int     flag;
35 char    *list;
36 char    *listend;
37 unsigned listsize;
38
39 char *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
46 char *disabled(msg)
47 char **msg;
48 {
49     register char *cp;
50     int hit;
51
52     *msg = 0;
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) {
73             *msg = cp;
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
107 static char *
108 cmp(p, v)
109 char *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
144 static 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
158 static append(fn)
159 char *fn;
160 {
161         register i, c;
162         register char *cp;
163         register char *ocp;
164         register int n;
165         FILE *f, *fopen();
166
167         if ((f = fopen(fn, "r")) == (FILE *) NULL)
168                 return;
169         cp = listend;
170 loop:
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
181                         c = getc(f);
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                 }
192                 if ((n = number(c, f)) < 0)
193                         goto ignore;
194                 c = getc(f);
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 {
209                         if ((n = number(getc(f), f)) < 0)
210                                 goto ignore;
211                         *cp++ = n;
212                         c = getc(f);
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;
222                 if ((n = number(getc(f), f)) < 0)
223                         goto ignore;
224                 c = getc(f);
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;
235                 c = getc(f);
236         }
237         *cp++ = '\n';
238         *cp++ = 0;
239         goto loop;
240
241 ignore:
242         cp = ocp;
243         while(c != '\n') {
244                 if(c == EOF) {
245                         (void) fclose(f);
246                         listend = cp;
247                         return;
248                 }
249                 c = getc(f);
250         }
251         goto loop;
252 }
253
254 static number(c, f)
255 register c;
256 FILE *f;
257 {
258         register n = 0;
259
260         while (isdigit(c)) {
261                 n = n*10 + c - '0';
262                 c = getc(f);
263         }
264         (void) ungetc(c, f);
265         if (n>=100)
266                 return(-1);
267         return(n);
268 }
269
This page took 0.063234 seconds and 5 git commands to generate.