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

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