last.c revision 125856
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1987, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3578201Sddstatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 381590Srgrimes#endif /* not lint */ 391590Srgrimes 401590Srgrimes#ifndef lint 4178201Sddstatic const char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94"; 421590Srgrimes#endif /* not lint */ 4399112Sobrien#include <sys/cdefs.h> 4499112Sobrien__FBSDID("$FreeBSD: head/usr.bin/last/last.c 125856 2004-02-15 21:52:59Z dwmalone $"); 451590Srgrimes 461590Srgrimes#include <sys/param.h> 471590Srgrimes#include <sys/stat.h> 481590Srgrimes 491590Srgrimes#include <err.h> 50118077Stjr#include <errno.h> 511590Srgrimes#include <fcntl.h> 5274588Sache#include <langinfo.h> 5316438Sache#include <locale.h> 541590Srgrimes#include <paths.h> 551590Srgrimes#include <signal.h> 561590Srgrimes#include <stdio.h> 571590Srgrimes#include <stdlib.h> 581590Srgrimes#include <string.h> 591590Srgrimes#include <time.h> 60125856Sdwmalone#include <timeconv.h> 611590Srgrimes#include <unistd.h> 621590Srgrimes#include <utmp.h> 6311547Sdg#include <sys/queue.h> 641590Srgrimes 651590Srgrimes#define NO 0 /* false/no */ 661590Srgrimes#define YES 1 /* true/yes */ 6777291Sdd#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 681590Srgrimes 691590Srgrimesstatic struct utmp buf[1024]; /* utmp read buffer */ 701590Srgrimes 711590Srgrimestypedef struct arg { 721590Srgrimes char *name; /* argument */ 731590Srgrimes#define HOST_TYPE -2 741590Srgrimes#define TTY_TYPE -3 751590Srgrimes#define USER_TYPE -4 761590Srgrimes int type; /* type of arg */ 771590Srgrimes struct arg *next; /* linked list pointer */ 781590Srgrimes} ARG; 791590SrgrimesARG *arglist; /* head of linked list */ 801590Srgrimes 8160938SjakeLIST_HEAD(ttylisthead, ttytab) ttylist; 8211547Sdg 8311547Sdgstruct ttytab { 8436062Sjb time_t logout; /* log out time */ 851590Srgrimes char tty[UT_LINESIZE + 1]; /* terminal name */ 8660938Sjake LIST_ENTRY(ttytab) list; 8711547Sdg}; 881590Srgrimes 8991536Siedowsestatic const char *crmsg; /* cause of last reboot */ 901590Srgrimesstatic long currentout, /* current logout value */ 911590Srgrimes maxrec; /* records to display */ 9278201Sddstatic const char *file = _PATH_WTMP; /* wtmp file */ 9336434Sdannystatic int sflag = 0; /* show delta in seconds */ 9436434Sdannystatic int width = 5; /* show seconds in delta */ 9591541Siedowsestatic int yflag; /* show year */ 9674588Sachestatic int d_first; 9791538Siedowsestatic int snapfound = 0; /* found snapshot entry? */ 9877291Sddstatic time_t snaptime; /* if != 0, we will only 9977291Sdd * report users logged in 10077291Sdd * at this snapshot time 10177291Sdd */ 1021590Srgrimes 10392920Simpvoid addarg(int, char *); 10492920Simptime_t dateconv(char *); 10592920Simpvoid doentry(struct utmp *); 10692920Simpvoid hostconv(char *); 10792920Simpvoid onintr(int); 10892920Simpvoid printentry(struct utmp *, struct ttytab *); 10992920Simpchar *ttyconv(char *); 11092920Simpint want(struct utmp *); 11192920Simpvoid usage(void); 11292920Simpvoid wtmp(void); 1131590Srgrimes 11436434Sdannyvoid 11536434Sdannyusage(void) 11636434Sdanny{ 11736434Sdanny (void)fprintf(stderr, 118119023Stjr"usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n" 119119023Stjr" [-n maxrec] [-t tty] [user ...]\n"); 12036434Sdanny exit(1); 12136434Sdanny} 12236434Sdanny 1231590Srgrimesint 124102944Sdwmalonemain(int argc, char *argv[]) 1251590Srgrimes{ 1261590Srgrimes int ch; 1271590Srgrimes char *p; 1281590Srgrimes 12916438Sache (void) setlocale(LC_TIME, ""); 13074588Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 13116438Sache 1321590Srgrimes maxrec = -1; 13377291Sdd snaptime = 0; 134118077Stjr while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1) 1351590Srgrimes switch (ch) { 1361590Srgrimes case '0': case '1': case '2': case '3': case '4': 1371590Srgrimes case '5': case '6': case '7': case '8': case '9': 1381590Srgrimes /* 1391590Srgrimes * kludge: last was originally designed to take 1401590Srgrimes * a number after a dash. 1411590Srgrimes */ 1421590Srgrimes if (maxrec == -1) { 143106215Smux p = strchr(argv[optind - 1], ch); 144106215Smux if (p == NULL) 145106215Smux p = strchr(argv[optind], ch); 146106215Smux maxrec = atol(p); 1471590Srgrimes if (!maxrec) 1481590Srgrimes exit(0); 1491590Srgrimes } 1501590Srgrimes break; 15177291Sdd case 'd': 15277291Sdd snaptime = dateconv(optarg); 15377291Sdd break; 1541590Srgrimes case 'f': 1551590Srgrimes file = optarg; 1561590Srgrimes break; 1571590Srgrimes case 'h': 1581590Srgrimes hostconv(optarg); 1591590Srgrimes addarg(HOST_TYPE, optarg); 1601590Srgrimes break; 161118077Stjr case 'n': 162118077Stjr errno = 0; 163118077Stjr maxrec = strtol(optarg, &p, 10); 164118077Stjr if (p == optarg || *p != '\0' || errno != 0 || 165118077Stjr maxrec <= 0) 166118077Stjr errx(1, "%s: bad line count", optarg); 167118077Stjr break; 16836434Sdanny case 's': 16936434Sdanny sflag++; /* Show delta as seconds */ 17036434Sdanny break; 1711590Srgrimes case 't': 1721590Srgrimes addarg(TTY_TYPE, ttyconv(optarg)); 1731590Srgrimes break; 17436434Sdanny case 'w': 17536434Sdanny width = 8; 17636434Sdanny break; 17791541Siedowse case 'y': 17891541Siedowse yflag++; 17991541Siedowse break; 1801590Srgrimes case '?': 1811590Srgrimes default: 18236434Sdanny usage(); 1831590Srgrimes } 1841590Srgrimes 18536434Sdanny if (sflag && width == 8) usage(); 18636434Sdanny 1871590Srgrimes if (argc) { 1881590Srgrimes setlinebuf(stdout); 1891590Srgrimes for (argv += optind; *argv; ++argv) { 1901590Srgrimes#define COMPATIBILITY 1911590Srgrimes#ifdef COMPATIBILITY 1921590Srgrimes /* code to allow "last p5" to work */ 1931590Srgrimes addarg(TTY_TYPE, ttyconv(*argv)); 1941590Srgrimes#endif 1951590Srgrimes addarg(USER_TYPE, *argv); 1961590Srgrimes } 1971590Srgrimes } 1981590Srgrimes wtmp(); 1991590Srgrimes exit(0); 2001590Srgrimes} 2011590Srgrimes 2021590Srgrimes/* 2031590Srgrimes * wtmp -- 2041590Srgrimes * read through the wtmp file 2051590Srgrimes */ 2061590Srgrimesvoid 207102944Sdwmalonewtmp(void) 2081590Srgrimes{ 2091590Srgrimes struct utmp *bp; /* current structure */ 2101590Srgrimes struct stat stb; /* stat of file for size */ 21136062Sjb long bl; 2121590Srgrimes int bytes, wfd; 21316438Sache char ct[80]; 21416438Sache struct tm *tm; 21585648Sdillon time_t t; 2161590Srgrimes 21711547Sdg LIST_INIT(&ttylist); 21811547Sdg 2191590Srgrimes if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) 2201590Srgrimes err(1, "%s", file); 2211590Srgrimes bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf); 2221590Srgrimes 22385648Sdillon (void)time(&t); 22489572Sdillon buf[0].ut_time = _time_to_int(t); 2251590Srgrimes (void)signal(SIGINT, onintr); 2261590Srgrimes (void)signal(SIGQUIT, onintr); 2271590Srgrimes 2281590Srgrimes while (--bl >= 0) { 2291590Srgrimes if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || 2301590Srgrimes (bytes = read(wfd, buf, sizeof(buf))) == -1) 2311590Srgrimes err(1, "%s", file); 23291536Siedowse for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) 23391536Siedowse doentry(bp); 2341590Srgrimes } 23589572Sdillon t = _int_to_time(buf[0].ut_time); 23685648Sdillon tm = localtime(&t); 23778201Sdd (void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm); 23862871Skris printf("%s", ct); 2391590Srgrimes} 2401590Srgrimes 2411590Srgrimes/* 24291536Siedowse * doentry -- 24391536Siedowse * process a single wtmp entry 24491536Siedowse */ 24591536Siedowsevoid 246102944Sdwmalonedoentry(struct utmp *bp) 24791536Siedowse{ 24891536Siedowse struct ttytab *tt, *ttx; /* ttylist entry */ 24991536Siedowse 25091536Siedowse /* 25191536Siedowse * if the terminal line is '~', the machine stopped. 25291536Siedowse * see utmp(5) for more info. 25391536Siedowse */ 25491536Siedowse if (bp->ut_line[0] == '~' && !bp->ut_line[1]) { 25591536Siedowse /* everybody just logged out */ 25691536Siedowse for (tt = LIST_FIRST(&ttylist); tt;) { 25791536Siedowse LIST_REMOVE(tt, list); 25891536Siedowse ttx = tt; 25991536Siedowse tt = LIST_NEXT(tt, list); 26091536Siedowse free(ttx); 26191536Siedowse } 26291536Siedowse currentout = -bp->ut_time; 26391536Siedowse crmsg = strncmp(bp->ut_name, "shutdown", UT_NAMESIZE) ? 26491536Siedowse "crash" : "shutdown"; 26591536Siedowse /* 26691536Siedowse * if we're in snapshot mode, we want to exit if this 26791536Siedowse * shutdown/reboot appears while we we are tracking the 26891536Siedowse * active range 26991536Siedowse */ 27091536Siedowse if (snaptime && snapfound) 27191536Siedowse exit(0); 27291536Siedowse /* 27391536Siedowse * don't print shutdown/reboot entries unless flagged for 27491536Siedowse */ 27591538Siedowse if (!snaptime && want(bp)) 27691536Siedowse printentry(bp, NULL); 27791536Siedowse return; 27891536Siedowse } 27991536Siedowse /* 28091536Siedowse * if the line is '{' or '|', date got set; see 28191536Siedowse * utmp(5) for more info. 28291536Siedowse */ 28391536Siedowse if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') && 28491536Siedowse !bp->ut_line[1]) { 28591538Siedowse if (want(bp) && !snaptime) 28691536Siedowse printentry(bp, NULL); 28791536Siedowse return; 28891536Siedowse } 28991536Siedowse /* find associated tty */ 29091536Siedowse LIST_FOREACH(tt, &ttylist, list) 29191536Siedowse if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE)) 29291536Siedowse break; 29391536Siedowse 29491536Siedowse if (tt == NULL) { 29591536Siedowse /* add new one */ 29691536Siedowse tt = malloc(sizeof(struct ttytab)); 29791536Siedowse if (tt == NULL) 29896785Sjmallett errx(1, "malloc failure"); 29991536Siedowse tt->logout = currentout; 30091536Siedowse strncpy(tt->tty, bp->ut_line, UT_LINESIZE); 30191536Siedowse LIST_INSERT_HEAD(&ttylist, tt, list); 30291536Siedowse } 30391536Siedowse 30491536Siedowse /* 30591536Siedowse * print record if not in snapshot mode and wanted 30691536Siedowse * or in snapshot mode and in snapshot range 30791536Siedowse */ 30891536Siedowse if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime && 30991536Siedowse (tt->logout > snaptime || tt->logout < 1)))) { 31091536Siedowse snapfound = 1; 31191536Siedowse /* 31291536Siedowse * when uucp and ftp log in over a network, the entry in 31391536Siedowse * the utmp file is the name plus their process id. See 31491536Siedowse * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information. 31591536Siedowse */ 31691536Siedowse if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1)) 31791536Siedowse bp->ut_line[3] = '\0'; 31891536Siedowse else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1)) 31991536Siedowse bp->ut_line[4] = '\0'; 32091536Siedowse printentry(bp, tt); 32191536Siedowse } 32291536Siedowse tt->logout = bp->ut_time; 32391536Siedowse} 32491536Siedowse 32591536Siedowse/* 32691536Siedowse * printentry -- 32791536Siedowse * output an entry 32891536Siedowse * 32991536Siedowse * If `tt' is non-NULL, use it and `crmsg' to print the logout time or 33091536Siedowse * logout type (crash/shutdown) as appropriate. 33191536Siedowse */ 33291536Siedowsevoid 333102944Sdwmaloneprintentry(struct utmp *bp, struct ttytab *tt) 33491536Siedowse{ 33591536Siedowse char ct[80]; 33691536Siedowse struct tm *tm; 33791536Siedowse time_t delta; /* time difference */ 33891536Siedowse time_t t; 33991536Siedowse 34091538Siedowse if (maxrec != -1 && !maxrec--) 34191538Siedowse exit(0); 34291536Siedowse t = _int_to_time(bp->ut_time); 34391536Siedowse tm = localtime(&t); 34491541Siedowse (void) strftime(ct, sizeof(ct), d_first ? 34591541Siedowse (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") : 34691541Siedowse (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm); 34791536Siedowse printf("%-*.*s %-*.*s %-*.*s %s%c", 34891536Siedowse UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, 34991536Siedowse UT_LINESIZE, UT_LINESIZE, bp->ut_line, 35091536Siedowse UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, 35191536Siedowse ct, tt == NULL ? '\n' : ' '); 35291536Siedowse if (tt == NULL) 35391536Siedowse return; 35491536Siedowse if (!tt->logout) { 35591536Siedowse puts(" still logged in"); 35691536Siedowse return; 35791536Siedowse } 35891536Siedowse if (tt->logout < 0) { 35991536Siedowse tt->logout = -tt->logout; 36091536Siedowse printf("- %s", crmsg); 36191536Siedowse } else { 36291536Siedowse tm = localtime(&tt->logout); 36391536Siedowse (void) strftime(ct, sizeof(ct), "%R", tm); 36491536Siedowse printf("- %s", ct); 36591536Siedowse } 36691536Siedowse delta = tt->logout - bp->ut_time; 36791536Siedowse if (sflag) { 36891536Siedowse printf(" (%8ld)\n", (long)delta); 36991536Siedowse } else { 37091536Siedowse tm = gmtime(&delta); 37191536Siedowse (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); 37291536Siedowse if (delta < 86400) 37391536Siedowse printf(" (%s)\n", ct); 37491536Siedowse else 37591536Siedowse printf(" (%ld+%s)\n", (long)delta / 86400, ct); 37691536Siedowse } 37791536Siedowse} 37891536Siedowse 37991536Siedowse/* 3801590Srgrimes * want -- 3811590Srgrimes * see if want this entry 3821590Srgrimes */ 3831590Srgrimesint 384102944Sdwmalonewant(struct utmp *bp) 3851590Srgrimes{ 3861590Srgrimes ARG *step; 3871590Srgrimes 38877291Sdd if (snaptime) 38977291Sdd return (NO); 39077291Sdd 3911590Srgrimes if (!arglist) 3921590Srgrimes return (YES); 3931590Srgrimes 3941590Srgrimes for (step = arglist; step; step = step->next) 3951590Srgrimes switch(step->type) { 3961590Srgrimes case HOST_TYPE: 3971590Srgrimes if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE)) 3981590Srgrimes return (YES); 3991590Srgrimes break; 4001590Srgrimes case TTY_TYPE: 4011590Srgrimes if (!strncmp(step->name, bp->ut_line, UT_LINESIZE)) 4021590Srgrimes return (YES); 4031590Srgrimes break; 4041590Srgrimes case USER_TYPE: 4051590Srgrimes if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE)) 4061590Srgrimes return (YES); 4071590Srgrimes break; 40891536Siedowse } 4091590Srgrimes return (NO); 4101590Srgrimes} 4111590Srgrimes 4121590Srgrimes/* 4131590Srgrimes * addarg -- 4141590Srgrimes * add an entry to a linked list of arguments 4151590Srgrimes */ 4161590Srgrimesvoid 417102944Sdwmaloneaddarg(int type, char *arg) 4181590Srgrimes{ 4191590Srgrimes ARG *cur; 4201590Srgrimes 42196785Sjmallett if ((cur = malloc(sizeof(ARG))) == NULL) 42296785Sjmallett errx(1, "malloc failure"); 4231590Srgrimes cur->next = arglist; 4241590Srgrimes cur->type = type; 4251590Srgrimes cur->name = arg; 4261590Srgrimes arglist = cur; 4271590Srgrimes} 4281590Srgrimes 4291590Srgrimes/* 4301590Srgrimes * hostconv -- 4311590Srgrimes * convert the hostname to search pattern; if the supplied host name 4321590Srgrimes * has a domain attached that is the same as the current domain, rip 4331590Srgrimes * off the domain suffix since that's what login(1) does. 4341590Srgrimes */ 4351590Srgrimesvoid 436102944Sdwmalonehostconv(char *arg) 4371590Srgrimes{ 4381590Srgrimes static int first = 1; 4391590Srgrimes static char *hostdot, name[MAXHOSTNAMELEN]; 4401590Srgrimes char *argdot; 4411590Srgrimes 4421590Srgrimes if (!(argdot = strchr(arg, '.'))) 4431590Srgrimes return; 4441590Srgrimes if (first) { 4451590Srgrimes first = 0; 4461590Srgrimes if (gethostname(name, sizeof(name))) 4471590Srgrimes err(1, "gethostname"); 4481590Srgrimes hostdot = strchr(name, '.'); 4491590Srgrimes } 4501590Srgrimes if (hostdot && !strcasecmp(hostdot, argdot)) 4511590Srgrimes *argdot = '\0'; 4521590Srgrimes} 4531590Srgrimes 4541590Srgrimes/* 4551590Srgrimes * ttyconv -- 4561590Srgrimes * convert tty to correct name. 4571590Srgrimes */ 4581590Srgrimeschar * 459102944Sdwmalonettyconv(char *arg) 4601590Srgrimes{ 4611590Srgrimes char *mval; 4621590Srgrimes 4631590Srgrimes /* 4641590Srgrimes * kludge -- we assume that all tty's end with 4651590Srgrimes * a two character suffix. 4661590Srgrimes */ 4671590Srgrimes if (strlen(arg) == 2) { 4681590Srgrimes /* either 6 for "ttyxx" or 8 for "console" */ 46996785Sjmallett if ((mval = malloc(8)) == NULL) 47096785Sjmallett errx(1, "malloc failure"); 4711590Srgrimes if (!strcmp(arg, "co")) 4721590Srgrimes (void)strcpy(mval, "console"); 4731590Srgrimes else { 4741590Srgrimes (void)strcpy(mval, "tty"); 4751590Srgrimes (void)strcpy(mval + 3, arg); 4761590Srgrimes } 4771590Srgrimes return (mval); 4781590Srgrimes } 4791590Srgrimes if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) 4801590Srgrimes return (arg + 5); 4811590Srgrimes return (arg); 4821590Srgrimes} 4831590Srgrimes 4841590Srgrimes/* 48577291Sdd * dateconv -- 48677291Sdd * Convert the snapshot time in command line given in the format 48777291Sdd * [[CC]YY]MMDDhhmm[.SS]] to a time_t. 48877291Sdd * Derived from atime_arg1() in usr.bin/touch/touch.c 48977291Sdd */ 49077291Sddtime_t 491102944Sdwmalonedateconv(char *arg) 49277291Sdd{ 49377291Sdd time_t timet; 49477291Sdd struct tm *t; 49577291Sdd int yearset; 49677291Sdd char *p; 49777291Sdd 49877291Sdd /* Start with the current time. */ 49977291Sdd if (time(&timet) < 0) 50077291Sdd err(1, "time"); 50177291Sdd if ((t = localtime(&timet)) == NULL) 50277291Sdd err(1, "localtime"); 50377291Sdd 50477291Sdd /* [[CC]YY]MMDDhhmm[.SS] */ 50577291Sdd if ((p = strchr(arg, '.')) == NULL) 50677291Sdd t->tm_sec = 0; /* Seconds defaults to 0. */ 50777291Sdd else { 50877291Sdd if (strlen(p + 1) != 2) 50977291Sdd goto terr; 51077291Sdd *p++ = '\0'; 51177291Sdd t->tm_sec = ATOI2(p); 51277291Sdd } 51377291Sdd 51477291Sdd yearset = 0; 51577291Sdd switch (strlen(arg)) { 51677291Sdd case 12: /* CCYYMMDDhhmm */ 51777291Sdd t->tm_year = ATOI2(arg); 51877291Sdd t->tm_year *= 100; 51977291Sdd yearset = 1; 52077291Sdd /* FALLTHOUGH */ 52177291Sdd case 10: /* YYMMDDhhmm */ 52277291Sdd if (yearset) { 52377291Sdd yearset = ATOI2(arg); 52477291Sdd t->tm_year += yearset; 52577291Sdd } else { 52677291Sdd yearset = ATOI2(arg); 52777291Sdd if (yearset < 69) 52877291Sdd t->tm_year = yearset + 2000; 52977291Sdd else 53077291Sdd t->tm_year = yearset + 1900; 53177291Sdd } 53277291Sdd t->tm_year -= 1900; /* Convert to UNIX time. */ 53377291Sdd /* FALLTHROUGH */ 53477291Sdd case 8: /* MMDDhhmm */ 53577291Sdd t->tm_mon = ATOI2(arg); 53677291Sdd --t->tm_mon; /* Convert from 01-12 to 00-11 */ 53777291Sdd t->tm_mday = ATOI2(arg); 53877291Sdd t->tm_hour = ATOI2(arg); 53977291Sdd t->tm_min = ATOI2(arg); 54077291Sdd break; 54177291Sdd case 4: /* hhmm */ 54277291Sdd t->tm_hour = ATOI2(arg); 54377291Sdd t->tm_min = ATOI2(arg); 54477291Sdd break; 54577291Sdd default: 54677291Sdd goto terr; 54777291Sdd } 54877291Sdd t->tm_isdst = -1; /* Figure out DST. */ 54977291Sdd timet = mktime(t); 55077291Sdd if (timet == -1) 55177291Sddterr: errx(1, 55277291Sdd "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 55377291Sdd return timet; 55477291Sdd} 55577291Sdd 55677291Sdd 55777291Sdd/* 5581590Srgrimes * onintr -- 5591590Srgrimes * on interrupt, we inform the user how far we've gotten 5601590Srgrimes */ 5611590Srgrimesvoid 562102944Sdwmaloneonintr(int signo) 5631590Srgrimes{ 56416438Sache char ct[80]; 56516438Sache struct tm *tm; 56689572Sdillon time_t t = _int_to_time(buf[0].ut_time); 5671590Srgrimes 56885648Sdillon tm = localtime(&t); 56974588Sache (void) strftime(ct, sizeof(ct), 57074588Sache d_first ? "%a %e %b %R" : "%a %b %e %R", 57174588Sache tm); 57274588Sache printf("\ninterrupted %s\n", ct); 5731590Srgrimes if (signo == SIGINT) 5741590Srgrimes exit(1); 5751590Srgrimes (void)fflush(stdout); /* fix required for rsh */ 5761590Srgrimes} 577