]> andersk Git - moira.git/blob - clients/userreg/disable.c
eliminate use of the `register' keyword: let the compiler decide
[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 #include "files.h"
18
19 #define LISTS   (2 * BUFSIZ)
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
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();
39 int number(char c, FILE *f);
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(char **msg)
47 {
48   char *cp;
49   int hit;
50
51   *msg = 0;
52   init();
53   append(DISABLE_FILE);
54   *listend++ = EOS;
55   *listend++ = EOS;
56
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;
77         }
78       while (*cp++)
79         ;
80     }
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;
103             }
104           while(*cp++ != 0)
105             ;
106         }
107     }
108   return ctime(&itime);
109 }
110
111 char *cmp(char *p, int v)
112 {
113   char *cp;
114
115   cp = p;
116   switch (*cp++)
117     {
118     case EXACT:
119       if (*cp++ != v)
120         flag++;
121       return cp;
122
123     case ANY:
124       return cp;
125
126     case LIST:
127       while (*cp != LIST)
128         {
129           if (*cp++ == v)
130             {
131               while (*cp++ != LIST)
132                 ;
133               return cp;
134             }
135         }
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;
147 }
148
149 init(void)
150 {
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;
162 }
163
164 append(char *fn)
165 {
166   int i, c;
167   char *cp;
168   char *ocp;
169   int n;
170   FILE *f, *fopen();
171
172   if (!(f = fopen(fn, "r")))
173     return;
174   cp = listend;
175 loop:
176   if (cp > list + listsize - MAXLIN)
177     {
178       int length = cp - list;
179
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;
198         }
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;
211
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);
221         }
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;
250
251 ignore:
252   cp = ocp;
253   while (c != '\n')
254     {
255       if (c == EOF)
256         {
257           fclose(f);
258           listend = cp;
259           return;
260         }
261       c = getc(f);
262     }
263   goto loop;
264 }
265
266 int number(char c, FILE *f)
267 {
268   int n = 0;
269
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;
279 }
280
This page took 0.067988 seconds and 5 git commands to generate.