calendar.c revision 1.46 1 1.46 lukem /* $NetBSD: calendar.c,v 1.46 2008/07/21 14:19:21 lukem Exp $ */
2 1.7 glass
3 1.1 cgd /*
4 1.7 glass * Copyright (c) 1989, 1993, 1994
5 1.7 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.28 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.46 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 1.46 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.7 glass #if 0
40 1.8 jtc static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
41 1.7 glass #endif
42 1.46 lukem __RCSID("$NetBSD: calendar.c,v 1.46 2008/07/21 14:19:21 lukem Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.1 cgd #include <sys/time.h>
47 1.1 cgd #include <sys/stat.h>
48 1.1 cgd #include <sys/uio.h>
49 1.7 glass #include <sys/wait.h>
50 1.7 glass
51 1.7 glass #include <ctype.h>
52 1.7 glass #include <err.h>
53 1.7 glass #include <errno.h>
54 1.7 glass #include <fcntl.h>
55 1.1 cgd #include <pwd.h>
56 1.44 christos #include <stdbool.h>
57 1.7 glass #include <stdio.h>
58 1.7 glass #include <stdlib.h>
59 1.7 glass #include <string.h>
60 1.13 kleink #include <time.h>
61 1.1 cgd #include <tzfile.h>
62 1.1 cgd #include <unistd.h>
63 1.7 glass
64 1.1 cgd #include "pathnames.h"
65 1.1 cgd
66 1.44 christos static unsigned short lookahead = 1;
67 1.44 christos static unsigned short weekend = 2;
68 1.44 christos static char *fname = NULL;
69 1.44 christos static char *datestr = NULL;
70 1.44 christos static const char *defaultnames[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR, NULL};
71 1.26 christos static struct passwd *pw;
72 1.26 christos static char path[MAXPATHLEN + 1];
73 1.44 christos static bool doall = false;
74 1.44 christos static bool cpp_restricted = false;
75 1.26 christos
76 1.26 christos /* 1-based month, 0-based days, cumulative */
77 1.44 christos static const int daytab[][14] = {
78 1.26 christos { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
79 1.26 christos { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
80 1.26 christos };
81 1.26 christos static struct tm *tp;
82 1.44 christos static const int *cumdays;
83 1.44 christos static int offset, yrdays;
84 1.26 christos static char dayname[10];
85 1.26 christos
86 1.26 christos static struct iovec header[] = {
87 1.44 christos { __UNCONST("From: "), 6 },
88 1.26 christos { NULL, 0 },
89 1.44 christos { __UNCONST(" (Reminder Service)\nTo: "), 24 },
90 1.26 christos { NULL, 0 },
91 1.44 christos { __UNCONST("\nSubject: "), 10 },
92 1.26 christos { NULL, 0 },
93 1.44 christos { __UNCONST("'s Calendar\nPrecedence: bulk\n\n"), 30 },
94 1.26 christos };
95 1.26 christos
96 1.44 christos static const char *days[] = {
97 1.26 christos "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
98 1.26 christos };
99 1.26 christos
100 1.44 christos static const char *months[] = {
101 1.26 christos "jan", "feb", "mar", "apr", "may", "jun",
102 1.26 christos "jul", "aug", "sep", "oct", "nov", "dec", NULL,
103 1.26 christos };
104 1.26 christos
105 1.26 christos static void atodays(int, char *, unsigned short *);
106 1.26 christos static void cal(void);
107 1.26 christos static void closecal(FILE *);
108 1.26 christos static int getday(char *);
109 1.26 christos static int getfield(char *, char **, int *);
110 1.26 christos static void getmmdd(struct tm *, char *);
111 1.26 christos static int getmonth(char *);
112 1.44 christos static bool isnow(char *);
113 1.26 christos static FILE *opencal(void);
114 1.26 christos static void settime(void);
115 1.42 perry static void usage(void) __dead;
116 1.7 glass
117 1.7 glass int
118 1.44 christos main(int argc, char **argv)
119 1.1 cgd {
120 1.1 cgd int ch;
121 1.15 mycroft const char *caldir;
122 1.1 cgd
123 1.44 christos (void)setprogname(argv[0]); /* for portability */
124 1.44 christos
125 1.44 christos while ((ch = getopt(argc, argv, "-ad:f:l:w:x")) != -1) {
126 1.7 glass switch (ch) {
127 1.1 cgd case '-': /* backward contemptible */
128 1.1 cgd case 'a':
129 1.1 cgd if (getuid()) {
130 1.7 glass errno = EPERM;
131 1.44 christos err(EXIT_FAILURE, NULL);
132 1.1 cgd }
133 1.44 christos doall = true;
134 1.1 cgd break;
135 1.10 thorpej case 'd':
136 1.10 thorpej datestr = optarg;
137 1.10 thorpej break;
138 1.10 thorpej case 'f':
139 1.10 thorpej fname = optarg;
140 1.10 thorpej break;
141 1.10 thorpej case 'l':
142 1.10 thorpej atodays(ch, optarg, &lookahead);
143 1.10 thorpej break;
144 1.10 thorpej case 'w':
145 1.10 thorpej atodays(ch, optarg, &weekend);
146 1.10 thorpej break;
147 1.33 jwise case 'x':
148 1.44 christos cpp_restricted = true;
149 1.38 wiz break;
150 1.1 cgd case '?':
151 1.1 cgd default:
152 1.1 cgd usage();
153 1.1 cgd }
154 1.44 christos }
155 1.1 cgd argc -= optind;
156 1.1 cgd argv += optind;
157 1.1 cgd
158 1.1 cgd if (argc)
159 1.1 cgd usage();
160 1.1 cgd
161 1.1 cgd settime();
162 1.26 christos if (doall) {
163 1.44 christos /*
164 1.44 christos * XXX - This ignores the user's CALENDAR_DIR variable.
165 1.44 christos * Run under user's login shell?
166 1.44 christos */
167 1.7 glass while ((pw = getpwent()) != NULL) {
168 1.1 cgd (void)setegid(pw->pw_gid);
169 1.1 cgd (void)seteuid(pw->pw_uid);
170 1.44 christos if (chdir(pw->pw_dir) != -1)
171 1.1 cgd cal();
172 1.1 cgd (void)seteuid(0);
173 1.1 cgd }
174 1.26 christos } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
175 1.44 christos if (chdir(caldir) != -1)
176 1.26 christos cal();
177 1.44 christos } else if ((pw = getpwuid(geteuid())) != NULL) {
178 1.44 christos if (chdir(pw->pw_dir) != -1)
179 1.32 jwise cal();
180 1.26 christos }
181 1.44 christos return 0;
182 1.1 cgd }
183 1.1 cgd
184 1.26 christos static void
185 1.26 christos cal(void)
186 1.1 cgd {
187 1.44 christos bool printing;
188 1.7 glass FILE *fp;
189 1.26 christos char *line;
190 1.1 cgd
191 1.7 glass if ((fp = opencal()) == NULL)
192 1.1 cgd return;
193 1.44 christos printing = false;
194 1.44 christos while ((line = fparseln(stdin,
195 1.44 christos NULL, NULL, NULL, FPARSELN_UNESCCOMM)) != NULL) {
196 1.26 christos if (line[0] == '\0')
197 1.1 cgd continue;
198 1.26 christos if (line[0] != '\t')
199 1.44 christos printing = isnow(line);
200 1.1 cgd if (printing)
201 1.26 christos (void)fprintf(fp, "%s\n", line);
202 1.26 christos free(line);
203 1.26 christos
204 1.1 cgd }
205 1.1 cgd closecal(fp);
206 1.1 cgd }
207 1.1 cgd
208 1.26 christos static void
209 1.26 christos settime(void)
210 1.1 cgd {
211 1.7 glass time_t now;
212 1.1 cgd
213 1.1 cgd (void)time(&now);
214 1.1 cgd tp = localtime(&now);
215 1.44 christos if (datestr)
216 1.10 thorpej getmmdd(tp, datestr);
217 1.44 christos
218 1.12 christos if (isleap(tp->tm_year + TM_YEAR_BASE)) {
219 1.1 cgd yrdays = DAYSPERLYEAR;
220 1.1 cgd cumdays = daytab[1];
221 1.1 cgd } else {
222 1.1 cgd yrdays = DAYSPERNYEAR;
223 1.1 cgd cumdays = daytab[0];
224 1.1 cgd }
225 1.1 cgd /* Friday displays Monday's events */
226 1.10 thorpej offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead;
227 1.1 cgd header[5].iov_base = dayname;
228 1.1 cgd header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd /*
232 1.1 cgd * Possible date formats include any combination of:
233 1.1 cgd * 3-charmonth (January, Jan, Jan)
234 1.1 cgd * 3-charweekday (Friday, Monday, mon.)
235 1.1 cgd * numeric month or day (1, 2, 04)
236 1.1 cgd *
237 1.1 cgd * Any character may separate them, or they may not be separated. Any line,
238 1.1 cgd * following a line that is matched, that starts with "whitespace", is shown
239 1.1 cgd * along with the matched line.
240 1.1 cgd */
241 1.44 christos static bool
242 1.44 christos isnow(char *endp)
243 1.1 cgd {
244 1.44 christos int day;
245 1.44 christos int flags;
246 1.44 christos int month;
247 1.44 christos int v1;
248 1.44 christos int v2;
249 1.1 cgd
250 1.1 cgd #define F_ISMONTH 0x01
251 1.1 cgd #define F_ISDAY 0x02
252 1.35 jwise #define F_WILDMONTH 0x04
253 1.35 jwise #define F_WILDDAY 0x08
254 1.35 jwise
255 1.1 cgd flags = 0;
256 1.35 jwise
257 1.1 cgd /* didn't recognize anything, skip it */
258 1.1 cgd if (!(v1 = getfield(endp, &endp, &flags)))
259 1.44 christos return false;
260 1.44 christos
261 1.7 glass if (flags & F_ISDAY || v1 > 12) {
262 1.1 cgd /* found a day */
263 1.1 cgd day = v1;
264 1.43 christos /* if no recognizable month, assume wildcard ('*') month */
265 1.45 christos if ((month = getfield(endp, &endp, &flags)) == 0) {
266 1.43 christos flags |= F_ISMONTH | F_WILDMONTH;
267 1.43 christos month = tp->tm_mon + 1;
268 1.43 christos }
269 1.7 glass } else if (flags & F_ISMONTH) {
270 1.1 cgd month = v1;
271 1.1 cgd /* if no recognizable day, assume the first */
272 1.45 christos if ((day = getfield(endp, &endp, &flags)) == 0)
273 1.1 cgd day = 1;
274 1.1 cgd } else {
275 1.1 cgd v2 = getfield(endp, &endp, &flags);
276 1.7 glass if (flags & F_ISMONTH) {
277 1.1 cgd day = v1;
278 1.1 cgd month = v2;
279 1.1 cgd } else {
280 1.1 cgd /* F_ISDAY set, v2 > 12, or no way to tell */
281 1.1 cgd month = v1;
282 1.1 cgd /* if no recognizable day, assume the first */
283 1.1 cgd day = v2 ? v2 : 1;
284 1.1 cgd }
285 1.1 cgd }
286 1.44 christos if (flags & F_WILDMONTH && flags & F_WILDDAY)
287 1.44 christos return true;
288 1.35 jwise
289 1.44 christos if (flags & F_WILDMONTH && flags & F_ISDAY && day == tp->tm_mday)
290 1.44 christos return true;
291 1.35 jwise
292 1.44 christos if (flags & F_ISMONTH && flags & F_WILDDAY && month == tp->tm_mon + 1)
293 1.44 christos return true;
294 1.35 jwise
295 1.7 glass if (flags & F_ISDAY)
296 1.4 cgd day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
297 1.1 cgd day = cumdays[month] + day;
298 1.1 cgd
299 1.1 cgd /* if today or today + offset days */
300 1.1 cgd if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
301 1.44 christos return true;
302 1.44 christos
303 1.1 cgd /* if number of days left in this year + days to event in next year */
304 1.1 cgd if (yrdays - tp->tm_yday + day <= offset)
305 1.44 christos return true;
306 1.44 christos
307 1.44 christos return false;
308 1.1 cgd }
309 1.1 cgd
310 1.26 christos static int
311 1.44 christos getfield(char *p, char **endp, int *flags)
312 1.1 cgd {
313 1.1 cgd int val;
314 1.44 christos char *start;
315 1.44 christos char savech;
316 1.1 cgd
317 1.18 christos #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
318 1.18 christos !isalpha((unsigned char)*p) && *p != '*')
319 1.18 christos
320 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
321 1.26 christos continue;
322 1.1 cgd if (*p == '*') { /* `*' is current month */
323 1.35 jwise if (!(*flags & F_ISMONTH)) {
324 1.44 christos *flags |= F_ISMONTH | F_WILDMONTH;
325 1.44 christos *endp = p + 1;
326 1.44 christos return tp->tm_mon + 1;
327 1.35 jwise } else {
328 1.44 christos *flags |= F_ISDAY | F_WILDDAY;
329 1.44 christos *endp = p + 1;
330 1.44 christos return 1;
331 1.35 jwise }
332 1.1 cgd }
333 1.18 christos if (isdigit((unsigned char)*p)) {
334 1.44 christos val = (int)strtol(p, &p, 10); /* if 0, it's failure */
335 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
336 1.26 christos continue;
337 1.1 cgd *endp = p;
338 1.44 christos return val;
339 1.1 cgd }
340 1.44 christos for (start = p; *p != '\0' && isalpha((unsigned char)*++p); /*EMPTY*/)
341 1.26 christos continue;
342 1.1 cgd savech = *p;
343 1.1 cgd *p = '\0';
344 1.44 christos if ((val = getmonth(start)) != 0)
345 1.1 cgd *flags |= F_ISMONTH;
346 1.44 christos else if ((val = getday(start)) != 0)
347 1.1 cgd *flags |= F_ISDAY;
348 1.44 christos else {
349 1.4 cgd *p = savech;
350 1.44 christos return 0;
351 1.4 cgd }
352 1.18 christos for (*p = savech; FLDCHAR(*p); ++p)
353 1.26 christos continue;
354 1.1 cgd *endp = p;
355 1.44 christos return val;
356 1.1 cgd }
357 1.1 cgd
358 1.26 christos static FILE *
359 1.26 christos opencal(void)
360 1.1 cgd {
361 1.44 christos int fd;
362 1.44 christos int pdes[2];
363 1.44 christos const char **name;
364 1.1 cgd
365 1.1 cgd /* open up calendar file as stdin */
366 1.31 jwise if (fname == NULL) {
367 1.31 jwise for (name = defaultnames; *name != NULL; name++) {
368 1.44 christos if (freopen(*name, "rf", stdin) == NULL)
369 1.31 jwise continue;
370 1.31 jwise else
371 1.31 jwise break;
372 1.31 jwise }
373 1.31 jwise if (*name == NULL) {
374 1.31 jwise if (doall)
375 1.44 christos return NULL;
376 1.44 christos err(EXIT_FAILURE, "Cannot open calendar file");
377 1.31 jwise }
378 1.44 christos } else if (freopen(fname, "rf", stdin) == NULL) {
379 1.1 cgd if (doall)
380 1.44 christos return NULL;
381 1.44 christos err(EXIT_FAILURE, "Cannot open `%s'", fname);
382 1.1 cgd }
383 1.31 jwise
384 1.44 christos if (pipe(pdes) == -1) {
385 1.26 christos warn("Cannot open pipe");
386 1.44 christos return NULL;
387 1.26 christos }
388 1.31 jwise
389 1.26 christos switch (fork()) {
390 1.44 christos case -1:
391 1.44 christos /* error */
392 1.1 cgd (void)close(pdes[0]);
393 1.1 cgd (void)close(pdes[1]);
394 1.44 christos return NULL;
395 1.1 cgd case 0:
396 1.1 cgd /* child -- stdin already setup, set stdout to pipe input */
397 1.1 cgd if (pdes[1] != STDOUT_FILENO) {
398 1.1 cgd (void)dup2(pdes[1], STDOUT_FILENO);
399 1.1 cgd (void)close(pdes[1]);
400 1.1 cgd }
401 1.1 cgd (void)close(pdes[0]);
402 1.33 jwise /* tell CPP to only open regular files */
403 1.44 christos if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1) == -1)
404 1.44 christos err(EXIT_FAILURE, "Cannot restrict cpp");
405 1.44 christos cpp_restricted = true;
406 1.33 jwise
407 1.26 christos (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
408 1.26 christos "-I" _PATH_CALENDARS, NULL);
409 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_CPP);
410 1.26 christos /*NOTREACHED*/
411 1.44 christos default:
412 1.44 christos /* parent -- set stdin to pipe output */
413 1.44 christos (void)dup2(pdes[0], STDIN_FILENO);
414 1.44 christos (void)close(pdes[0]);
415 1.44 christos (void)close(pdes[1]);
416 1.31 jwise
417 1.44 christos /* not reading all calendar files, just set output to stdout */
418 1.44 christos if (!doall)
419 1.44 christos return stdout;
420 1.44 christos
421 1.44 christos /*
422 1.44 christos * Set output to a temporary file, so if no output
423 1.44 christos * don't send mail.
424 1.44 christos */
425 1.44 christos (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
426 1.44 christos if ((fd = mkstemp(path)) == -1) {
427 1.44 christos warn("Cannot create temporary file");
428 1.44 christos return NULL;
429 1.44 christos }
430 1.44 christos return fdopen(fd, "w+");
431 1.26 christos }
432 1.44 christos /*NOTREACHED*/
433 1.1 cgd }
434 1.1 cgd
435 1.26 christos static void
436 1.44 christos closecal(FILE *fp)
437 1.1 cgd {
438 1.1 cgd struct stat sbuf;
439 1.44 christos ssize_t nread;
440 1.44 christos int pdes[2];
441 1.44 christos int status;
442 1.7 glass char buf[1024];
443 1.1 cgd
444 1.1 cgd if (!doall)
445 1.1 cgd return;
446 1.1 cgd
447 1.1 cgd (void)rewind(fp);
448 1.44 christos if (fstat(fileno(fp), &sbuf) == -1 || sbuf.st_size == 0)
449 1.1 cgd goto done;
450 1.44 christos if (pipe(pdes) == -1)
451 1.1 cgd goto done;
452 1.44 christos
453 1.26 christos switch (fork()) {
454 1.44 christos case -1:
455 1.44 christos /* error */
456 1.1 cgd (void)close(pdes[0]);
457 1.1 cgd (void)close(pdes[1]);
458 1.44 christos break;
459 1.10 thorpej case 0:
460 1.1 cgd /* child -- set stdin to pipe output */
461 1.1 cgd if (pdes[0] != STDIN_FILENO) {
462 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
463 1.1 cgd (void)close(pdes[0]);
464 1.1 cgd }
465 1.1 cgd (void)close(pdes[1]);
466 1.26 christos (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
467 1.1 cgd "\"Reminder Service\"", "-f", "root", NULL);
468 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_SENDMAIL);
469 1.26 christos /*NOTREACHED*/
470 1.44 christos default:
471 1.44 christos /* parent -- write to pipe input */
472 1.44 christos (void)close(pdes[0]);
473 1.44 christos
474 1.44 christos header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
475 1.44 christos header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
476 1.44 christos (void)writev(pdes[1], header, 7);
477 1.44 christos while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
478 1.44 christos (void)write(pdes[1], buf, (size_t)nread);
479 1.44 christos (void)close(pdes[1]);
480 1.44 christos break;
481 1.1 cgd }
482 1.26 christos
483 1.1 cgd done: (void)fclose(fp);
484 1.1 cgd (void)unlink(path);
485 1.44 christos while (wait(&status) != -1)
486 1.26 christos continue;
487 1.1 cgd }
488 1.1 cgd
489 1.26 christos static int
490 1.44 christos getmonth(char *s)
491 1.1 cgd {
492 1.44 christos const char **p;
493 1.1 cgd
494 1.1 cgd for (p = months; *p; ++p)
495 1.44 christos if (strncasecmp(s, *p, 3) == 0)
496 1.44 christos return (int)(p - months) + 1;
497 1.44 christos return 0;
498 1.1 cgd }
499 1.1 cgd
500 1.26 christos static int
501 1.44 christos getday(char *s)
502 1.1 cgd {
503 1.44 christos const char **p;
504 1.1 cgd
505 1.1 cgd for (p = days; *p; ++p)
506 1.44 christos if (strncasecmp(s, *p, 3) == 0)
507 1.44 christos return (int)(p - days) + 1;
508 1.44 christos return 0;
509 1.1 cgd }
510 1.1 cgd
511 1.26 christos static void
512 1.44 christos atodays(int ch, char *arg, unsigned short *rvp)
513 1.10 thorpej {
514 1.10 thorpej int u;
515 1.10 thorpej
516 1.44 christos u = atoi(arg);
517 1.44 christos if (u < 0 || u > 366)
518 1.26 christos warnx("-%c %d out of range 0-366, ignored.", ch, u);
519 1.44 christos else
520 1.44 christos *rvp = u;
521 1.10 thorpej }
522 1.10 thorpej
523 1.10 thorpej #define todigit(x) ((x) - '0')
524 1.10 thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
525 1.18 christos #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
526 1.10 thorpej
527 1.26 christos static void
528 1.44 christos getmmdd(struct tm *ptm, char *ds)
529 1.10 thorpej {
530 1.44 christos bool ok = false;
531 1.10 thorpej struct tm ttm;
532 1.10 thorpej
533 1.44 christos ttm = *ptm;
534 1.10 thorpej ttm.tm_isdst = -1;
535 1.10 thorpej
536 1.10 thorpej if (ISDIG2(ds)) {
537 1.10 thorpej ttm.tm_mon = ATOI2(ds) - 1;
538 1.10 thorpej ds += 2;
539 1.10 thorpej }
540 1.10 thorpej if (ISDIG2(ds)) {
541 1.10 thorpej ttm.tm_mday = ATOI2(ds);
542 1.10 thorpej ds += 2;
543 1.44 christos ok = true;
544 1.10 thorpej }
545 1.10 thorpej if (ok) {
546 1.10 thorpej if (ISDIG2(ds) && ISDIG2(ds + 2)) {
547 1.12 christos ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
548 1.10 thorpej ds += 2;
549 1.10 thorpej ttm.tm_year += ATOI2(ds);
550 1.10 thorpej } else if (ISDIG2(ds)) {
551 1.10 thorpej ttm.tm_year = ATOI2(ds);
552 1.12 christos if (ttm.tm_year < 69)
553 1.12 christos ttm.tm_year += 2000 - TM_YEAR_BASE;
554 1.12 christos else
555 1.12 christos ttm.tm_year += 1900 - TM_YEAR_BASE;
556 1.10 thorpej }
557 1.10 thorpej }
558 1.44 christos if (ok && mktime(&ttm) == -1)
559 1.44 christos ok = false;
560 1.44 christos
561 1.44 christos if (ok)
562 1.44 christos *ptm = ttm;
563 1.44 christos else {
564 1.26 christos warnx("Can't convert `%s' to date, ignored.", ds);
565 1.10 thorpej usage();
566 1.10 thorpej }
567 1.10 thorpej }
568 1.10 thorpej
569 1.44 christos __dead
570 1.26 christos static void
571 1.26 christos usage(void)
572 1.1 cgd {
573 1.34 wiz (void)fprintf(stderr, "usage: %s [-ax] [-d MMDD[[YY]YY]"
574 1.26 christos " [-f fname] [-l days] [-w days]\n", getprogname());
575 1.1 cgd exit(1);
576 1.1 cgd }
577