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