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