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