last.c revision 106215
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 106215 2002-10-30 18:19:59Z mux $");
451590Srgrimes
461590Srgrimes#include <sys/param.h>
471590Srgrimes#include <sys/stat.h>
481590Srgrimes
491590Srgrimes#include <err.h>
501590Srgrimes#include <fcntl.h>
5174588Sache#include <langinfo.h>
5216438Sache#include <locale.h>
531590Srgrimes#include <paths.h>
541590Srgrimes#include <signal.h>
551590Srgrimes#include <stdio.h>
561590Srgrimes#include <stdlib.h>
571590Srgrimes#include <string.h>
581590Srgrimes#include <time.h>
591590Srgrimes#include <unistd.h>
601590Srgrimes#include <utmp.h>
6111547Sdg#include <sys/queue.h>
621590Srgrimes
631590Srgrimes#define	NO	0				/* false/no */
641590Srgrimes#define	YES	1				/* true/yes */
6577291Sdd#define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
661590Srgrimes
671590Srgrimesstatic struct utmp	buf[1024];		/* utmp read buffer */
681590Srgrimes
691590Srgrimestypedef struct arg {
701590Srgrimes	char	*name;				/* argument */
711590Srgrimes#define	HOST_TYPE	-2
721590Srgrimes#define	TTY_TYPE	-3
731590Srgrimes#define	USER_TYPE	-4
741590Srgrimes	int	type;				/* type of arg */
751590Srgrimes	struct arg	*next;			/* linked list pointer */
761590Srgrimes} ARG;
771590SrgrimesARG	*arglist;				/* head of linked list */
781590Srgrimes
7960938SjakeLIST_HEAD(ttylisthead, ttytab) ttylist;
8011547Sdg
8111547Sdgstruct ttytab {
8236062Sjb	time_t	logout;				/* log out time */
831590Srgrimes	char	tty[UT_LINESIZE + 1];		/* terminal name */
8460938Sjake	LIST_ENTRY(ttytab) list;
8511547Sdg};
861590Srgrimes
8791536Siedowsestatic const	char *crmsg;			/* cause of last reboot */
881590Srgrimesstatic long	currentout,			/* current logout value */
891590Srgrimes		maxrec;				/* records to display */
9078201Sddstatic const	char *file = _PATH_WTMP;		/* wtmp file */
9136434Sdannystatic int	sflag = 0;			/* show delta in seconds */
9236434Sdannystatic int	width = 5;			/* show seconds in delta */
9391541Siedowsestatic int	yflag;				/* show year */
9474588Sachestatic int      d_first;
9591538Siedowsestatic int	snapfound = 0;			/* found snapshot entry? */
9677291Sddstatic time_t	snaptime;			/* if != 0, we will only
9777291Sdd						 * report users logged in
9877291Sdd						 * at this snapshot time
9977291Sdd						 */
1001590Srgrimes
10192920Simpvoid	 addarg(int, char *);
10292920Simptime_t	 dateconv(char *);
10392920Simpvoid	 doentry(struct utmp *);
10492920Simpvoid	 hostconv(char *);
10592920Simpvoid	 onintr(int);
10692920Simpvoid	 printentry(struct utmp *, struct ttytab *);
10792920Simpchar	*ttyconv(char *);
10892920Simpint	 want(struct utmp *);
10992920Simpvoid	 usage(void);
11092920Simpvoid	 wtmp(void);
1111590Srgrimes
11236434Sdannyvoid
11336434Sdannyusage(void)
11436434Sdanny{
11536434Sdanny	(void)fprintf(stderr,
11691541Siedowse"usage: last [-#] [-y] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n"
11781161Sdd"\t[-t tty] [-s|w] [user ...]\n");
11836434Sdanny	exit(1);
11936434Sdanny}
12036434Sdanny
1211590Srgrimesint
122102944Sdwmalonemain(int argc, char *argv[])
1231590Srgrimes{
1241590Srgrimes	int ch;
1251590Srgrimes	char *p;
1261590Srgrimes
12716438Sache	(void) setlocale(LC_TIME, "");
12874588Sache	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
12916438Sache
1301590Srgrimes	maxrec = -1;
13177291Sdd	snaptime = 0;
13291541Siedowse	while ((ch = getopt(argc, argv, "0123456789d:f:h:st:wy")) != -1)
1331590Srgrimes		switch (ch) {
1341590Srgrimes		case '0': case '1': case '2': case '3': case '4':
1351590Srgrimes		case '5': case '6': case '7': case '8': case '9':
1361590Srgrimes			/*
1371590Srgrimes			 * kludge: last was originally designed to take
1381590Srgrimes			 * a number after a dash.
1391590Srgrimes			 */
1401590Srgrimes			if (maxrec == -1) {
141106215Smux				p = strchr(argv[optind - 1], ch);
142106215Smux				if (p == NULL)
143106215Smux					p = strchr(argv[optind], ch);
144106215Smux				maxrec = atol(p);
1451590Srgrimes				if (!maxrec)
1461590Srgrimes					exit(0);
1471590Srgrimes			}
1481590Srgrimes			break;
14977291Sdd		case 'd':
15077291Sdd			snaptime = dateconv(optarg);
15177291Sdd			break;
1521590Srgrimes		case 'f':
1531590Srgrimes			file = optarg;
1541590Srgrimes			break;
1551590Srgrimes		case 'h':
1561590Srgrimes			hostconv(optarg);
1571590Srgrimes			addarg(HOST_TYPE, optarg);
1581590Srgrimes			break;
15936434Sdanny		case 's':
16036434Sdanny			sflag++;	/* Show delta as seconds */
16136434Sdanny			break;
1621590Srgrimes		case 't':
1631590Srgrimes			addarg(TTY_TYPE, ttyconv(optarg));
1641590Srgrimes			break;
16536434Sdanny		case 'w':
16636434Sdanny			width = 8;
16736434Sdanny			break;
16891541Siedowse		case 'y':
16991541Siedowse			yflag++;
17091541Siedowse			break;
1711590Srgrimes		case '?':
1721590Srgrimes		default:
17336434Sdanny			usage();
1741590Srgrimes		}
1751590Srgrimes
17636434Sdanny	if (sflag && width == 8) usage();
17736434Sdanny
1781590Srgrimes	if (argc) {
1791590Srgrimes		setlinebuf(stdout);
1801590Srgrimes		for (argv += optind; *argv; ++argv) {
1811590Srgrimes#define	COMPATIBILITY
1821590Srgrimes#ifdef	COMPATIBILITY
1831590Srgrimes			/* code to allow "last p5" to work */
1841590Srgrimes			addarg(TTY_TYPE, ttyconv(*argv));
1851590Srgrimes#endif
1861590Srgrimes			addarg(USER_TYPE, *argv);
1871590Srgrimes		}
1881590Srgrimes	}
1891590Srgrimes	wtmp();
1901590Srgrimes	exit(0);
1911590Srgrimes}
1921590Srgrimes
1931590Srgrimes/*
1941590Srgrimes * wtmp --
1951590Srgrimes *	read through the wtmp file
1961590Srgrimes */
1971590Srgrimesvoid
198102944Sdwmalonewtmp(void)
1991590Srgrimes{
2001590Srgrimes	struct utmp	*bp;			/* current structure */
2011590Srgrimes	struct stat	stb;			/* stat of file for size */
20236062Sjb	long	bl;
2031590Srgrimes	int	bytes, wfd;
20416438Sache	char ct[80];
20516438Sache	struct tm *tm;
20685648Sdillon	time_t	t;
2071590Srgrimes
20811547Sdg	LIST_INIT(&ttylist);
20911547Sdg
2101590Srgrimes	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
2111590Srgrimes		err(1, "%s", file);
2121590Srgrimes	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
2131590Srgrimes
21485648Sdillon	(void)time(&t);
21589572Sdillon	buf[0].ut_time = _time_to_int(t);
2161590Srgrimes	(void)signal(SIGINT, onintr);
2171590Srgrimes	(void)signal(SIGQUIT, onintr);
2181590Srgrimes
2191590Srgrimes	while (--bl >= 0) {
2201590Srgrimes		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
2211590Srgrimes		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
2221590Srgrimes			err(1, "%s", file);
22391536Siedowse		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp)
22491536Siedowse			doentry(bp);
2251590Srgrimes	}
22689572Sdillon	t = _int_to_time(buf[0].ut_time);
22785648Sdillon	tm = localtime(&t);
22878201Sdd	(void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm);
22962871Skris	printf("%s", ct);
2301590Srgrimes}
2311590Srgrimes
2321590Srgrimes/*
23391536Siedowse * doentry --
23491536Siedowse *	process a single wtmp entry
23591536Siedowse */
23691536Siedowsevoid
237102944Sdwmalonedoentry(struct utmp *bp)
23891536Siedowse{
23991536Siedowse	struct ttytab	*tt, *ttx;		/* ttylist entry */
24091536Siedowse
24191536Siedowse	/*
24291536Siedowse	 * if the terminal line is '~', the machine stopped.
24391536Siedowse	 * see utmp(5) for more info.
24491536Siedowse	 */
24591536Siedowse	if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
24691536Siedowse		/* everybody just logged out */
24791536Siedowse		for (tt = LIST_FIRST(&ttylist); tt;) {
24891536Siedowse			LIST_REMOVE(tt, list);
24991536Siedowse			ttx = tt;
25091536Siedowse			tt = LIST_NEXT(tt, list);
25191536Siedowse			free(ttx);
25291536Siedowse		}
25391536Siedowse		currentout = -bp->ut_time;
25491536Siedowse		crmsg = strncmp(bp->ut_name, "shutdown", UT_NAMESIZE) ?
25591536Siedowse		    "crash" : "shutdown";
25691536Siedowse		/*
25791536Siedowse		 * if we're in snapshot mode, we want to exit if this
25891536Siedowse		 * shutdown/reboot appears while we we are tracking the
25991536Siedowse		 * active range
26091536Siedowse		 */
26191536Siedowse		if (snaptime && snapfound)
26291536Siedowse			exit(0);
26391536Siedowse		/*
26491536Siedowse		 * don't print shutdown/reboot entries unless flagged for
26591536Siedowse		 */
26691538Siedowse		if (!snaptime && want(bp))
26791536Siedowse			printentry(bp, NULL);
26891536Siedowse		return;
26991536Siedowse	}
27091536Siedowse	/*
27191536Siedowse	 * if the line is '{' or '|', date got set; see
27291536Siedowse	 * utmp(5) for more info.
27391536Siedowse	 */
27491536Siedowse	if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|') &&
27591536Siedowse	    !bp->ut_line[1]) {
27691538Siedowse		if (want(bp) && !snaptime)
27791536Siedowse			printentry(bp, NULL);
27891536Siedowse		return;
27991536Siedowse	}
28091536Siedowse	/* find associated tty */
28191536Siedowse	LIST_FOREACH(tt, &ttylist, list)
28291536Siedowse	    if (!strncmp(tt->tty, bp->ut_line, UT_LINESIZE))
28391536Siedowse		    break;
28491536Siedowse
28591536Siedowse	if (tt == NULL) {
28691536Siedowse		/* add new one */
28791536Siedowse		tt = malloc(sizeof(struct ttytab));
28891536Siedowse		if (tt == NULL)
28996785Sjmallett			errx(1, "malloc failure");
29091536Siedowse		tt->logout = currentout;
29191536Siedowse		strncpy(tt->tty, bp->ut_line, UT_LINESIZE);
29291536Siedowse		LIST_INSERT_HEAD(&ttylist, tt, list);
29391536Siedowse	}
29491536Siedowse
29591536Siedowse	/*
29691536Siedowse	 * print record if not in snapshot mode and wanted
29791536Siedowse	 * or in snapshot mode and in snapshot range
29891536Siedowse	 */
29991536Siedowse	if (bp->ut_name[0] && (want(bp) || (bp->ut_time < snaptime &&
30091536Siedowse	    (tt->logout > snaptime || tt->logout < 1)))) {
30191536Siedowse		snapfound = 1;
30291536Siedowse		/*
30391536Siedowse		 * when uucp and ftp log in over a network, the entry in
30491536Siedowse		 * the utmp file is the name plus their process id.  See
30591536Siedowse		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
30691536Siedowse		 */
30791536Siedowse		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
30891536Siedowse			bp->ut_line[3] = '\0';
30991536Siedowse		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
31091536Siedowse			bp->ut_line[4] = '\0';
31191536Siedowse		printentry(bp, tt);
31291536Siedowse	}
31391536Siedowse	tt->logout = bp->ut_time;
31491536Siedowse}
31591536Siedowse
31691536Siedowse/*
31791536Siedowse * printentry --
31891536Siedowse *	output an entry
31991536Siedowse *
32091536Siedowse * If `tt' is non-NULL, use it and `crmsg' to print the logout time or
32191536Siedowse * logout type (crash/shutdown) as appropriate.
32291536Siedowse */
32391536Siedowsevoid
324102944Sdwmaloneprintentry(struct utmp *bp, struct ttytab *tt)
32591536Siedowse{
32691536Siedowse	char ct[80];
32791536Siedowse	struct tm *tm;
32891536Siedowse	time_t	delta;				/* time difference */
32991536Siedowse	time_t	t;
33091536Siedowse
33191538Siedowse	if (maxrec != -1 && !maxrec--)
33291538Siedowse		exit(0);
33391536Siedowse	t = _int_to_time(bp->ut_time);
33491536Siedowse	tm = localtime(&t);
33591541Siedowse	(void) strftime(ct, sizeof(ct), d_first ?
33691541Siedowse	    (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") :
33791541Siedowse	    (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm);
33891536Siedowse	printf("%-*.*s %-*.*s %-*.*s %s%c",
33991536Siedowse	    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
34091536Siedowse	    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
34191536Siedowse	    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
34291536Siedowse	    ct, tt == NULL ? '\n' : ' ');
34391536Siedowse	if (tt == NULL)
34491536Siedowse		return;
34591536Siedowse	if (!tt->logout) {
34691536Siedowse		puts("  still logged in");
34791536Siedowse		return;
34891536Siedowse	}
34991536Siedowse	if (tt->logout < 0) {
35091536Siedowse		tt->logout = -tt->logout;
35191536Siedowse		printf("- %s", crmsg);
35291536Siedowse	} else {
35391536Siedowse		tm = localtime(&tt->logout);
35491536Siedowse		(void) strftime(ct, sizeof(ct), "%R", tm);
35591536Siedowse		printf("- %s", ct);
35691536Siedowse	}
35791536Siedowse	delta = tt->logout - bp->ut_time;
35891536Siedowse	if (sflag) {
35991536Siedowse		printf("  (%8ld)\n", (long)delta);
36091536Siedowse	} else {
36191536Siedowse		tm = gmtime(&delta);
36291536Siedowse		(void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
36391536Siedowse		if (delta < 86400)
36491536Siedowse			printf("  (%s)\n", ct);
36591536Siedowse		else
36691536Siedowse			printf(" (%ld+%s)\n", (long)delta / 86400, ct);
36791536Siedowse	}
36891536Siedowse}
36991536Siedowse
37091536Siedowse/*
3711590Srgrimes * want --
3721590Srgrimes *	see if want this entry
3731590Srgrimes */
3741590Srgrimesint
375102944Sdwmalonewant(struct utmp *bp)
3761590Srgrimes{
3771590Srgrimes	ARG *step;
3781590Srgrimes
37977291Sdd	if (snaptime)
38077291Sdd		return (NO);
38177291Sdd
3821590Srgrimes	if (!arglist)
3831590Srgrimes		return (YES);
3841590Srgrimes
3851590Srgrimes	for (step = arglist; step; step = step->next)
3861590Srgrimes		switch(step->type) {
3871590Srgrimes		case HOST_TYPE:
3881590Srgrimes			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
3891590Srgrimes				return (YES);
3901590Srgrimes			break;
3911590Srgrimes		case TTY_TYPE:
3921590Srgrimes			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
3931590Srgrimes				return (YES);
3941590Srgrimes			break;
3951590Srgrimes		case USER_TYPE:
3961590Srgrimes			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
3971590Srgrimes				return (YES);
3981590Srgrimes			break;
39991536Siedowse		}
4001590Srgrimes	return (NO);
4011590Srgrimes}
4021590Srgrimes
4031590Srgrimes/*
4041590Srgrimes * addarg --
4051590Srgrimes *	add an entry to a linked list of arguments
4061590Srgrimes */
4071590Srgrimesvoid
408102944Sdwmaloneaddarg(int type, char *arg)
4091590Srgrimes{
4101590Srgrimes	ARG *cur;
4111590Srgrimes
41296785Sjmallett	if ((cur = malloc(sizeof(ARG))) == NULL)
41396785Sjmallett		errx(1, "malloc failure");
4141590Srgrimes	cur->next = arglist;
4151590Srgrimes	cur->type = type;
4161590Srgrimes	cur->name = arg;
4171590Srgrimes	arglist = cur;
4181590Srgrimes}
4191590Srgrimes
4201590Srgrimes/*
4211590Srgrimes * hostconv --
4221590Srgrimes *	convert the hostname to search pattern; if the supplied host name
4231590Srgrimes *	has a domain attached that is the same as the current domain, rip
4241590Srgrimes *	off the domain suffix since that's what login(1) does.
4251590Srgrimes */
4261590Srgrimesvoid
427102944Sdwmalonehostconv(char *arg)
4281590Srgrimes{
4291590Srgrimes	static int first = 1;
4301590Srgrimes	static char *hostdot, name[MAXHOSTNAMELEN];
4311590Srgrimes	char *argdot;
4321590Srgrimes
4331590Srgrimes	if (!(argdot = strchr(arg, '.')))
4341590Srgrimes		return;
4351590Srgrimes	if (first) {
4361590Srgrimes		first = 0;
4371590Srgrimes		if (gethostname(name, sizeof(name)))
4381590Srgrimes			err(1, "gethostname");
4391590Srgrimes		hostdot = strchr(name, '.');
4401590Srgrimes	}
4411590Srgrimes	if (hostdot && !strcasecmp(hostdot, argdot))
4421590Srgrimes		*argdot = '\0';
4431590Srgrimes}
4441590Srgrimes
4451590Srgrimes/*
4461590Srgrimes * ttyconv --
4471590Srgrimes *	convert tty to correct name.
4481590Srgrimes */
4491590Srgrimeschar *
450102944Sdwmalonettyconv(char *arg)
4511590Srgrimes{
4521590Srgrimes	char *mval;
4531590Srgrimes
4541590Srgrimes	/*
4551590Srgrimes	 * kludge -- we assume that all tty's end with
4561590Srgrimes	 * a two character suffix.
4571590Srgrimes	 */
4581590Srgrimes	if (strlen(arg) == 2) {
4591590Srgrimes		/* either 6 for "ttyxx" or 8 for "console" */
46096785Sjmallett		if ((mval = malloc(8)) == NULL)
46196785Sjmallett			errx(1, "malloc failure");
4621590Srgrimes		if (!strcmp(arg, "co"))
4631590Srgrimes			(void)strcpy(mval, "console");
4641590Srgrimes		else {
4651590Srgrimes			(void)strcpy(mval, "tty");
4661590Srgrimes			(void)strcpy(mval + 3, arg);
4671590Srgrimes		}
4681590Srgrimes		return (mval);
4691590Srgrimes	}
4701590Srgrimes	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
4711590Srgrimes		return (arg + 5);
4721590Srgrimes	return (arg);
4731590Srgrimes}
4741590Srgrimes
4751590Srgrimes/*
47677291Sdd * dateconv --
47777291Sdd * 	Convert the snapshot time in command line given in the format
47877291Sdd * 	[[CC]YY]MMDDhhmm[.SS]] to a time_t.
47977291Sdd * 	Derived from atime_arg1() in usr.bin/touch/touch.c
48077291Sdd */
48177291Sddtime_t
482102944Sdwmalonedateconv(char *arg)
48377291Sdd{
48477291Sdd        time_t timet;
48577291Sdd        struct tm *t;
48677291Sdd        int yearset;
48777291Sdd        char *p;
48877291Sdd
48977291Sdd        /* Start with the current time. */
49077291Sdd        if (time(&timet) < 0)
49177291Sdd                err(1, "time");
49277291Sdd        if ((t = localtime(&timet)) == NULL)
49377291Sdd                err(1, "localtime");
49477291Sdd
49577291Sdd        /* [[CC]YY]MMDDhhmm[.SS] */
49677291Sdd        if ((p = strchr(arg, '.')) == NULL)
49777291Sdd                t->tm_sec = 0; 		/* Seconds defaults to 0. */
49877291Sdd        else {
49977291Sdd                if (strlen(p + 1) != 2)
50077291Sdd                        goto terr;
50177291Sdd                *p++ = '\0';
50277291Sdd                t->tm_sec = ATOI2(p);
50377291Sdd        }
50477291Sdd
50577291Sdd        yearset = 0;
50677291Sdd        switch (strlen(arg)) {
50777291Sdd        case 12:                	/* CCYYMMDDhhmm */
50877291Sdd                t->tm_year = ATOI2(arg);
50977291Sdd                t->tm_year *= 100;
51077291Sdd                yearset = 1;
51177291Sdd                /* FALLTHOUGH */
51277291Sdd        case 10:                	/* YYMMDDhhmm */
51377291Sdd                if (yearset) {
51477291Sdd                        yearset = ATOI2(arg);
51577291Sdd                        t->tm_year += yearset;
51677291Sdd                } else {
51777291Sdd                        yearset = ATOI2(arg);
51877291Sdd                        if (yearset < 69)
51977291Sdd                                t->tm_year = yearset + 2000;
52077291Sdd                        else
52177291Sdd                                t->tm_year = yearset + 1900;
52277291Sdd                }
52377291Sdd                t->tm_year -= 1900;     /* Convert to UNIX time. */
52477291Sdd                /* FALLTHROUGH */
52577291Sdd        case 8:				/* MMDDhhmm */
52677291Sdd                t->tm_mon = ATOI2(arg);
52777291Sdd                --t->tm_mon;    	/* Convert from 01-12 to 00-11 */
52877291Sdd                t->tm_mday = ATOI2(arg);
52977291Sdd                t->tm_hour = ATOI2(arg);
53077291Sdd                t->tm_min = ATOI2(arg);
53177291Sdd                break;
53277291Sdd        case 4:				/* hhmm */
53377291Sdd                t->tm_hour = ATOI2(arg);
53477291Sdd                t->tm_min = ATOI2(arg);
53577291Sdd                break;
53677291Sdd        default:
53777291Sdd                goto terr;
53877291Sdd        }
53977291Sdd        t->tm_isdst = -1;       	/* Figure out DST. */
54077291Sdd        timet = mktime(t);
54177291Sdd        if (timet == -1)
54277291Sddterr:           errx(1,
54377291Sdd        "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
54477291Sdd        return timet;
54577291Sdd}
54677291Sdd
54777291Sdd
54877291Sdd/*
5491590Srgrimes * onintr --
5501590Srgrimes *	on interrupt, we inform the user how far we've gotten
5511590Srgrimes */
5521590Srgrimesvoid
553102944Sdwmaloneonintr(int signo)
5541590Srgrimes{
55516438Sache	char ct[80];
55616438Sache	struct tm *tm;
55789572Sdillon	time_t t = _int_to_time(buf[0].ut_time);
5581590Srgrimes
55985648Sdillon	tm = localtime(&t);
56074588Sache	(void) strftime(ct, sizeof(ct),
56174588Sache			d_first ? "%a %e %b %R" : "%a %b %e %R",
56274588Sache			tm);
56374588Sache	printf("\ninterrupted %s\n", ct);
5641590Srgrimes	if (signo == SIGINT)
5651590Srgrimes		exit(1);
5661590Srgrimes	(void)fflush(stdout);			/* fix required for rsh */
5671590Srgrimes}
568