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

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