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