calendar.c revision 1.43 1 1.43 christos /* $NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos 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.11 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
35 1.11 lukem The Regents of the University of California. All rights reserved.\n");
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.43 christos __RCSID("$NetBSD: calendar.c,v 1.43 2008/03/06 17:37:57 christos 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.7 glass #include <stdio.h>
57 1.7 glass #include <stdlib.h>
58 1.7 glass #include <string.h>
59 1.13 kleink #include <time.h>
60 1.1 cgd #include <tzfile.h>
61 1.1 cgd #include <unistd.h>
62 1.7 glass
63 1.1 cgd #include "pathnames.h"
64 1.1 cgd
65 1.10 thorpej #ifndef TRUE
66 1.10 thorpej #define TRUE 1
67 1.10 thorpej #endif
68 1.10 thorpej #ifndef FALSE
69 1.10 thorpej #define FALSE 0
70 1.10 thorpej #endif
71 1.10 thorpej
72 1.26 christos static unsigned short lookahead = 1, weekend = 2;
73 1.31 jwise static char *fname = NULL, *datestr = NULL;
74 1.36 jwise static char *defaultnames[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR, NULL};
75 1.26 christos static struct passwd *pw;
76 1.26 christos static int doall;
77 1.26 christos static char path[MAXPATHLEN + 1];
78 1.33 jwise static int cpp_restricted = 0;
79 1.26 christos
80 1.26 christos /* 1-based month, 0-based days, cumulative */
81 1.26 christos static int daytab[][14] = {
82 1.26 christos { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
83 1.26 christos { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
84 1.26 christos };
85 1.26 christos static struct tm *tp;
86 1.26 christos static int *cumdays, offset, yrdays;
87 1.26 christos static char dayname[10];
88 1.26 christos
89 1.26 christos static struct iovec header[] = {
90 1.26 christos { "From: ", 6 },
91 1.26 christos { NULL, 0 },
92 1.26 christos { " (Reminder Service)\nTo: ", 24 },
93 1.26 christos { NULL, 0 },
94 1.26 christos { "\nSubject: ", 10 },
95 1.26 christos { NULL, 0 },
96 1.26 christos { "'s Calendar\nPrecedence: bulk\n\n", 30 },
97 1.26 christos };
98 1.26 christos
99 1.26 christos static char *days[] = {
100 1.26 christos "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
101 1.26 christos };
102 1.26 christos
103 1.26 christos static char *months[] = {
104 1.26 christos "jan", "feb", "mar", "apr", "may", "jun",
105 1.26 christos "jul", "aug", "sep", "oct", "nov", "dec", NULL,
106 1.26 christos };
107 1.26 christos
108 1.26 christos int main(int, char **);
109 1.26 christos
110 1.26 christos static void atodays(int, char *, unsigned short *);
111 1.26 christos static void cal(void);
112 1.26 christos static void closecal(FILE *);
113 1.26 christos static int getday(char *);
114 1.26 christos static int getfield(char *, char **, int *);
115 1.26 christos static void getmmdd(struct tm *, char *);
116 1.26 christos static int getmonth(char *);
117 1.26 christos static int isnow(char *);
118 1.26 christos static FILE *opencal(void);
119 1.26 christos static void settime(void);
120 1.42 perry static void usage(void) __dead;
121 1.7 glass
122 1.7 glass int
123 1.1 cgd main(argc, argv)
124 1.1 cgd int argc;
125 1.7 glass char *argv[];
126 1.1 cgd {
127 1.1 cgd int ch;
128 1.15 mycroft const char *caldir;
129 1.1 cgd
130 1.34 wiz while ((ch = getopt(argc, argv, "-ad:f:l:w:x")) != -1)
131 1.7 glass switch (ch) {
132 1.1 cgd case '-': /* backward contemptible */
133 1.1 cgd case 'a':
134 1.1 cgd if (getuid()) {
135 1.7 glass errno = EPERM;
136 1.21 drochner err(1, NULL);
137 1.1 cgd }
138 1.1 cgd doall = 1;
139 1.1 cgd break;
140 1.10 thorpej case 'd':
141 1.10 thorpej datestr = optarg;
142 1.10 thorpej break;
143 1.10 thorpej case 'f':
144 1.10 thorpej fname = optarg;
145 1.10 thorpej break;
146 1.10 thorpej case 'l':
147 1.10 thorpej atodays(ch, optarg, &lookahead);
148 1.10 thorpej break;
149 1.10 thorpej case 'w':
150 1.10 thorpej atodays(ch, optarg, &weekend);
151 1.10 thorpej break;
152 1.33 jwise case 'x':
153 1.33 jwise cpp_restricted = 1;
154 1.38 wiz break;
155 1.1 cgd case '?':
156 1.1 cgd default:
157 1.1 cgd usage();
158 1.1 cgd }
159 1.1 cgd argc -= optind;
160 1.1 cgd argv += optind;
161 1.1 cgd
162 1.1 cgd if (argc)
163 1.1 cgd usage();
164 1.1 cgd
165 1.1 cgd settime();
166 1.26 christos if (doall) {
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.1 cgd if (!chdir(pw->pw_dir))
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.26 christos if(!chdir(caldir))
176 1.26 christos cal();
177 1.26 christos } else {
178 1.32 jwise if (((pw = getpwuid(geteuid())) != NULL) && !chdir(pw->pw_dir))
179 1.32 jwise cal();
180 1.26 christos }
181 1.1 cgd exit(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.37 lukem int printing = 0;
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.29 briggs while ((line = fparseln(stdin, NULL, NULL, NULL, FPARSELN_UNESCCOMM))
194 1.29 briggs != NULL) {
195 1.26 christos if (line[0] == '\0')
196 1.1 cgd continue;
197 1.26 christos if (line[0] != '\t')
198 1.26 christos printing = isnow(line) ? 1 : 0;
199 1.1 cgd if (printing)
200 1.26 christos (void)fprintf(fp, "%s\n", line);
201 1.26 christos free(line);
202 1.26 christos
203 1.1 cgd }
204 1.1 cgd closecal(fp);
205 1.1 cgd }
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.10 thorpej if (datestr) {
216 1.10 thorpej getmmdd(tp, datestr);
217 1.10 thorpej }
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.26 christos static int
242 1.1 cgd isnow(endp)
243 1.1 cgd char *endp;
244 1.1 cgd {
245 1.1 cgd int day, flags, month, v1, v2;
246 1.1 cgd
247 1.1 cgd #define F_ISMONTH 0x01
248 1.1 cgd #define F_ISDAY 0x02
249 1.35 jwise #define F_WILDMONTH 0x04
250 1.35 jwise #define F_WILDDAY 0x08
251 1.35 jwise
252 1.1 cgd flags = 0;
253 1.35 jwise
254 1.1 cgd /* didn't recognize anything, skip it */
255 1.1 cgd if (!(v1 = getfield(endp, &endp, &flags)))
256 1.7 glass return (0);
257 1.7 glass if (flags & F_ISDAY || v1 > 12) {
258 1.1 cgd /* found a day */
259 1.1 cgd day = v1;
260 1.43 christos /* if no recognizable month, assume wildcard ('*') month */
261 1.43 christos if (!(month = getfield(endp, &endp, &flags))) {
262 1.43 christos flags |= F_ISMONTH | F_WILDMONTH;
263 1.43 christos month = tp->tm_mon + 1;
264 1.43 christos }
265 1.7 glass } else if (flags & F_ISMONTH) {
266 1.1 cgd month = v1;
267 1.1 cgd /* if no recognizable day, assume the first */
268 1.1 cgd if (!(day = getfield(endp, &endp, &flags)))
269 1.1 cgd day = 1;
270 1.1 cgd } else {
271 1.1 cgd v2 = getfield(endp, &endp, &flags);
272 1.7 glass if (flags & F_ISMONTH) {
273 1.1 cgd day = v1;
274 1.1 cgd month = v2;
275 1.1 cgd } else {
276 1.1 cgd /* F_ISDAY set, v2 > 12, or no way to tell */
277 1.1 cgd month = v1;
278 1.1 cgd /* if no recognizable day, assume the first */
279 1.1 cgd day = v2 ? v2 : 1;
280 1.1 cgd }
281 1.1 cgd }
282 1.35 jwise
283 1.39 jld if ((flags & F_WILDMONTH) && (flags & F_WILDDAY))
284 1.35 jwise return (1);
285 1.35 jwise
286 1.40 elad if ((flags & F_WILDMONTH) && (flags & F_ISDAY) && (day == tp->tm_mday))
287 1.35 jwise return (1);
288 1.35 jwise
289 1.39 jld if (((flags & F_ISMONTH) && (flags & F_WILDDAY)) && (month == tp->tm_mon + 1))
290 1.35 jwise return (1);
291 1.35 jwise
292 1.7 glass if (flags & F_ISDAY)
293 1.4 cgd day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
294 1.1 cgd day = cumdays[month] + day;
295 1.1 cgd
296 1.1 cgd /* if today or today + offset days */
297 1.1 cgd if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
298 1.7 glass return (1);
299 1.1 cgd /* if number of days left in this year + days to event in next year */
300 1.1 cgd if (yrdays - tp->tm_yday + day <= offset)
301 1.7 glass return (1);
302 1.7 glass return (0);
303 1.1 cgd }
304 1.1 cgd
305 1.26 christos static int
306 1.1 cgd getfield(p, endp, flags)
307 1.1 cgd char *p, **endp;
308 1.1 cgd int *flags;
309 1.1 cgd {
310 1.1 cgd int val;
311 1.1 cgd char *start, savech;
312 1.1 cgd
313 1.18 christos #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
314 1.18 christos !isalpha((unsigned char)*p) && *p != '*')
315 1.18 christos
316 1.18 christos for (; FLDCHAR(*p); ++p)
317 1.26 christos continue;
318 1.1 cgd if (*p == '*') { /* `*' is current month */
319 1.35 jwise if (!(*flags & F_ISMONTH)) {
320 1.35 jwise *flags |= F_ISMONTH|F_WILDMONTH;
321 1.35 jwise *endp = p+1;
322 1.35 jwise return (tp->tm_mon + 1);
323 1.35 jwise } else {
324 1.35 jwise *flags |= F_ISDAY|F_WILDDAY;
325 1.35 jwise *endp = p+1;
326 1.35 jwise return (1);
327 1.35 jwise }
328 1.1 cgd }
329 1.18 christos if (isdigit((unsigned char)*p)) {
330 1.1 cgd val = strtol(p, &p, 10); /* if 0, it's failure */
331 1.18 christos for (; FLDCHAR(*p); ++p)
332 1.26 christos continue;
333 1.1 cgd *endp = p;
334 1.7 glass return (val);
335 1.1 cgd }
336 1.18 christos for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
337 1.26 christos continue;
338 1.1 cgd savech = *p;
339 1.1 cgd *p = '\0';
340 1.26 christos if ((val = getmonth(start)) != 0) {
341 1.1 cgd *flags |= F_ISMONTH;
342 1.26 christos } else if ((val = getday(start)) != 0) {
343 1.1 cgd *flags |= F_ISDAY;
344 1.26 christos } else {
345 1.4 cgd *p = savech;
346 1.7 glass return (0);
347 1.4 cgd }
348 1.18 christos for (*p = savech; FLDCHAR(*p); ++p)
349 1.26 christos continue;
350 1.1 cgd *endp = p;
351 1.7 glass return (val);
352 1.1 cgd }
353 1.1 cgd
354 1.26 christos static FILE *
355 1.26 christos opencal(void)
356 1.1 cgd {
357 1.1 cgd int fd, pdes[2];
358 1.31 jwise char **name;
359 1.1 cgd
360 1.1 cgd /* open up calendar file as stdin */
361 1.31 jwise if (fname == NULL) {
362 1.31 jwise for (name = defaultnames; *name != NULL; name++) {
363 1.31 jwise if (!freopen(*name, "rf", stdin))
364 1.31 jwise continue;
365 1.31 jwise else
366 1.31 jwise break;
367 1.31 jwise }
368 1.31 jwise if (*name == NULL) {
369 1.31 jwise if (doall)
370 1.31 jwise return (NULL);
371 1.31 jwise err(1, "Cannot open calendar file");
372 1.31 jwise }
373 1.31 jwise } else if (!freopen(fname, "rf", stdin)) {
374 1.1 cgd if (doall)
375 1.7 glass return (NULL);
376 1.23 christos err(1, "Cannot open `%s'", fname);
377 1.1 cgd }
378 1.31 jwise
379 1.26 christos if (pipe(pdes) < 0) {
380 1.26 christos warn("Cannot open pipe");
381 1.7 glass return (NULL);
382 1.26 christos }
383 1.31 jwise
384 1.26 christos switch (fork()) {
385 1.1 cgd case -1: /* error */
386 1.1 cgd (void)close(pdes[0]);
387 1.1 cgd (void)close(pdes[1]);
388 1.7 glass return (NULL);
389 1.1 cgd case 0:
390 1.1 cgd /* child -- stdin already setup, set stdout to pipe input */
391 1.1 cgd if (pdes[1] != STDOUT_FILENO) {
392 1.1 cgd (void)dup2(pdes[1], STDOUT_FILENO);
393 1.1 cgd (void)close(pdes[1]);
394 1.1 cgd }
395 1.1 cgd (void)close(pdes[0]);
396 1.33 jwise /* tell CPP to only open regular files */
397 1.33 jwise if(!cpp_restricted && setenv("CPP_RESTRICTED", "", 1))
398 1.33 jwise err(1, "Cannot restrict cpp");
399 1.33 jwise cpp_restricted = 1;
400 1.33 jwise
401 1.26 christos (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
402 1.26 christos "-I" _PATH_CALENDARS, NULL);
403 1.26 christos err(1, "Cannot exec `%s'", _PATH_CPP);
404 1.26 christos /*NOTREACHED*/
405 1.1 cgd }
406 1.31 jwise
407 1.1 cgd /* parent -- set stdin to pipe output */
408 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
409 1.1 cgd (void)close(pdes[0]);
410 1.1 cgd (void)close(pdes[1]);
411 1.1 cgd
412 1.1 cgd /* not reading all calendar files, just set output to stdout */
413 1.1 cgd if (!doall)
414 1.7 glass return (stdout);
415 1.1 cgd
416 1.1 cgd /* set output to a temporary file, so if no output don't send mail */
417 1.7 glass (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
418 1.26 christos if ((fd = mkstemp(path)) < 0) {
419 1.26 christos warn("Cannot create temporary file");
420 1.7 glass return (NULL);
421 1.26 christos }
422 1.7 glass return (fdopen(fd, "w+"));
423 1.1 cgd }
424 1.1 cgd
425 1.26 christos static void
426 1.1 cgd closecal(fp)
427 1.1 cgd FILE *fp;
428 1.1 cgd {
429 1.1 cgd struct stat sbuf;
430 1.1 cgd int nread, pdes[2], status;
431 1.7 glass char buf[1024];
432 1.1 cgd
433 1.1 cgd if (!doall)
434 1.1 cgd return;
435 1.1 cgd
436 1.1 cgd (void)rewind(fp);
437 1.1 cgd if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
438 1.1 cgd goto done;
439 1.10 thorpej if (pipe(pdes) < 0)
440 1.1 cgd goto done;
441 1.26 christos switch (fork()) {
442 1.1 cgd case -1: /* error */
443 1.1 cgd (void)close(pdes[0]);
444 1.1 cgd (void)close(pdes[1]);
445 1.1 cgd goto done;
446 1.10 thorpej case 0:
447 1.1 cgd /* child -- set stdin to pipe output */
448 1.1 cgd if (pdes[0] != STDIN_FILENO) {
449 1.1 cgd (void)dup2(pdes[0], STDIN_FILENO);
450 1.1 cgd (void)close(pdes[0]);
451 1.1 cgd }
452 1.1 cgd (void)close(pdes[1]);
453 1.26 christos (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
454 1.1 cgd "\"Reminder Service\"", "-f", "root", NULL);
455 1.26 christos err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
456 1.26 christos /*NOTREACHED*/
457 1.1 cgd }
458 1.1 cgd /* parent -- write to pipe input */
459 1.1 cgd (void)close(pdes[0]);
460 1.1 cgd
461 1.14 mycroft header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
462 1.1 cgd header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
463 1.1 cgd writev(pdes[1], header, 7);
464 1.1 cgd while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
465 1.1 cgd (void)write(pdes[1], buf, nread);
466 1.1 cgd (void)close(pdes[1]);
467 1.26 christos
468 1.1 cgd done: (void)fclose(fp);
469 1.1 cgd (void)unlink(path);
470 1.26 christos while (wait(&status) >= 0)
471 1.26 christos continue;
472 1.1 cgd }
473 1.1 cgd
474 1.26 christos static int
475 1.1 cgd getmonth(s)
476 1.11 lukem char *s;
477 1.1 cgd {
478 1.11 lukem char **p;
479 1.1 cgd
480 1.1 cgd for (p = months; *p; ++p)
481 1.1 cgd if (!strncasecmp(s, *p, 3))
482 1.7 glass return ((p - months) + 1);
483 1.7 glass return (0);
484 1.1 cgd }
485 1.1 cgd
486 1.26 christos static int
487 1.1 cgd getday(s)
488 1.11 lukem char *s;
489 1.1 cgd {
490 1.11 lukem char **p;
491 1.1 cgd
492 1.1 cgd for (p = days; *p; ++p)
493 1.1 cgd if (!strncasecmp(s, *p, 3))
494 1.7 glass return ((p - days) + 1);
495 1.7 glass return (0);
496 1.1 cgd }
497 1.1 cgd
498 1.26 christos static void
499 1.26 christos atodays(int ch, char *optarg, unsigned short *days)
500 1.10 thorpej {
501 1.10 thorpej int u;
502 1.10 thorpej
503 1.10 thorpej u = atoi(optarg);
504 1.10 thorpej if ((u < 0) || (u > 366)) {
505 1.26 christos warnx("-%c %d out of range 0-366, ignored.", ch, u);
506 1.10 thorpej } else {
507 1.10 thorpej *days = u;
508 1.10 thorpej }
509 1.10 thorpej }
510 1.10 thorpej
511 1.10 thorpej #define todigit(x) ((x) - '0')
512 1.10 thorpej #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
513 1.18 christos #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
514 1.10 thorpej
515 1.26 christos static void
516 1.10 thorpej getmmdd(struct tm *tp, char *ds)
517 1.10 thorpej {
518 1.10 thorpej int ok = FALSE;
519 1.10 thorpej struct tm ttm;
520 1.10 thorpej
521 1.10 thorpej ttm = *tp;
522 1.10 thorpej ttm.tm_isdst = -1;
523 1.10 thorpej
524 1.10 thorpej if (ISDIG2(ds)) {
525 1.10 thorpej ttm.tm_mon = ATOI2(ds) - 1;
526 1.10 thorpej ds += 2;
527 1.10 thorpej }
528 1.10 thorpej
529 1.10 thorpej if (ISDIG2(ds)) {
530 1.10 thorpej ttm.tm_mday = ATOI2(ds);
531 1.10 thorpej ds += 2;
532 1.10 thorpej
533 1.10 thorpej ok = TRUE;
534 1.10 thorpej }
535 1.10 thorpej
536 1.10 thorpej if (ok) {
537 1.10 thorpej if (ISDIG2(ds) && ISDIG2(ds + 2)) {
538 1.12 christos ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
539 1.10 thorpej ds += 2;
540 1.10 thorpej ttm.tm_year += ATOI2(ds);
541 1.10 thorpej } else if (ISDIG2(ds)) {
542 1.10 thorpej ttm.tm_year = ATOI2(ds);
543 1.12 christos if (ttm.tm_year < 69)
544 1.12 christos ttm.tm_year += 2000 - TM_YEAR_BASE;
545 1.12 christos else
546 1.12 christos ttm.tm_year += 1900 - TM_YEAR_BASE;
547 1.10 thorpej }
548 1.10 thorpej }
549 1.10 thorpej
550 1.10 thorpej if (ok && (mktime(&ttm) < 0)) {
551 1.10 thorpej ok = FALSE;
552 1.10 thorpej }
553 1.10 thorpej
554 1.10 thorpej if (ok) {
555 1.10 thorpej *tp = ttm;
556 1.10 thorpej } else {
557 1.26 christos warnx("Can't convert `%s' to date, ignored.", ds);
558 1.10 thorpej usage();
559 1.10 thorpej }
560 1.10 thorpej }
561 1.10 thorpej
562 1.26 christos static void
563 1.26 christos usage(void)
564 1.1 cgd {
565 1.34 wiz (void)fprintf(stderr, "usage: %s [-ax] [-d MMDD[[YY]YY]"
566 1.26 christos " [-f fname] [-l days] [-w days]\n", getprogname());
567 1.1 cgd exit(1);
568 1.1 cgd }
569