last.c revision 91538
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.
3260833Sjake *
3360833Sjake * $FreeBSD: head/usr.bin/last/last.c 91538 2002-03-01 19:46:20Z iedowse $
341590Srgrimes */
351590Srgrimes
361590Srgrimes#ifndef lint
3778201Sddstatic const char copyright[] =
381590Srgrimes"@(#) Copyright (c) 1987, 1993, 1994\n\
391590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes#ifndef lint
4378201Sddstatic const char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
441590Srgrimes#endif /* not lint */
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 */
9374588Sachestatic int      d_first;
9491538Siedowsestatic int	snapfound = 0;			/* found snapshot entry? */
9577291Sddstatic time_t	snaptime;			/* if != 0, we will only
9677291Sdd						 * report users logged in
9777291Sdd						 * at this snapshot time
9877291Sdd						 */
991590Srgrimes
1001590Srgrimesvoid	 addarg __P((int, char *));
10178201Sddtime_t	 dateconv __P((char *));
10291536Siedowsevoid	 doentry __P((struct utmp *));
1031590Srgrimesvoid	 hostconv __P((char *));
1041590Srgrimesvoid	 onintr __P((int));
10591536Siedowsevoid	 printentry __P((struct utmp *, struct ttytab *));
1061590Srgrimeschar	*ttyconv __P((char *));
10711547Sdgint	 want __P((struct utmp *));
10878201Sddvoid	 usage __P((void));
1091590Srgrimesvoid	 wtmp __P((void));
1101590Srgrimes
11136434Sdannyvoid
11236434Sdannyusage(void)
11336434Sdanny{
11436434Sdanny	(void)fprintf(stderr,
11581161Sdd"usage: last [-#] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h hostname]\n"
11681161Sdd"\t[-t tty] [-s|w] [user ...]\n");
11736434Sdanny	exit(1);
11836434Sdanny}
11936434Sdanny
1201590Srgrimesint
1211590Srgrimesmain(argc, argv)
1221590Srgrimes	int argc;
1231590Srgrimes	char *argv[];
1241590Srgrimes{
1251590Srgrimes	int ch;
1261590Srgrimes	char *p;
1271590Srgrimes
12816438Sache	(void) setlocale(LC_TIME, "");
12974588Sache	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
13016438Sache
1311590Srgrimes	maxrec = -1;
13277291Sdd	snaptime = 0;
13377291Sdd	while ((ch = getopt(argc, argv, "0123456789d:f:h:st:w")) != -1)
1341590Srgrimes		switch (ch) {
1351590Srgrimes		case '0': case '1': case '2': case '3': case '4':
1361590Srgrimes		case '5': case '6': case '7': case '8': case '9':
1371590Srgrimes			/*
1381590Srgrimes			 * kludge: last was originally designed to take
1391590Srgrimes			 * a number after a dash.
1401590Srgrimes			 */
1411590Srgrimes			if (maxrec == -1) {
1421590Srgrimes				p = argv[optind - 1];
1431590Srgrimes				if (p[0] == '-' && p[1] == ch && !p[2])
1441590Srgrimes					maxrec = atol(++p);
1451590Srgrimes				else
1461590Srgrimes					maxrec = atol(argv[optind] + 1);
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;
16136434Sdanny		case 's':
16236434Sdanny			sflag++;	/* Show delta as seconds */
16336434Sdanny			break;
1641590Srgrimes		case 't':
1651590Srgrimes			addarg(TTY_TYPE, ttyconv(optarg));
1661590Srgrimes			break;
16736434Sdanny		case 'w':
16836434Sdanny			width = 8;
16936434Sdanny			break;
1701590Srgrimes		case '?':
1711590Srgrimes		default:
17236434Sdanny			usage();
1731590Srgrimes		}
1741590Srgrimes
17536434Sdanny	if (sflag && width == 8) usage();
17636434Sdanny
1771590Srgrimes	if (argc) {
1781590Srgrimes		setlinebuf(stdout);
1791590Srgrimes		for (argv += optind; *argv; ++argv) {
1801590Srgrimes#define	COMPATIBILITY
1811590Srgrimes#ifdef	COMPATIBILITY
1821590Srgrimes			/* code to allow "last p5" to work */
1831590Srgrimes			addarg(TTY_TYPE, ttyconv(*argv));
1841590Srgrimes#endif
1851590Srgrimes			addarg(USER_TYPE, *argv);
1861590Srgrimes		}
1871590Srgrimes	}
1881590Srgrimes	wtmp();
1891590Srgrimes	exit(0);
1901590Srgrimes}
1911590Srgrimes
1921590Srgrimes/*
1931590Srgrimes * wtmp --
1941590Srgrimes *	read through the wtmp file
1951590Srgrimes */
1961590Srgrimesvoid
1971590Srgrimeswtmp()
1981590Srgrimes{
1991590Srgrimes	struct utmp	*bp;			/* current structure */
2001590Srgrimes	struct stat	stb;			/* stat of file for size */
20136062Sjb	long	bl;
2021590Srgrimes	int	bytes, wfd;
20316438Sache	char ct[80];
20416438Sache	struct tm *tm;
20585648Sdillon	time_t	t;
2061590Srgrimes
20711547Sdg	LIST_INIT(&ttylist);
20811547Sdg
2091590Srgrimes	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
2101590Srgrimes		err(1, "%s", file);
2111590Srgrimes	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
2121590Srgrimes
21385648Sdillon	(void)time(&t);
21489572Sdillon	buf[0].ut_time = _time_to_int(t);
2151590Srgrimes	(void)signal(SIGINT, onintr);
2161590Srgrimes	(void)signal(SIGQUIT, onintr);
2171590Srgrimes
2181590Srgrimes	while (--bl >= 0) {
2191590Srgrimes		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
2201590Srgrimes		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
2211590Srgrimes			err(1, "%s", file);
22291536Siedowse		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp)
22391536Siedowse			doentry(bp);
2241590Srgrimes	}
22589572Sdillon	t = _int_to_time(buf[0].ut_time);
22685648Sdillon	tm = localtime(&t);
22778201Sdd	(void) strftime(ct, sizeof(ct), "\nwtmp begins %+\n", tm);
22862871Skris	printf("%s", ct);
2291590Srgrimes}
2301590Srgrimes
2311590Srgrimes/*
23291536Siedowse * doentry --
23391536Siedowse *	process a single wtmp entry
23491536Siedowse */
23591536Siedowsevoid
23691536Siedowsedoentry(bp)
23791536Siedowse	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)
28991536Siedowse			err(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
32491536Siedowseprintentry(bp, tt)
32591536Siedowse	struct utmp *bp;
32691536Siedowse	struct ttytab *tt;
32791536Siedowse{
32891536Siedowse	char ct[80];
32991536Siedowse	struct tm *tm;
33091536Siedowse	time_t	delta;				/* time difference */
33191536Siedowse	time_t	t;
33291536Siedowse
33391538Siedowse	if (maxrec != -1 && !maxrec--)
33491538Siedowse		exit(0);
33591536Siedowse	t = _int_to_time(bp->ut_time);
33691536Siedowse	tm = localtime(&t);
33791536Siedowse	(void) strftime(ct, sizeof(ct), d_first ? "%a %e %b %R" :
33891536Siedowse	     "%a %b %e %R", tm);
33991536Siedowse	printf("%-*.*s %-*.*s %-*.*s %s%c",
34091536Siedowse	    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
34191536Siedowse	    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
34291536Siedowse	    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
34391536Siedowse	    ct, tt == NULL ? '\n' : ' ');
34491536Siedowse	if (tt == NULL)
34591536Siedowse		return;
34691536Siedowse	if (!tt->logout) {
34791536Siedowse		puts("  still logged in");
34891536Siedowse		return;
34991536Siedowse	}
35091536Siedowse	if (tt->logout < 0) {
35191536Siedowse		tt->logout = -tt->logout;
35291536Siedowse		printf("- %s", crmsg);
35391536Siedowse	} else {
35491536Siedowse		tm = localtime(&tt->logout);
35591536Siedowse		(void) strftime(ct, sizeof(ct), "%R", tm);
35691536Siedowse		printf("- %s", ct);
35791536Siedowse	}
35891536Siedowse	delta = tt->logout - bp->ut_time;
35991536Siedowse	if (sflag) {
36091536Siedowse		printf("  (%8ld)\n", (long)delta);
36191536Siedowse	} else {
36291536Siedowse		tm = gmtime(&delta);
36391536Siedowse		(void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm);
36491536Siedowse		if (delta < 86400)
36591536Siedowse			printf("  (%s)\n", ct);
36691536Siedowse		else
36791536Siedowse			printf(" (%ld+%s)\n", (long)delta / 86400, ct);
36891536Siedowse	}
36991536Siedowse}
37091536Siedowse
37191536Siedowse/*
3721590Srgrimes * want --
3731590Srgrimes *	see if want this entry
3741590Srgrimes */
3751590Srgrimesint
37611547Sdgwant(bp)
3771590Srgrimes	struct utmp *bp;
3781590Srgrimes{
3791590Srgrimes	ARG *step;
3801590Srgrimes
38177291Sdd	if (snaptime)
38277291Sdd		return (NO);
38377291Sdd
3841590Srgrimes	if (!arglist)
3851590Srgrimes		return (YES);
3861590Srgrimes
3871590Srgrimes	for (step = arglist; step; step = step->next)
3881590Srgrimes		switch(step->type) {
3891590Srgrimes		case HOST_TYPE:
3901590Srgrimes			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
3911590Srgrimes				return (YES);
3921590Srgrimes			break;
3931590Srgrimes		case TTY_TYPE:
3941590Srgrimes			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
3951590Srgrimes				return (YES);
3961590Srgrimes			break;
3971590Srgrimes		case USER_TYPE:
3981590Srgrimes			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
3991590Srgrimes				return (YES);
4001590Srgrimes			break;
40191536Siedowse		}
4021590Srgrimes	return (NO);
4031590Srgrimes}
4041590Srgrimes
4051590Srgrimes/*
4061590Srgrimes * addarg --
4071590Srgrimes *	add an entry to a linked list of arguments
4081590Srgrimes */
4091590Srgrimesvoid
4101590Srgrimesaddarg(type, arg)
4111590Srgrimes	int type;
4121590Srgrimes	char *arg;
4131590Srgrimes{
4141590Srgrimes	ARG *cur;
4151590Srgrimes
4161590Srgrimes	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
4171590Srgrimes		err(1, "malloc failure");
4181590Srgrimes	cur->next = arglist;
4191590Srgrimes	cur->type = type;
4201590Srgrimes	cur->name = arg;
4211590Srgrimes	arglist = cur;
4221590Srgrimes}
4231590Srgrimes
4241590Srgrimes/*
4251590Srgrimes * hostconv --
4261590Srgrimes *	convert the hostname to search pattern; if the supplied host name
4271590Srgrimes *	has a domain attached that is the same as the current domain, rip
4281590Srgrimes *	off the domain suffix since that's what login(1) does.
4291590Srgrimes */
4301590Srgrimesvoid
4311590Srgrimeshostconv(arg)
4321590Srgrimes	char *arg;
4331590Srgrimes{
4341590Srgrimes	static int first = 1;
4351590Srgrimes	static char *hostdot, name[MAXHOSTNAMELEN];
4361590Srgrimes	char *argdot;
4371590Srgrimes
4381590Srgrimes	if (!(argdot = strchr(arg, '.')))
4391590Srgrimes		return;
4401590Srgrimes	if (first) {
4411590Srgrimes		first = 0;
4421590Srgrimes		if (gethostname(name, sizeof(name)))
4431590Srgrimes			err(1, "gethostname");
4441590Srgrimes		hostdot = strchr(name, '.');
4451590Srgrimes	}
4461590Srgrimes	if (hostdot && !strcasecmp(hostdot, argdot))
4471590Srgrimes		*argdot = '\0';
4481590Srgrimes}
4491590Srgrimes
4501590Srgrimes/*
4511590Srgrimes * ttyconv --
4521590Srgrimes *	convert tty to correct name.
4531590Srgrimes */
4541590Srgrimeschar *
4551590Srgrimesttyconv(arg)
4561590Srgrimes	char *arg;
4571590Srgrimes{
4581590Srgrimes	char *mval;
4591590Srgrimes
4601590Srgrimes	/*
4611590Srgrimes	 * kludge -- we assume that all tty's end with
4621590Srgrimes	 * a two character suffix.
4631590Srgrimes	 */
4641590Srgrimes	if (strlen(arg) == 2) {
4651590Srgrimes		/* either 6 for "ttyxx" or 8 for "console" */
4661590Srgrimes		if (!(mval = malloc((u_int)8)))
4671590Srgrimes			err(1, "malloc failure");
4681590Srgrimes		if (!strcmp(arg, "co"))
4691590Srgrimes			(void)strcpy(mval, "console");
4701590Srgrimes		else {
4711590Srgrimes			(void)strcpy(mval, "tty");
4721590Srgrimes			(void)strcpy(mval + 3, arg);
4731590Srgrimes		}
4741590Srgrimes		return (mval);
4751590Srgrimes	}
4761590Srgrimes	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
4771590Srgrimes		return (arg + 5);
4781590Srgrimes	return (arg);
4791590Srgrimes}
4801590Srgrimes
4811590Srgrimes/*
48277291Sdd * dateconv --
48377291Sdd * 	Convert the snapshot time in command line given in the format
48477291Sdd * 	[[CC]YY]MMDDhhmm[.SS]] to a time_t.
48577291Sdd * 	Derived from atime_arg1() in usr.bin/touch/touch.c
48677291Sdd */
48777291Sddtime_t
48877291Sdddateconv(arg)
48977291Sdd        char *arg;
49077291Sdd{
49177291Sdd        time_t timet;
49277291Sdd        struct tm *t;
49377291Sdd        int yearset;
49477291Sdd        char *p;
49577291Sdd
49677291Sdd        /* Start with the current time. */
49777291Sdd        if (time(&timet) < 0)
49877291Sdd                err(1, "time");
49977291Sdd        if ((t = localtime(&timet)) == NULL)
50077291Sdd                err(1, "localtime");
50177291Sdd
50277291Sdd        /* [[CC]YY]MMDDhhmm[.SS] */
50377291Sdd        if ((p = strchr(arg, '.')) == NULL)
50477291Sdd                t->tm_sec = 0; 		/* Seconds defaults to 0. */
50577291Sdd        else {
50677291Sdd                if (strlen(p + 1) != 2)
50777291Sdd                        goto terr;
50877291Sdd                *p++ = '\0';
50977291Sdd                t->tm_sec = ATOI2(p);
51077291Sdd        }
51177291Sdd
51277291Sdd        yearset = 0;
51377291Sdd        switch (strlen(arg)) {
51477291Sdd        case 12:                	/* CCYYMMDDhhmm */
51577291Sdd                t->tm_year = ATOI2(arg);
51677291Sdd                t->tm_year *= 100;
51777291Sdd                yearset = 1;
51877291Sdd                /* FALLTHOUGH */
51977291Sdd        case 10:                	/* YYMMDDhhmm */
52077291Sdd                if (yearset) {
52177291Sdd                        yearset = ATOI2(arg);
52277291Sdd                        t->tm_year += yearset;
52377291Sdd                } else {
52477291Sdd                        yearset = ATOI2(arg);
52577291Sdd                        if (yearset < 69)
52677291Sdd                                t->tm_year = yearset + 2000;
52777291Sdd                        else
52877291Sdd                                t->tm_year = yearset + 1900;
52977291Sdd                }
53077291Sdd                t->tm_year -= 1900;     /* Convert to UNIX time. */
53177291Sdd                /* FALLTHROUGH */
53277291Sdd        case 8:				/* MMDDhhmm */
53377291Sdd                t->tm_mon = ATOI2(arg);
53477291Sdd                --t->tm_mon;    	/* Convert from 01-12 to 00-11 */
53577291Sdd                t->tm_mday = ATOI2(arg);
53677291Sdd                t->tm_hour = ATOI2(arg);
53777291Sdd                t->tm_min = ATOI2(arg);
53877291Sdd                break;
53977291Sdd        case 4:				/* hhmm */
54077291Sdd                t->tm_hour = ATOI2(arg);
54177291Sdd                t->tm_min = ATOI2(arg);
54277291Sdd                break;
54377291Sdd        default:
54477291Sdd                goto terr;
54577291Sdd        }
54677291Sdd        t->tm_isdst = -1;       	/* Figure out DST. */
54777291Sdd        timet = mktime(t);
54877291Sdd        if (timet == -1)
54977291Sddterr:           errx(1,
55077291Sdd        "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
55177291Sdd        return timet;
55277291Sdd}
55377291Sdd
55477291Sdd
55577291Sdd/*
5561590Srgrimes * onintr --
5571590Srgrimes *	on interrupt, we inform the user how far we've gotten
5581590Srgrimes */
5591590Srgrimesvoid
5601590Srgrimesonintr(signo)
5611590Srgrimes	int signo;
5621590Srgrimes{
56316438Sache	char ct[80];
56416438Sache	struct tm *tm;
56589572Sdillon	time_t t = _int_to_time(buf[0].ut_time);
5661590Srgrimes
56785648Sdillon	tm = localtime(&t);
56874588Sache	(void) strftime(ct, sizeof(ct),
56974588Sache			d_first ? "%a %e %b %R" : "%a %b %e %R",
57074588Sache			tm);
57174588Sache	printf("\ninterrupted %s\n", ct);
5721590Srgrimes	if (signo == SIGINT)
5731590Srgrimes		exit(1);
5741590Srgrimes	(void)fflush(stdout);			/* fix required for rsh */
5751590Srgrimes}
576