calendar.c revision 1.47 1 1.47 dholland /* $NetBSD: calendar.c,v 1.47 2008/09/30 05:51:41 dholland 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.47 dholland __RCSID("$NetBSD: calendar.c,v 1.47 2008/09/30 05:51:41 dholland 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.47 dholland /* if month is out of range, treat it as '*' */
287 1.47 dholland if (month < 1 || month > 12) {
288 1.47 dholland flags |= F_ISMONTH | F_WILDMONTH;
289 1.47 dholland month = tp->tm_mon + 1;
290 1.47 dholland }
291 1.47 dholland
292 1.44 christos if (flags & F_WILDMONTH && flags & F_WILDDAY)
293 1.44 christos return true;
294 1.35 jwise
295 1.44 christos if (flags & F_WILDMONTH && flags & F_ISDAY && day == tp->tm_mday)
296 1.44 christos return true;
297 1.35 jwise
298 1.44 christos if (flags & F_ISMONTH && flags & F_WILDDAY && month == tp->tm_mon + 1)
299 1.44 christos return true;
300 1.35 jwise
301 1.7 glass if (flags & F_ISDAY)
302 1.4 cgd day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
303 1.1 cgd day = cumdays[month] + day;
304 1.1 cgd
305 1.1 cgd /* if today or today + offset days */
306 1.1 cgd if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
307 1.44 christos return true;
308 1.44 christos
309 1.1 cgd /* if number of days left in this year + days to event in next year */
310 1.1 cgd if (yrdays - tp->tm_yday + day <= offset)
311 1.44 christos return true;
312 1.44 christos
313 1.44 christos return false;
314 1.1 cgd }
315 1.1 cgd
316 1.26 christos static int
317 1.44 christos getfield(char *p, char **endp, int *flags)
318 1.1 cgd {
319 1.1 cgd int val;
320 1.44 christos char *start;
321 1.44 christos char savech;
322 1.1 cgd
323 1.18 christos #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
324 1.18 christos !isalpha((unsigned char)*p) && *p != '*')
325 1.18 christos
326 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
327 1.26 christos continue;
328 1.1 cgd if (*p == '*') { /* `*' is current month */
329 1.35 jwise if (!(*flags & F_ISMONTH)) {
330 1.44 christos *flags |= F_ISMONTH | F_WILDMONTH;
331 1.44 christos *endp = p + 1;
332 1.44 christos return tp->tm_mon + 1;
333 1.35 jwise } else {
334 1.44 christos *flags |= F_ISDAY | F_WILDDAY;
335 1.44 christos *endp = p + 1;
336 1.44 christos return 1;
337 1.35 jwise }
338 1.1 cgd }
339 1.18 christos if (isdigit((unsigned char)*p)) {
340 1.44 christos val = (int)strtol(p, &p, 10); /* if 0, it's failure */
341 1.44 christos for (/*EMPTY*/; FLDCHAR(*p); ++p)
342 1.26 christos continue;
343 1.1 cgd *endp = p;
344 1.44 christos return val;
345 1.1 cgd }
346 1.44 christos for (start = p; *p != '\0' && isalpha((unsigned char)*++p); /*EMPTY*/)
347 1.26 christos continue;
348 1.1 cgd savech = *p;
349 1.1 cgd *p = '\0';
350 1.44 christos if ((val = getmonth(start)) != 0)
351 1.1 cgd *flags |= F_ISMONTH;
352 1.44 christos else if ((val = getday(start)) != 0)
353 1.1 cgd *flags |= F_ISDAY;
354 1.44 christos else {
355 1.4 cgd *p = savech;
356 1.44 christos return 0;
357 1.4 cgd }
358 1.18 christos for (*p = savech; FLDCHAR(*p); ++p)
359 1.26 christos continue;
360 1.1 cgd *endp = p;
361 1.44 christos return val;
362 1.1 cgd }
363 1.1 cgd
364 1.26 christos static FILE *
365 1.26 christos opencal(void)
366 1.1 cgd {
367 1.44 christos int fd;
368 1.44 christos int pdes[2];
369 1.44 christos const char **name;
370 1.1 cgd
371 1.1 cgd /* open up calendar file as stdin */
372 1.31 jwise if (fname == NULL) {
373 1.31 jwise for (name = defaultnames; *name != NULL; name++) {
374 1.44 christos if (freopen(*name, "rf", stdin) == NULL)
375 1.31 jwise continue;
376 1.31 jwise else
377 1.31 jwise break;
378 1.31 jwise }
379 1.31 jwise if (*name == NULL) {
380 1.31 jwise if (doall)
381 1.44 christos return NULL;
382 1.44 christos err(EXIT_FAILURE, "Cannot open calendar file");
383 1.31 jwise }
384 1.44 christos } else if (freopen(fname, "rf", stdin) == NULL) {
385 1.1 cgd if (doall)
386 1.44 christos return NULL;
387 1.44 christos err(EXIT_FAILURE, "Cannot open `%s'", fname);
388 1.1 cgd }
389 1.31 jwise
390 1.44 christos if (pipe(pdes) == -1) {
391 1.26 christos warn("Cannot open pipe");
392 1.44 christos return NULL;
393 1.26 christos }
394 1.31 jwise
395 1.26 christos switch (fork()) {
396 1.44 christos case -1:
397 1.44 christos /* error */
398 1.1 cgd (void)close(pdes[0]);
399 1.1 cgd (void)close(pdes[1]);
400 1.44 christos return NULL;
401 1.1 cgd case 0:
402 1.1 cgd /* child -- stdin already setup, set stdout to pipe input */
403 1.1 cgd if (pdes[1] != STDOUT_FILENO) {
404 1.1 cgd (void)dup2(pdes[1], STDOUT_FILENO);
405 1.1 cgd (void)close(pdes[1]);
406 1.1 cgd }
407 1.1 cgd (void)close(pdes[0]);
408 1.33 jwise /* tell CPP to only open regular files */
409 1.44 christos if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1) == -1)
410 1.44 christos err(EXIT_FAILURE, "Cannot restrict cpp");
411 1.44 christos cpp_restricted = true;
412 1.33 jwise
413 1.26 christos (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
414 1.26 christos "-I" _PATH_CALENDARS, NULL);
415 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_CPP);
416 1.26 christos /*NOTREACHED*/
417 1.44 christos default:
418 1.44 christos /* parent -- set stdin to pipe output */
419 1.44 christos (void)dup2(pdes[0], STDIN_FILENO);
420 1.44 christos (void)close(pdes[0]);
421 1.44 christos (void)close(pdes[1]);
422 1.31 jwise
423 1.44 christos /* not reading all calendar files, just set output to stdout */
424 1.44 christos if (!doall)
425 1.44 christos return stdout;
426 1.44 christos
427 1.44 christos /*
428 1.44 christos * Set output to a temporary file, so if no output
429 1.44 christos * don't send mail.
430 1.44 christos */
431 1.44 christos (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
432 1.44 christos if ((fd = mkstemp(path)) == -1) {
433 1.44 christos warn("Cannot create temporary file");
434 1.44 christos return NULL;
435 1.44 christos }
436 1.44 christos return fdopen(fd, "w+");
437 1.26 christos }
438 1.44 christos /*NOTREACHED*/
439 1.1 cgd }
440 1.1 cgd
441 1.26 christos static void
442 1.44 christos closecal(FILE *fp)
443 1.1 cgd {
444 1.1 cgd struct stat sbuf;
445 1.44 christos ssize_t nread;
446 1.44 christos int pdes[2];
447 1.44 christos int status;
448 1.7 glass char buf[1024];
449 1.1 cgd
450 1.1 cgd if (!doall)
451 1.1 cgd return;
452 1.1 cgd
453 1.1 cgd (void)rewind(fp);
454 1.44 christos if (fstat(fileno(fp), &sbuf) == -1 || sbuf.st_size == 0)
455 1.1 cgd goto done;
456 1.44 christos if (pipe(pdes) == -1)
457 1.1 cgd goto done;
458 1.44 christos
459 1.26 christos switch (fork()) {
460 1.44 christos case -1:
461 1.44 christos /* error */
462 1.1 cgd (void)close(pdes[0]);
463 1.1 cgd (void)close(pdes[1]);
464 1.44 christos break;
465 1.10 thorpej case 0:
466 1.1 cgd /* child -- set stdin to pipe output */
467 1.1 cgd if (pdes[0] != STDIN_FILENO) {
468 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
469 1.1 cgd (void)close(pdes[0]);
470 1.1 cgd }
471 1.1 cgd (void)close(pdes[1]);
472 1.26 christos (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
473 1.1 cgd "\"Reminder Service\"", "-f", "root", NULL);
474 1.44 christos err(EXIT_FAILURE, "Cannot exec `%s'", _PATH_SENDMAIL);
475 1.26 christos /*NOTREACHED*/
476 1.44 christos default:
477 1.44 christos /* parent -- write to pipe input */
478 1.44 christos (void)close(pdes[0]);
479 1.44 christos
480 1.44 christos header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
481 1.44 christos header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
482 1.44 christos (void)writev(pdes[1], header, 7);
483 1.44 christos while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
484 1.44 christos (void)write(pdes[1], buf, (size_t)nread);
485 1.44 christos (void)close(pdes[1]);
486 1.44 christos break;
487 1.1 cgd }
488 1.26 christos
489 1.1 cgd done: (void)fclose(fp);
490 1.1 cgd (void)unlink(path);
491 1.44 christos while (wait(&status) != -1)
492 1.26 christos continue;
493 1.1 cgd }
494 1.1 cgd
495 1.26 christos static int
496 1.44 christos getmonth(char *s)
497 1.1 cgd {
498 1.44 christos const char **p;
499 1.1 cgd
500 1.1 cgd for (p = months; *p; ++p)
501 1.44 christos if (strncasecmp(s, *p, 3) == 0)
502 1.44 christos return (int)(p - months) + 1;
503 1.44 christos return 0;
504 1.1 cgd }
505 1.1 cgd
506 1.26 christos static int
507 1.44 christos getday(char *s)
508 1.1 cgd {
509 1.44 christos const char **p;
510 1.1 cgd
511 1.1 cgd for (p = days; *p; ++p)
512 1.44 christos if (strncasecmp(s, *p, 3) == 0)
513 1.44 christos return (int)(p - days) + 1;
514 1.44 christos return 0;
515 1.1 cgd }
516 1.1 cgd
517 1.26 christos static void
518 1.44 christos atodays(int ch, char *arg, unsigned short *rvp)
519 1.10 thorpej {
520 1.10 thorpej int u;
521 1.10 thorpej
522 1.44 christos u = atoi(arg);
523 1.44 christos if (u < 0 || u > 366)
524 1.26 christos warnx("-%c %d out of range 0-366, ignored.", ch, u);
525 1.44 christos else
526 1.44 christos *rvp = u;
527 1.10 thorpej }
528 1.10 thorpej
529 1.10 thorpej #define todigit(x) ((x) - '0')
530 1.10 thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
531 1.18 christos #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
532 1.10 thorpej
533 1.26 christos static void
534 1.44 christos getmmdd(struct tm *ptm, char *ds)
535 1.10 thorpej {
536 1.44 christos bool ok = false;
537 1.10 thorpej struct tm ttm;
538 1.10 thorpej
539 1.44 christos ttm = *ptm;
540 1.10 thorpej ttm.tm_isdst = -1;
541 1.10 thorpej
542 1.10 thorpej if (ISDIG2(ds)) {
543 1.10 thorpej ttm.tm_mon = ATOI2(ds) - 1;
544 1.10 thorpej ds += 2;
545 1.10 thorpej }
546 1.10 thorpej if (ISDIG2(ds)) {
547 1.10 thorpej ttm.tm_mday = ATOI2(ds);
548 1.10 thorpej ds += 2;
549 1.44 christos ok = true;
550 1.10 thorpej }
551 1.10 thorpej if (ok) {
552 1.10 thorpej if (ISDIG2(ds) && ISDIG2(ds + 2)) {
553 1.12 christos ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
554 1.10 thorpej ds += 2;
555 1.10 thorpej ttm.tm_year += ATOI2(ds);
556 1.10 thorpej } else if (ISDIG2(ds)) {
557 1.10 thorpej ttm.tm_year = ATOI2(ds);
558 1.12 christos if (ttm.tm_year < 69)
559 1.12 christos ttm.tm_year += 2000 - TM_YEAR_BASE;
560 1.12 christos else
561 1.12 christos ttm.tm_year += 1900 - TM_YEAR_BASE;
562 1.10 thorpej }
563 1.10 thorpej }
564 1.44 christos if (ok && mktime(&ttm) == -1)
565 1.44 christos ok = false;
566 1.44 christos
567 1.44 christos if (ok)
568 1.44 christos *ptm = ttm;
569 1.44 christos else {
570 1.26 christos warnx("Can't convert `%s' to date, ignored.", ds);
571 1.10 thorpej usage();
572 1.10 thorpej }
573 1.10 thorpej }
574 1.10 thorpej
575 1.44 christos __dead
576 1.26 christos static void
577 1.26 christos usage(void)
578 1.1 cgd {
579 1.34 wiz (void)fprintf(stderr, "usage: %s [-ax] [-d MMDD[[YY]YY]"
580 1.26 christos " [-f fname] [-l days] [-w days]\n", getprogname());
581 1.1 cgd exit(1);
582 1.1 cgd }
583