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

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