xref: /minix3/usr.bin/last/last.c (revision a06e2ab3959428ebf7e32b1cde13e386df9115ab)
1*a06e2ab3SBen Gras /*	$NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $	*/
2*a06e2ab3SBen Gras 
3*a06e2ab3SBen Gras /*
4*a06e2ab3SBen Gras  * Copyright (c) 1987, 1993, 1994
5*a06e2ab3SBen Gras  *	The Regents of the University of California.  All rights reserved.
6*a06e2ab3SBen Gras  *
7*a06e2ab3SBen Gras  * Redistribution and use in source and binary forms, with or without
8*a06e2ab3SBen Gras  * modification, are permitted provided that the following conditions
9*a06e2ab3SBen Gras  * are met:
10*a06e2ab3SBen Gras  * 1. Redistributions of source code must retain the above copyright
11*a06e2ab3SBen Gras  *    notice, this list of conditions and the following disclaimer.
12*a06e2ab3SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*a06e2ab3SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*a06e2ab3SBen Gras  *    documentation and/or other materials provided with the distribution.
15*a06e2ab3SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*a06e2ab3SBen Gras  *    may be used to endorse or promote products derived from this software
17*a06e2ab3SBen Gras  *    without specific prior written permission.
18*a06e2ab3SBen Gras  *
19*a06e2ab3SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*a06e2ab3SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*a06e2ab3SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*a06e2ab3SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*a06e2ab3SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*a06e2ab3SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*a06e2ab3SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*a06e2ab3SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*a06e2ab3SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*a06e2ab3SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*a06e2ab3SBen Gras  * SUCH DAMAGE.
30*a06e2ab3SBen Gras  */
31*a06e2ab3SBen Gras 
32*a06e2ab3SBen Gras #include <sys/cdefs.h>
33*a06e2ab3SBen Gras #ifndef lint
34*a06e2ab3SBen Gras __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
35*a06e2ab3SBen Gras  The Regents of the University of California.  All rights reserved.");
36*a06e2ab3SBen Gras #endif /* not lint */
37*a06e2ab3SBen Gras 
38*a06e2ab3SBen Gras #ifndef lint
39*a06e2ab3SBen Gras #if 0
40*a06e2ab3SBen Gras static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 4/2/94";
41*a06e2ab3SBen Gras #endif
42*a06e2ab3SBen Gras __RCSID("$NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $");
43*a06e2ab3SBen Gras #endif /* not lint */
44*a06e2ab3SBen Gras 
45*a06e2ab3SBen Gras #include <sys/param.h>
46*a06e2ab3SBen Gras #include <sys/stat.h>
47*a06e2ab3SBen Gras 
48*a06e2ab3SBen Gras #include <err.h>
49*a06e2ab3SBen Gras #include <errno.h>
50*a06e2ab3SBen Gras #include <fcntl.h>
51*a06e2ab3SBen Gras #include <paths.h>
52*a06e2ab3SBen Gras #include <stdio.h>
53*a06e2ab3SBen Gras #include <stdlib.h>
54*a06e2ab3SBen Gras #include <string.h>
55*a06e2ab3SBen Gras #include <time.h>
56*a06e2ab3SBen Gras #include <tzfile.h>
57*a06e2ab3SBen Gras #include <unistd.h>
58*a06e2ab3SBen Gras #include <arpa/inet.h>
59*a06e2ab3SBen Gras #ifdef SUPPORT_UTMPX
60*a06e2ab3SBen Gras #include <utmpx.h>
61*a06e2ab3SBen Gras #endif
62*a06e2ab3SBen Gras #ifdef SUPPORT_UTMP
63*a06e2ab3SBen Gras #include <utmp.h>
64*a06e2ab3SBen Gras #endif
65*a06e2ab3SBen Gras #include <util.h>
66*a06e2ab3SBen Gras 
67*a06e2ab3SBen Gras #ifndef UT_NAMESIZE
68*a06e2ab3SBen Gras #define UT_NAMESIZE 8
69*a06e2ab3SBen Gras #define UT_LINESIZE 8
70*a06e2ab3SBen Gras #define UT_HOSTSIZE 16
71*a06e2ab3SBen Gras #endif
72*a06e2ab3SBen Gras #ifndef SIGNATURE
73*a06e2ab3SBen Gras #define SIGNATURE -1
74*a06e2ab3SBen Gras #endif
75*a06e2ab3SBen Gras 
76*a06e2ab3SBen Gras 
77*a06e2ab3SBen Gras 
78*a06e2ab3SBen Gras #define	NO	0			/* false/no */
79*a06e2ab3SBen Gras #define	YES	1			/* true/yes */
80*a06e2ab3SBen Gras 
81*a06e2ab3SBen Gras #define	TBUFLEN	30			/* length of time string buffer */
82*a06e2ab3SBen Gras #define	TFMT	"%a %b %d %R"		/* strftime format string */
83*a06e2ab3SBen Gras #define	LTFMT	"%a %b %d %Y %T"	/* strftime long format string */
84*a06e2ab3SBen Gras #define	TFMTS	"%R"			/* strftime format string - time only */
85*a06e2ab3SBen Gras #define	LTFMTS	"%T"			/* strftime long format string - " */
86*a06e2ab3SBen Gras 
87*a06e2ab3SBen Gras /* fmttime() flags */
88*a06e2ab3SBen Gras #define	FULLTIME	0x1		/* show year, seconds */
89*a06e2ab3SBen Gras #define	TIMEONLY	0x2		/* show time only, not date */
90*a06e2ab3SBen Gras #define	GMT		0x4		/* show time at GMT, for offsets only */
91*a06e2ab3SBen Gras 
92*a06e2ab3SBen Gras #define MAXUTMP		1024
93*a06e2ab3SBen Gras 
94*a06e2ab3SBen Gras typedef struct arg {
95*a06e2ab3SBen Gras 	const char	*name;		/* argument */
96*a06e2ab3SBen Gras #define	HOST_TYPE	-2
97*a06e2ab3SBen Gras #define	TTY_TYPE	-3
98*a06e2ab3SBen Gras #define	USER_TYPE	-4
99*a06e2ab3SBen Gras 	int		type;		/* type of arg */
100*a06e2ab3SBen Gras 	struct arg	*next;		/* linked list pointer */
101*a06e2ab3SBen Gras } ARG;
102*a06e2ab3SBen Gras static ARG	*arglist;		/* head of linked list */
103*a06e2ab3SBen Gras 
104*a06e2ab3SBen Gras typedef struct ttytab {
105*a06e2ab3SBen Gras 	time_t	logout;			/* log out time */
106*a06e2ab3SBen Gras 	char	tty[128];		/* terminal name */
107*a06e2ab3SBen Gras 	struct ttytab	*next;		/* linked list pointer */
108*a06e2ab3SBen Gras } TTY;
109*a06e2ab3SBen Gras static TTY	*ttylist;		/* head of linked list */
110*a06e2ab3SBen Gras 
111*a06e2ab3SBen Gras static time_t	currentout;		/* current logout value */
112*a06e2ab3SBen Gras static long	maxrec;			/* records to display */
113*a06e2ab3SBen Gras static int	fulltime = 0;		/* Display seconds? */
114*a06e2ab3SBen Gras static int	xflag;			/* Assume file is wtmpx format */
115*a06e2ab3SBen Gras 
116*a06e2ab3SBen Gras static void	 addarg(int, const char *);
117*a06e2ab3SBen Gras static TTY	*addtty(const char *);
118*a06e2ab3SBen Gras static void	 hostconv(char *);
119*a06e2ab3SBen Gras static const char *ttyconv(char *);
120*a06e2ab3SBen Gras #ifdef SUPPORT_UTMPX
121*a06e2ab3SBen Gras static void	 wtmpx(const char *, int, int, int, int);
122*a06e2ab3SBen Gras #endif
123*a06e2ab3SBen Gras #ifdef SUPPORT_UTMP
124*a06e2ab3SBen Gras static void	 wtmp(const char *, int, int, int, int);
125*a06e2ab3SBen Gras #endif
126*a06e2ab3SBen Gras static char	*fmttime(time_t, int);
127*a06e2ab3SBen Gras __dead static void	 usage(void);
128*a06e2ab3SBen Gras 
129*a06e2ab3SBen Gras static
usage(void)130*a06e2ab3SBen Gras void usage(void)
131*a06e2ab3SBen Gras {
132*a06e2ab3SBen Gras 	(void)fprintf(stderr, "usage: %s [-#%s] [-nTx] [-f file]"
133*a06e2ab3SBen Gras 	    " [-H hostsize] [-h host] [-L linesize]\n"
134*a06e2ab3SBen Gras 	    "\t    [-N namesize] [-t tty] [user ...]\n", getprogname(),
135*a06e2ab3SBen Gras #ifdef NOTYET_SUPPORT_UTMPX
136*a06e2ab3SBen Gras 	    "w"
137*a06e2ab3SBen Gras #else
138*a06e2ab3SBen Gras 	    ""
139*a06e2ab3SBen Gras #endif
140*a06e2ab3SBen Gras 	);
141*a06e2ab3SBen Gras 	exit(EXIT_FAILURE);
142*a06e2ab3SBen Gras }
143*a06e2ab3SBen Gras 
144*a06e2ab3SBen Gras int
main(int argc,char * argv[])145*a06e2ab3SBen Gras main(int argc, char *argv[])
146*a06e2ab3SBen Gras {
147*a06e2ab3SBen Gras 	int ch;
148*a06e2ab3SBen Gras 	char *p;
149*a06e2ab3SBen Gras 	const char *file = NULL;
150*a06e2ab3SBen Gras 	int namesize = UT_NAMESIZE;
151*a06e2ab3SBen Gras 	int linesize = UT_LINESIZE;
152*a06e2ab3SBen Gras 	int hostsize = UT_HOSTSIZE;
153*a06e2ab3SBen Gras 	int numeric = 0;
154*a06e2ab3SBen Gras 
155*a06e2ab3SBen Gras 	maxrec = -1;
156*a06e2ab3SBen Gras 
157*a06e2ab3SBen Gras 	while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:x")) != -1)
158*a06e2ab3SBen Gras 		switch (ch) {
159*a06e2ab3SBen Gras 		case '0': case '1': case '2': case '3': case '4':
160*a06e2ab3SBen Gras 		case '5': case '6': case '7': case '8': case '9':
161*a06e2ab3SBen Gras 			/*
162*a06e2ab3SBen Gras 			 * kludge: last was originally designed to take
163*a06e2ab3SBen Gras 			 * a number after a dash.
164*a06e2ab3SBen Gras 			 */
165*a06e2ab3SBen Gras 			if (maxrec == -1) {
166*a06e2ab3SBen Gras 				p = argv[optind - 1];
167*a06e2ab3SBen Gras 				if (p[0] == '-' && p[1] == ch && !p[2])
168*a06e2ab3SBen Gras 					maxrec = atol(++p);
169*a06e2ab3SBen Gras 				else if (optind < argc)
170*a06e2ab3SBen Gras 					maxrec = atol(argv[optind] + 1);
171*a06e2ab3SBen Gras 				else
172*a06e2ab3SBen Gras 					usage();
173*a06e2ab3SBen Gras 				if (!maxrec)
174*a06e2ab3SBen Gras 					return 0;
175*a06e2ab3SBen Gras 			}
176*a06e2ab3SBen Gras 			break;
177*a06e2ab3SBen Gras 		case 'f':
178*a06e2ab3SBen Gras 			file = optarg;
179*a06e2ab3SBen Gras 			if ('\0' == file[0])
180*a06e2ab3SBen Gras 				usage();
181*a06e2ab3SBen Gras 			break;
182*a06e2ab3SBen Gras 		case 'H':
183*a06e2ab3SBen Gras 			hostsize = atoi(optarg);
184*a06e2ab3SBen Gras 			if (hostsize < 1)
185*a06e2ab3SBen Gras 				usage();
186*a06e2ab3SBen Gras 			break;
187*a06e2ab3SBen Gras 		case 'h':
188*a06e2ab3SBen Gras 			hostconv(optarg);
189*a06e2ab3SBen Gras 			addarg(HOST_TYPE, optarg);
190*a06e2ab3SBen Gras 			break;
191*a06e2ab3SBen Gras 		case 'L':
192*a06e2ab3SBen Gras 			linesize = atoi(optarg);
193*a06e2ab3SBen Gras 			if (linesize < 1)
194*a06e2ab3SBen Gras 				usage();
195*a06e2ab3SBen Gras 			break;
196*a06e2ab3SBen Gras 		case 'N':
197*a06e2ab3SBen Gras 			namesize = atoi(optarg);
198*a06e2ab3SBen Gras 			if (namesize < 1)
199*a06e2ab3SBen Gras 				usage();
200*a06e2ab3SBen Gras 			break;
201*a06e2ab3SBen Gras 		case 'n':
202*a06e2ab3SBen Gras 			numeric = 1;
203*a06e2ab3SBen Gras 			break;
204*a06e2ab3SBen Gras 		case 'T':
205*a06e2ab3SBen Gras 			fulltime = 1;
206*a06e2ab3SBen Gras 			break;
207*a06e2ab3SBen Gras 		case 't':
208*a06e2ab3SBen Gras 			addarg(TTY_TYPE, ttyconv(optarg));
209*a06e2ab3SBen Gras 			break;
210*a06e2ab3SBen Gras 		case 'x':
211*a06e2ab3SBen Gras 			xflag = 1;
212*a06e2ab3SBen Gras 			break;
213*a06e2ab3SBen Gras 		case '?':
214*a06e2ab3SBen Gras 		default:
215*a06e2ab3SBen Gras 			usage();
216*a06e2ab3SBen Gras 		}
217*a06e2ab3SBen Gras 
218*a06e2ab3SBen Gras 	if (argc) {
219*a06e2ab3SBen Gras 		setlinebuf(stdout);
220*a06e2ab3SBen Gras 		for (argv += optind; *argv; ++argv) {
221*a06e2ab3SBen Gras #define	COMPATIBILITY
222*a06e2ab3SBen Gras #ifdef	COMPATIBILITY
223*a06e2ab3SBen Gras 			/* code to allow "last p5" to work */
224*a06e2ab3SBen Gras 			addarg(TTY_TYPE, ttyconv(*argv));
225*a06e2ab3SBen Gras #endif
226*a06e2ab3SBen Gras 			addarg(USER_TYPE, *argv);
227*a06e2ab3SBen Gras 		}
228*a06e2ab3SBen Gras 	}
229*a06e2ab3SBen Gras 	if (file == NULL) {
230*a06e2ab3SBen Gras #ifdef SUPPORT_UTMPX
231*a06e2ab3SBen Gras 		if (access(_PATH_WTMPX, R_OK) == 0)
232*a06e2ab3SBen Gras 			file = _PATH_WTMPX;
233*a06e2ab3SBen Gras 		else
234*a06e2ab3SBen Gras #endif
235*a06e2ab3SBen Gras #ifdef SUPPORT_UTMP
236*a06e2ab3SBen Gras 		if (access(_PATH_WTMP, R_OK) == 0)
237*a06e2ab3SBen Gras 			file = _PATH_WTMP;
238*a06e2ab3SBen Gras #endif
239*a06e2ab3SBen Gras 		if (file == NULL)
240*a06e2ab3SBen Gras #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
241*a06e2ab3SBen Gras 			errx(EXIT_FAILURE, "Cannot access `%s' or `%s'", _PATH_WTMPX,
242*a06e2ab3SBen Gras 			    _PATH_WTMP);
243*a06e2ab3SBen Gras #elif defined(SUPPORT_UTMPX)
244*a06e2ab3SBen Gras 			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMPX);
245*a06e2ab3SBen Gras #elif defined(SUPPORT_UTMP)
246*a06e2ab3SBen Gras 			errx(EXIT_FAILURE, "Cannot access `%s'", _PATH_WTMP);
247*a06e2ab3SBen Gras #else
248*a06e2ab3SBen Gras 			errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
249*a06e2ab3SBen Gras #endif
250*a06e2ab3SBen Gras 	}
251*a06e2ab3SBen Gras #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
252*a06e2ab3SBen Gras 	if (file[strlen(file) - 1] == 'x' || xflag)
253*a06e2ab3SBen Gras 		wtmpx(file, namesize, linesize, hostsize, numeric);
254*a06e2ab3SBen Gras 	else
255*a06e2ab3SBen Gras 		wtmp(file, namesize, linesize, hostsize, numeric);
256*a06e2ab3SBen Gras #elif defined(SUPPORT_UTMPX)
257*a06e2ab3SBen Gras 	wtmpx(file, namesize, linesize, hostsize, numeric);
258*a06e2ab3SBen Gras #elif defined(SUPPORT_UTMP)
259*a06e2ab3SBen Gras 	wtmp(file, namesize, linesize, hostsize, numeric);
260*a06e2ab3SBen Gras #else
261*a06e2ab3SBen Gras 	errx(EXIT_FAILURE, "No utmp or utmpx support compiled in.");
262*a06e2ab3SBen Gras #endif
263*a06e2ab3SBen Gras 	exit(EXIT_SUCCESS);
264*a06e2ab3SBen Gras }
265*a06e2ab3SBen Gras 
266*a06e2ab3SBen Gras 
267*a06e2ab3SBen Gras /*
268*a06e2ab3SBen Gras  * addarg --
269*a06e2ab3SBen Gras  *	add an entry to a linked list of arguments
270*a06e2ab3SBen Gras  */
271*a06e2ab3SBen Gras static void
addarg(int type,const char * arg)272*a06e2ab3SBen Gras addarg(int type, const char *arg)
273*a06e2ab3SBen Gras {
274*a06e2ab3SBen Gras 	ARG *cur;
275*a06e2ab3SBen Gras 
276*a06e2ab3SBen Gras 	if (!(cur = (ARG *)malloc(sizeof(ARG))))
277*a06e2ab3SBen Gras 		err(EXIT_FAILURE, "malloc failure");
278*a06e2ab3SBen Gras 	cur->next = arglist;
279*a06e2ab3SBen Gras 	cur->type = type;
280*a06e2ab3SBen Gras 	cur->name = arg;
281*a06e2ab3SBen Gras 	arglist = cur;
282*a06e2ab3SBen Gras }
283*a06e2ab3SBen Gras 
284*a06e2ab3SBen Gras /*
285*a06e2ab3SBen Gras  * addtty --
286*a06e2ab3SBen Gras  *	add an entry to a linked list of ttys
287*a06e2ab3SBen Gras  */
288*a06e2ab3SBen Gras static TTY *
addtty(const char * tty)289*a06e2ab3SBen Gras addtty(const char *tty)
290*a06e2ab3SBen Gras {
291*a06e2ab3SBen Gras 	TTY *cur;
292*a06e2ab3SBen Gras 
293*a06e2ab3SBen Gras 	if (!(cur = (TTY *)malloc(sizeof(TTY))))
294*a06e2ab3SBen Gras 		err(EXIT_FAILURE, "malloc failure");
295*a06e2ab3SBen Gras 	cur->next = ttylist;
296*a06e2ab3SBen Gras 	cur->logout = currentout;
297*a06e2ab3SBen Gras 	memmove(cur->tty, tty, sizeof(cur->tty));
298*a06e2ab3SBen Gras 	return (ttylist = cur);
299*a06e2ab3SBen Gras }
300*a06e2ab3SBen Gras 
301*a06e2ab3SBen Gras /*
302*a06e2ab3SBen Gras  * hostconv --
303*a06e2ab3SBen Gras  *	convert the hostname to search pattern; if the supplied host name
304*a06e2ab3SBen Gras  *	has a domain attached that is the same as the current domain, rip
305*a06e2ab3SBen Gras  *	off the domain suffix since that's what login(1) does.
306*a06e2ab3SBen Gras  */
307*a06e2ab3SBen Gras static void
hostconv(char * arg)308*a06e2ab3SBen Gras hostconv(char *arg)
309*a06e2ab3SBen Gras {
310*a06e2ab3SBen Gras 	static int first = 1;
311*a06e2ab3SBen Gras 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
312*a06e2ab3SBen Gras 	char *argdot;
313*a06e2ab3SBen Gras 
314*a06e2ab3SBen Gras 	if (!(argdot = strchr(arg, '.')))
315*a06e2ab3SBen Gras 		return;
316*a06e2ab3SBen Gras 	if (first) {
317*a06e2ab3SBen Gras 		first = 0;
318*a06e2ab3SBen Gras 		if (gethostname(name, sizeof(name)))
319*a06e2ab3SBen Gras 			err(EXIT_FAILURE, "gethostname");
320*a06e2ab3SBen Gras 		name[sizeof(name) - 1] = '\0';
321*a06e2ab3SBen Gras 		hostdot = strchr(name, '.');
322*a06e2ab3SBen Gras 	}
323*a06e2ab3SBen Gras 	if (hostdot && !strcasecmp(hostdot, argdot))
324*a06e2ab3SBen Gras 		*argdot = '\0';
325*a06e2ab3SBen Gras }
326*a06e2ab3SBen Gras 
327*a06e2ab3SBen Gras /*
328*a06e2ab3SBen Gras  * ttyconv --
329*a06e2ab3SBen Gras  *	convert tty to correct name.
330*a06e2ab3SBen Gras  */
331*a06e2ab3SBen Gras static const char *
ttyconv(char * arg)332*a06e2ab3SBen Gras ttyconv(char *arg)
333*a06e2ab3SBen Gras {
334*a06e2ab3SBen Gras 	char *mval;
335*a06e2ab3SBen Gras 
336*a06e2ab3SBen Gras 	if (!strcmp(arg, "co"))
337*a06e2ab3SBen Gras 		return ("console");
338*a06e2ab3SBen Gras 	/*
339*a06e2ab3SBen Gras 	 * kludge -- we assume that all tty's end with
340*a06e2ab3SBen Gras 	 * a two character suffix.
341*a06e2ab3SBen Gras 	 */
342*a06e2ab3SBen Gras 	if (strlen(arg) == 2) {
343*a06e2ab3SBen Gras 		if (asprintf(&mval, "tty%s", arg) == -1)
344*a06e2ab3SBen Gras 			err(EXIT_FAILURE, "malloc failure");
345*a06e2ab3SBen Gras 		return (mval);
346*a06e2ab3SBen Gras 	}
347*a06e2ab3SBen Gras 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
348*a06e2ab3SBen Gras 		return (&arg[sizeof(_PATH_DEV) - 1]);
349*a06e2ab3SBen Gras 	return (arg);
350*a06e2ab3SBen Gras }
351*a06e2ab3SBen Gras 
352*a06e2ab3SBen Gras /*
353*a06e2ab3SBen Gras  * fmttime --
354*a06e2ab3SBen Gras  *	return pointer to (static) formatted time string.
355*a06e2ab3SBen Gras  */
356*a06e2ab3SBen Gras static char *
fmttime(time_t t,int flags)357*a06e2ab3SBen Gras fmttime(time_t t, int flags)
358*a06e2ab3SBen Gras {
359*a06e2ab3SBen Gras 	struct tm *tm;
360*a06e2ab3SBen Gras 	static char tbuf[TBUFLEN];
361*a06e2ab3SBen Gras 
362*a06e2ab3SBen Gras 	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
363*a06e2ab3SBen Gras 	if (tm == NULL) {
364*a06e2ab3SBen Gras 		strcpy(tbuf, "????");
365*a06e2ab3SBen Gras 		return tbuf;
366*a06e2ab3SBen Gras 	}
367*a06e2ab3SBen Gras 	strftime(tbuf, sizeof(tbuf),
368*a06e2ab3SBen Gras 	    (flags & TIMEONLY)
369*a06e2ab3SBen Gras 	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
370*a06e2ab3SBen Gras 	     : (flags & FULLTIME ? LTFMT : TFMT),
371*a06e2ab3SBen Gras 	    tm);
372*a06e2ab3SBen Gras 	return (tbuf);
373*a06e2ab3SBen Gras }
374*a06e2ab3SBen Gras 
375*a06e2ab3SBen Gras #ifdef SUPPORT_UTMP
376*a06e2ab3SBen Gras #define TYPE(a)	0
377*a06e2ab3SBen Gras #define NAMESIZE UT_NAMESIZE
378*a06e2ab3SBen Gras #define LINESIZE UT_LINESIZE
379*a06e2ab3SBen Gras #define HOSTSIZE UT_HOSTSIZE
380*a06e2ab3SBen Gras #define ut_timefld ut_time
381*a06e2ab3SBen Gras #define HAS_UT_SS 0
382*a06e2ab3SBen Gras #include "want.c"
383*a06e2ab3SBen Gras #undef TYPE /*(a)*/
384*a06e2ab3SBen Gras #undef NAMESIZE
385*a06e2ab3SBen Gras #undef LINESIZE
386*a06e2ab3SBen Gras #undef HOSTSIZE
387*a06e2ab3SBen Gras #undef ut_timefld
388*a06e2ab3SBen Gras #undef HAS_UT_SS
389*a06e2ab3SBen Gras #endif
390*a06e2ab3SBen Gras 
391*a06e2ab3SBen Gras #ifdef SUPPORT_UTMPX
392*a06e2ab3SBen Gras #define utmp utmpx
393*a06e2ab3SBen Gras #define want wantx
394*a06e2ab3SBen Gras #define wtmp wtmpx
395*a06e2ab3SBen Gras #define gethost gethostx
396*a06e2ab3SBen Gras #define buf bufx
397*a06e2ab3SBen Gras #define onintr onintrx
398*a06e2ab3SBen Gras #define TYPE(a) (a)->ut_type
399*a06e2ab3SBen Gras #define NAMESIZE UTX_USERSIZE
400*a06e2ab3SBen Gras #define LINESIZE UTX_LINESIZE
401*a06e2ab3SBen Gras #define HOSTSIZE UTX_HOSTSIZE
402*a06e2ab3SBen Gras #define ut_timefld ut_xtime
403*a06e2ab3SBen Gras #define HAS_UT_SS 1
404*a06e2ab3SBen Gras #include "want.c"
405*a06e2ab3SBen Gras #endif
406