[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.48

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