Annotation of src/usr.bin/last/last.c, Revision 1.33
1.33 ! deraadt 1: /* $OpenBSD: last.c,v 1.32 2005/07/01 02:10:24 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: last.c,v 1.6 1994/12/24 16:49:02 cgd Exp $ */
3:
4: /*
5: * Copyright (c) 1987, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.26 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: static char copyright[] =
35: "@(#) Copyright (c) 1987, 1993, 1994\n\
36: The Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
42: #endif
1.33 ! deraadt 43: static char rcsid[] = "$OpenBSD: last.c,v 1.32 2005/07/01 02:10:24 millert Exp $";
1.1 deraadt 44: #endif /* not lint */
45:
46: #include <sys/param.h>
47: #include <sys/stat.h>
48:
49: #include <err.h>
50: #include <fcntl.h>
51: #include <paths.h>
52: #include <signal.h>
53: #include <stdio.h>
54: #include <stdlib.h>
55: #include <string.h>
56: #include <time.h>
57: #include <tzfile.h>
58: #include <unistd.h>
59: #include <utmp.h>
60:
61: #define NO 0 /* false/no */
62: #define YES 1 /* true/yes */
1.18 mickey 63: #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
1.1 deraadt 64:
65: static struct utmp buf[1024]; /* utmp read buffer */
66:
1.33 ! deraadt 67: struct arg {
1.1 deraadt 68: char *name; /* argument */
69: #define HOST_TYPE -2
70: #define TTY_TYPE -3
71: #define USER_TYPE -4
72: int type; /* type of arg */
1.33 ! deraadt 73: struct arg *next; /* linked list pointer */
! 74: } *arglist;
1.1 deraadt 75:
1.33 ! deraadt 76: struct ttytab {
1.1 deraadt 77: time_t logout; /* log out time */
78: char tty[UT_LINESIZE + 1]; /* terminal name */
1.33 ! deraadt 79: struct ttytab*next; /* linked list pointer */
! 80: } *ttylist;
1.1 deraadt 81:
82: static time_t currentout; /* current logout value */
1.33 ! deraadt 83: static long maxrec = -1; /* records to display */
1.1 deraadt 84: static char *file = _PATH_WTMP; /* wtmp file */
1.6 mickey 85: static int fulltime = 0; /* Display seconds? */
1.33 ! deraadt 86: static time_t snaptime = 0; /* report only at this time */
! 87: static int calculate = 0;
! 88: static int seconds = 0;
1.1 deraadt 89:
1.19 millert 90: void addarg(int, char *);
1.33 ! deraadt 91: struct ttytab *addtty(char *);
1.19 millert 92: void hostconv(char *);
93: void onintr(int);
94: char *ttyconv(char *);
95: time_t dateconv(char *);
96: int want(struct utmp *, int);
97: void wtmp(void);
98: void checkargs(void);
1.22 deraadt 99: void usage(void);
1.1 deraadt 100:
1.28 deraadt 101: #define NAME_WIDTH 9
1.14 fgsch 102: #define HOST_WIDTH 24
1.13 deraadt 103:
1.1 deraadt 104: int
1.27 deraadt 105: main(int argc, char *argv[])
1.1 deraadt 106: {
1.31 millert 107: const char *errstr;
1.33 ! deraadt 108: int ch, lastch = '\0', newarg = 1, prevoptind = 1;
1.1 deraadt 109:
1.32 millert 110: while ((ch = getopt(argc, argv, "0123456789cf:h:n:st:d:T")) != -1) {
1.1 deraadt 111: switch (ch) {
112: case '0': case '1': case '2': case '3': case '4':
113: case '5': case '6': case '7': case '8': case '9':
114: /*
115: * kludge: last was originally designed to take
116: * a number after a dash.
117: */
1.32 millert 118: if (newarg || !isdigit(lastch))
119: maxrec = 0;
120: else if (maxrec > INT_MAX / 10)
121: usage();
122: maxrec = (maxrec * 10) + (ch - '0');
1.1 deraadt 123: break;
1.9 downsj 124: case 'c':
125: calculate++;
126: break;
1.1 deraadt 127: case 'f':
128: file = optarg;
129: break;
130: case 'h':
131: hostconv(optarg);
132: addarg(HOST_TYPE, optarg);
133: break;
1.31 millert 134: case 'n':
135: maxrec = strtonum(optarg, 0, LONG_MAX, &errstr);
136: if (errstr != NULL)
137: errx(1, "number of lines is %s: %s", errstr,
138: optarg);
139: if (maxrec == 0)
140: exit(0);
141: break;
1.9 downsj 142: case 's':
143: seconds++;
144: break;
1.1 deraadt 145: case 't':
146: addarg(TTY_TYPE, ttyconv(optarg));
147: break;
1.4 jdm 148: case 'd':
149: snaptime = dateconv(optarg);
150: break;
1.6 mickey 151: case 'T':
1.9 downsj 152: fulltime = 1;
1.6 mickey 153: break;
1.1 deraadt 154: default:
1.22 deraadt 155: usage();
1.1 deraadt 156: }
1.32 millert 157: lastch = ch;
158: newarg = optind != prevoptind;
159: prevoptind = optind;
160: }
161: if (maxrec == 0)
162: exit(0);
1.1 deraadt 163:
164: if (argc) {
165: setlinebuf(stdout);
166: for (argv += optind; *argv; ++argv) {
167: #define COMPATIBILITY
168: #ifdef COMPATIBILITY
169: /* code to allow "last p5" to work */
170: addarg(TTY_TYPE, ttyconv(*argv));
171: #endif
172: addarg(USER_TYPE, *argv);
173: }
174: }
1.18 mickey 175:
1.5 jdm 176: checkargs();
1.1 deraadt 177: wtmp();
178: exit(0);
179: }
180:
181: /*
1.33 ! deraadt 182: * if snaptime is set, print warning if usernames, or -t or -h
! 183: * flags are also provided
1.5 jdm 184: */
185: void
1.27 deraadt 186: checkargs(void)
1.5 jdm 187: {
1.18 mickey 188: int ttyflag = 0;
1.33 ! deraadt 189: struct arg *step;
1.5 jdm 190:
1.33 ! deraadt 191: if (!snaptime || !arglist)
1.5 jdm 192: return;
193:
194: for (step = arglist; step; step = step->next)
195: switch (step->type) {
196: case HOST_TYPE:
1.7 deraadt 197: (void)fprintf(stderr,
198: "Warning: Ignoring hostname flag\n");
1.5 jdm 199: break;
200: case TTY_TYPE:
1.18 mickey 201: if (!ttyflag) { /* don't print this twice */
1.7 deraadt 202: (void)fprintf(stderr,
203: "Warning: Ignoring tty flag\n");
1.5 jdm 204: ttyflag = 1;
205: }
206: break;
207: case USER_TYPE:
1.7 deraadt 208: (void)fprintf(stderr,
209: "Warning: Ignoring username[s]\n");
1.5 jdm 210: break;
211: default:
1.20 millert 212: break;
1.5 jdm 213: /* PRINT NOTHING */
214: }
1.18 mickey 215: }
1.5 jdm 216:
217:
218: /*
1.33 ! deraadt 219: * read through the wtmp file
1.1 deraadt 220: */
221: void
1.27 deraadt 222: wtmp(void)
1.1 deraadt 223: {
1.33 ! deraadt 224: time_t delta, total = 0;
! 225: int timesize, wfd, snapfound = 0;
! 226: char *ct, *crmsg, tim[40];
! 227: struct utmp *bp;
! 228: struct stat stb;
! 229: ssize_t bytes;
1.8 deraadt 230: off_t bl;
1.33 ! deraadt 231: struct ttytab *T;
! 232:
1.1 deraadt 233: if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
234: err(1, "%s", file);
235: bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
236:
1.6 mickey 237: if (fulltime)
238: timesize = 8; /* HH:MM:SS */
239: else
240: timesize = 5; /* HH:MM */
241:
1.1 deraadt 242: (void)time(&buf[0].ut_time);
243: (void)signal(SIGINT, onintr);
244: (void)signal(SIGQUIT, onintr);
245:
246: while (--bl >= 0) {
1.11 millert 247: if (lseek(wfd, bl * sizeof(buf), SEEK_SET) == -1 ||
1.1 deraadt 248: (bytes = read(wfd, buf, sizeof(buf))) == -1)
249: err(1, "%s", file);
250: for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
251: /*
252: * if the terminal line is '~', the machine stopped.
253: * see utmp(5) for more info.
254: */
255: if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
256: /* everybody just logged out */
257: for (T = ttylist; T; T = T->next)
258: T->logout = -bp->ut_time;
259: currentout = -bp->ut_time;
260: crmsg = strncmp(bp->ut_name, "shutdown",
261: UT_NAMESIZE) ? "crash" : "shutdown";
1.33 ! deraadt 262:
1.18 mickey 263: /*
264: * if we're in snapshot mode, we want to
1.4 jdm 265: * exit if this shutdown/reboot appears
266: * while we we are tracking the active
267: * range
268: */
1.33 ! deraadt 269: if (snaptime && snapfound) {
! 270: close(wfd);
1.4 jdm 271: return;
1.33 ! deraadt 272: }
! 273:
1.18 mickey 274: /*
1.4 jdm 275: * don't print shutdown/reboot entries
1.18 mickey 276: * unless flagged for
277: */
1.5 jdm 278: if (want(bp, NO)) {
1.9 downsj 279: if (seconds) {
1.33 ! deraadt 280: snprintf(tim, sizeof tim, "%ld",
! 281: (long)bp->ut_time);
1.9 downsj 282: } else {
283: ct = ctime(&bp->ut_time);
1.33 ! deraadt 284: snprintf(tim, sizeof tim,
! 285: "%10.10s %*.*s", ct,
! 286: timesize, timesize, ct + 11);
1.9 downsj 287: }
1.33 ! deraadt 288: printf("%-*.*s %-*.*s %-*.*s %s \n",
! 289: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
! 290: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
! 291: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
! 292: tim);
! 293:
! 294: if (maxrec != -1 && !--maxrec) {
! 295: close(wfd);
1.1 deraadt 296: return;
1.33 ! deraadt 297: }
1.1 deraadt 298: }
299: continue;
300: }
1.33 ! deraadt 301:
1.1 deraadt 302: /*
303: * if the line is '{' or '|', date got set; see
304: * utmp(5) for more info.
305: */
1.33 ! deraadt 306: if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') &&
! 307: !bp->ut_line[1]) {
1.5 jdm 308: if (want(bp, NO)) {
1.9 downsj 309: if (seconds) {
1.33 ! deraadt 310: snprintf(tim, sizeof tim, "%ld",
1.28 deraadt 311: (long)bp->ut_time);
1.18 mickey 312: } else {
1.9 downsj 313: ct = ctime(&bp->ut_time);
1.33 ! deraadt 314: snprintf(tim, sizeof tim,
! 315: "%10.10s %*.*s", ct,
! 316: timesize, timesize, ct + 11);
1.18 mickey 317: }
1.33 ! deraadt 318: printf("%-*.*s %-*.*s %-*.*s %s \n",
! 319: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
! 320: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
! 321: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
! 322: tim);
! 323:
! 324: if (maxrec && !--maxrec) {
! 325: close(wfd);
1.1 deraadt 326: return;
1.33 ! deraadt 327: }
1.1 deraadt 328: }
329: continue;
330: }
1.33 ! deraadt 331:
1.1 deraadt 332: /* find associated tty */
333: for (T = ttylist;; T = T->next) {
334: if (!T) {
335: /* add new one */
336: T = addtty(bp->ut_line);
337: break;
338: }
339: if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
340: break;
341: }
1.33 ! deraadt 342:
1.18 mickey 343: /*
1.4 jdm 344: * print record if not in snapshot mode and wanted
345: * or in snapshot mode and in snapshot range
346: */
347: if (bp->ut_name[0] &&
1.28 deraadt 348: ((want(bp, YES)) || (bp->ut_time < snaptime &&
349: (T->logout > snaptime || !T->logout ||
350: T->logout < 0)))) {
1.4 jdm 351: snapfound = 1;
1.9 downsj 352: if (seconds) {
1.33 ! deraadt 353: snprintf(tim, sizeof tim, "%ld",
1.28 deraadt 354: (long)bp->ut_time);
1.9 downsj 355: } else {
356: ct = ctime(&bp->ut_time);
1.33 ! deraadt 357: snprintf(tim, sizeof tim,
! 358: "%10.10s %*.*s", ct,
! 359: timesize, timesize, ct + 11);
1.9 downsj 360: }
1.33 ! deraadt 361: printf("%-*.*s %-*.*s %-*.*s %s ",
! 362: NAME_WIDTH, UT_NAMESIZE, bp->ut_name,
! 363: UT_LINESIZE, UT_LINESIZE, bp->ut_line,
! 364: HOST_WIDTH, UT_HOSTSIZE, bp->ut_host,
! 365: tim);
! 366:
1.1 deraadt 367: if (!T->logout)
368: puts(" still logged in");
369: else {
370: if (T->logout < 0) {
371: T->logout = -T->logout;
372: printf("- %s", crmsg);
1.9 downsj 373: } else {
1.18 mickey 374: if (seconds)
1.9 downsj 375: printf("- %ld",
1.16 pvalchev 376: (long)T->logout);
1.9 downsj 377: else
378: printf("- %*.*s",
379: timesize, timesize,
380: ctime(&T->logout)+11);
1.1 deraadt 381: }
382: delta = T->logout - bp->ut_time;
1.9 downsj 383: if (seconds)
1.16 pvalchev 384: printf(" (%ld)\n", (long)delta);
1.9 downsj 385: else {
386: if (delta < SECSPERDAY)
387: printf(" (%*.*s)\n",
388: timesize, timesize,
389: asctime(gmtime(&delta))+11);
390: else
391: printf(" (%ld+%*.*s)\n",
392: delta / SECSPERDAY,
393: timesize, timesize,
394: asctime(gmtime(&delta))+11);
395: }
396: if (calculate)
397: total += delta;
1.1 deraadt 398: }
1.33 ! deraadt 399: if (maxrec != -1 && !--maxrec) {
! 400: close(wfd);
1.1 deraadt 401: return;
1.33 ! deraadt 402: }
1.1 deraadt 403: }
404: T->logout = bp->ut_time;
405: }
1.9 downsj 406: }
1.33 ! deraadt 407: close(wfd);
1.9 downsj 408: if (calculate) {
409: if ((total / SECSPERDAY) > 0) {
410: int days = (total / SECSPERDAY);
411: total -= (days * SECSPERDAY);
412:
413: printf("\nTotal time: %d days, %*.*s\n",
1.28 deraadt 414: days, timesize, timesize,
415: asctime(gmtime(&total))+11);
1.9 downsj 416: } else
417: printf("\nTotal time: %*.*s\n",
1.28 deraadt 418: timesize, timesize,
419: asctime(gmtime(&total))+11);
1.1 deraadt 420: }
421: ct = ctime(&buf[0].ut_time);
1.10 deraadt 422: printf("\nwtmp begins %10.10s %*.*s %4.4s\n", ct, timesize, timesize,
423: ct + 11, ct + 20);
1.1 deraadt 424: }
425:
426: /*
1.33 ! deraadt 427: * see if want this entry
1.1 deraadt 428: */
429: int
1.27 deraadt 430: want(struct utmp *bp, int check)
1.1 deraadt 431: {
1.33 ! deraadt 432: struct arg *step;
1.1 deraadt 433:
1.15 deraadt 434: if (check) {
1.1 deraadt 435: /*
436: * when uucp and ftp log in over a network, the entry in
437: * the utmp file is the name plus their process id. See
438: * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
439: */
440: if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
441: bp->ut_line[3] = '\0';
442: else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
443: bp->ut_line[4] = '\0';
1.15 deraadt 444: }
1.5 jdm 445:
1.18 mickey 446: if (snaptime) /* if snaptime is set, return NO */
1.5 jdm 447: return (NO);
448:
1.1 deraadt 449: if (!arglist)
450: return (YES);
451:
452: for (step = arglist; step; step = step->next)
1.33 ! deraadt 453: switch (step->type) {
1.1 deraadt 454: case HOST_TYPE:
455: if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
456: return (YES);
457: break;
458: case TTY_TYPE:
459: if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
460: return (YES);
461: break;
462: case USER_TYPE:
463: if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
464: return (YES);
465: break;
1.5 jdm 466: }
467:
1.1 deraadt 468: return (NO);
469: }
470:
471: /*
1.33 ! deraadt 472: * add an entry to a linked list of arguments
1.1 deraadt 473: */
474: void
1.27 deraadt 475: addarg(int type, char *arg)
1.1 deraadt 476: {
1.33 ! deraadt 477: struct arg *cur;
1.1 deraadt 478:
1.33 ! deraadt 479: if (!(cur = (struct arg *)malloc((u_int)sizeof(struct arg))))
1.1 deraadt 480: err(1, "malloc failure");
481: cur->next = arglist;
482: cur->type = type;
483: cur->name = arg;
484: arglist = cur;
485: }
486:
487: /*
1.33 ! deraadt 488: * add an entry to a linked list of ttys
1.1 deraadt 489: */
1.33 ! deraadt 490: struct ttytab *
1.27 deraadt 491: addtty(char *ttyname)
1.1 deraadt 492: {
1.33 ! deraadt 493: struct ttytab *cur;
1.1 deraadt 494:
1.33 ! deraadt 495: if (!(cur = (struct ttytab *)malloc((u_int)sizeof(struct ttytab))))
1.1 deraadt 496: err(1, "malloc failure");
497: cur->next = ttylist;
498: cur->logout = currentout;
499: memmove(cur->tty, ttyname, UT_LINESIZE);
500: return (ttylist = cur);
501: }
502:
503: /*
1.33 ! deraadt 504: * convert the hostname to search pattern; if the supplied host name
! 505: * has a domain attached that is the same as the current domain, rip
! 506: * off the domain suffix since that's what login(1) does.
1.1 deraadt 507: */
508: void
1.27 deraadt 509: hostconv(char *arg)
1.1 deraadt 510: {
1.33 ! deraadt 511: static char *hostdot, name[MAXHOSTNAMELEN];
1.1 deraadt 512: static int first = 1;
513: char *argdot;
514:
515: if (!(argdot = strchr(arg, '.')))
516: return;
517: if (first) {
518: first = 0;
519: if (gethostname(name, sizeof(name)))
520: err(1, "gethostname");
521: hostdot = strchr(name, '.');
522: }
523: if (hostdot && !strcasecmp(hostdot, argdot))
524: *argdot = '\0';
525: }
526:
527: /*
1.33 ! deraadt 528: * convert tty to correct name.
1.1 deraadt 529: */
530: char *
1.27 deraadt 531: ttyconv(char *arg)
1.1 deraadt 532: {
1.33 ! deraadt 533: size_t len = 8;
1.1 deraadt 534: char *mval;
535:
536: /*
537: * kludge -- we assume that all tty's end with
538: * a two character suffix.
539: */
540: if (strlen(arg) == 2) {
541: /* either 6 for "ttyxx" or 8 for "console" */
1.21 deraadt 542: if (!(mval = malloc(len)))
1.1 deraadt 543: err(1, "malloc failure");
544: if (!strcmp(arg, "co"))
1.21 deraadt 545: (void)strlcpy(mval, "console", len);
546: else
547: snprintf(mval, len, "tty%s", arg);
1.1 deraadt 548: return (mval);
549: }
550: if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
551: return (arg + 5);
552: return (arg);
553: }
1.4 jdm 554:
1.18 mickey 555: /*
556: * Convert the snapshot time in command line given in the format
1.23 deraadt 557: * [[[CC]YY]MMDD]hhmm[.SS]] to a time_t.
1.18 mickey 558: * Derived from atime_arg1() in usr.bin/touch/touch.c
1.4 jdm 559: */
560: time_t
1.27 deraadt 561: dateconv(char *arg)
1.4 jdm 562: {
1.7 deraadt 563: time_t timet;
564: struct tm *t;
565: int yearset;
566: char *p;
567:
568: /* Start with the current time. */
569: if (time(&timet) < 0)
570: err(1, "time");
571: if ((t = localtime(&timet)) == NULL)
572: err(1, "localtime");
573:
1.23 deraadt 574: /* [[[CC]YY]MMDD]hhmm[.SS] */
1.7 deraadt 575: if ((p = strchr(arg, '.')) == NULL)
1.18 mickey 576: t->tm_sec = 0; /* Seconds defaults to 0. */
1.7 deraadt 577: else {
578: if (strlen(p + 1) != 2)
579: goto terr;
580: *p++ = '\0';
581: t->tm_sec = ATOI2(p);
582: }
583:
584: yearset = 0;
585: switch (strlen(arg)) {
1.22 deraadt 586: case 12: /* CCYYMMDDhhmm */
1.7 deraadt 587: t->tm_year = ATOI2(arg);
588: t->tm_year *= 100;
589: yearset = 1;
1.29 otto 590: /* FALLTHROUGH */
1.22 deraadt 591: case 10: /* YYMMDDhhmm */
1.7 deraadt 592: if (yearset) {
593: yearset = ATOI2(arg);
594: t->tm_year += yearset;
595: } else {
596: yearset = ATOI2(arg);
597: if (yearset < 69)
598: t->tm_year = yearset + 2000;
599: else
600: t->tm_year = yearset + 1900;
601: }
1.28 deraadt 602: t->tm_year -= 1900; /* Convert to UNIX time. */
1.7 deraadt 603: /* FALLTHROUGH */
604: case 8: /* MMDDhhmm */
605: t->tm_mon = ATOI2(arg);
1.18 mickey 606: --t->tm_mon; /* Convert from 01-12 to 00-11 */
1.7 deraadt 607: t->tm_mday = ATOI2(arg);
608: t->tm_hour = ATOI2(arg);
609: t->tm_min = ATOI2(arg);
610: break;
611: case 4: /* hhmm */
612: t->tm_hour = ATOI2(arg);
613: t->tm_min = ATOI2(arg);
614: break;
615: default:
616: goto terr;
617: }
1.18 mickey 618: t->tm_isdst = -1; /* Figure out DST. */
1.7 deraadt 619: timet = mktime(t);
620: if (timet == -1)
1.28 deraadt 621: terr: errx(1, "out of range or illegal time specification: "
622: "[[[CC]YY]MMDD]hhmm[.SS]");
1.22 deraadt 623: return (timet);
1.4 jdm 624: }
625:
1.1 deraadt 626:
627: /*
1.33 ! deraadt 628: * on interrupt, we inform the user how far we've gotten
1.1 deraadt 629: */
630: void
1.27 deraadt 631: onintr(int signo)
1.1 deraadt 632: {
1.25 deraadt 633: char str[1024], *ct;
1.1 deraadt 634:
1.25 deraadt 635: ct = ctime(&buf[0].ut_time); /* XXX signal race */
636: snprintf(str, sizeof str, "\ninterrupted %10.10s %8.8s \n",
637: ct, ct + 11);
638: write(STDOUT_FILENO, str, strlen(str));
1.1 deraadt 639: if (signo == SIGINT)
1.25 deraadt 640: _exit(1);
1.22 deraadt 641: }
642:
643: void
644: usage(void)
645: {
646: extern char *__progname;
647:
648: fprintf(stderr,
1.31 millert 649: "usage: %s [-#] [-csT] [-f file] [-n #] [-t tty] [-h host]"
1.23 deraadt 650: " [-d [[[CC]YY]MMDD]hhmm[.SS]] [user ...]\n", __progname);
1.22 deraadt 651: exit(1);
1.1 deraadt 652: }