[BACK]Return to last.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / last

Annotation of src/usr.bin/last/last.c, Revision 1.47

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