2 * timestr.c See if a string like 'Su2300-0700' matches (UUCP style).
4 * Version: @(#)timestr.c 0.10 21-Mar-1999 miquels@cistron.nl
17 { "su", "mo", "tu", "we", "th", "fr", "sa", "wk", "any", "al" };
19 #define DAYMIN (24*60)
20 #define WEEKMIN (24*60*7)
21 #define val(x) (( (x) < 48 || (x) > 57) ? 0 : ((x) - 48))
24 # define xprintf if (1) printf
26 # define xprintf if (0) printf
32 static int strcode (char **str)
36 xprintf("strcode %s called\n", *str);
38 for (i = 0; i < 10; i++) {
42 if (strncmp(*str, days[i], l) == 0) {
47 xprintf("strcode result %d\n", i);
49 return (i >= 10) ? -1 : i;
54 * Fill bitmap with hours/mins.
56 static int hour_fill(char *bitmap, char *tm)
62 xprintf("hour_fill called for %s\n", tm);
65 * Get timerange in start and end.
68 if ((p = strchr(tm, '-')) != NULL) {
70 if (p - tm != 5 || strlen(p) < 4 || !isdigit(*p))
72 end = 600 * val(p[0]) + 60 * val(p[1]) + atoi(p + 2);
78 if (strlen(tm) < 4 || !isdigit(*tm))
80 start = 600 * val(tm[0]) + 60 * val(tm[1]) + atoi(tm + 2);
81 if (end < 0) end = start;
83 /* Treat 2400 as 0000, and do some more silent error checks. */
85 if (start < 0) start = 0;
86 if (end >= DAYMIN) end = DAYMIN - 1;
87 if (start >= DAYMIN) start = DAYMIN - 1;
89 xprintf("hour_fill: range from %d to %d\n", start, end);
98 xprintf("setting byte %d, bit %d\n", byte, bit);
99 bitmap[byte] |= (1 << bit);
108 * Call the fill bitmap function for every day listed.
110 static int day_fill(char *bitmap, char *tm)
116 for (hr = tm; *hr; hr++)
119 if (hr == tm) tm = "Al";
121 xprintf("dayfill: hr %s tm %s\n", hr, tm);
123 while ((start = strcode(&tm)) >= 0) {
125 * Find start and end weekdays and
126 * build a valid range 0 - 6.
130 if ((end = strcode(&tm)) < 0)
143 xprintf("day_fill: range from %d to %d\n", start, end);
145 hour_fill(bitmap + 180 * n, hr);
156 * Fill the week bitmap with allowed times.
158 static int week_fill(char *bitmap, char *tm)
163 strncpy(tmp, tm, 128);
165 for (s = tmp; *s; s++)
166 if (isupper(*s)) *s = tolower(*s);
168 s = strtok(tmp, ",|");
171 s = strtok(NULL, ",|");
178 * Match a timestring and return seconds left.
179 * -1 for no match, 0 for unlimited.
181 int timestr_match(char *tmstr, time_t t)
184 char bitmap[WEEKMIN / 8];
194 now = tm->tm_wday * DAYMIN + tm->tm_hour * 60 + tm->tm_min;
196 memset(bitmap, 0, sizeof(bitmap));
197 week_fill(bitmap, tmstr);
201 for (i = 0; i < 7; i++) {
203 s = bitmap + 180 * i;
204 for (y = 0; y < 23; y++) {
205 s = bitmap + 180 * i + (75 * y) / 10;
206 printf("%c", memcmp(s, null, 8) == 0 ? '.' : '#');
213 * See how many minutes we have.
219 xprintf("READ: checking byte %d bit %d\n", byte, bit);
220 if (!(bitmap[byte] & (1 << bit)))
230 return (i == now) ? 0 : tot;
235 int main(int argc, char **argv)
240 fprintf(stderr, "Usage: test timestring\n");
243 l = timestr_match(argv[1], time(NULL));
244 printf ("%s: %d seconds left\n", argv[1], l);