xref: /dragonfly/usr.bin/last/last.c (revision c6f73aab)
1 /*	@(#)last.c	8.2 (Berkeley) 4/2/94 */
2 /*	$NetBSD: last.c,v 1.15 2000/06/30 06:19:58 simonb 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.
16  * 3. Neither the name of the University nor the names of its contributors
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 #include <sys/param.h>
34 #include <sys/stat.h>
35 
36 #include <err.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <tzfile.h>
45 #include <unistd.h>
46 #ifdef SUPPORT_UTMPX
47 #include <utmpx.h>
48 #endif
49 #ifdef SUPPORT_UTMP
50 #include <utmp.h>
51 #endif
52 
53 #ifndef UT_NAMESIZE
54 #define UT_NAMESIZE 8
55 #define UT_LINESIZE 8
56 #define UT_HOSTSIZE 16
57 #endif
58 #ifndef SIGNATURE
59 #define SIGNATURE -1
60 #endif
61 
62 
63 
64 #define	NO	0			/* false/no */
65 #define	YES	1			/* true/yes */
66 
67 #define	TBUFLEN	30			/* length of time string buffer */
68 #define	TFMT	"%a %b %d %R"		/* strftime format string */
69 #define	LTFMT	"%a %b %d %Y %T"	/* strftime long format string */
70 #define	TFMTS	"%R"			/* strftime format string - time only */
71 #define	LTFMTS	"%T"			/* strftime long format string - " */
72 
73 /* fmttime() flags */
74 #define	FULLTIME	0x1		/* show year, seconds */
75 #define	TIMEONLY	0x2		/* show time only, not date */
76 #define	GMT		0x4		/* show time at GMT, for offsets only */
77 
78 #define MAXUTMP		1024;
79 
80 typedef struct arg {
81 	char	*name;			/* argument */
82 #define	HOST_TYPE	-2
83 #define	TTY_TYPE	-3
84 #define	USER_TYPE	-4
85 	int	type;			/* type of arg */
86 	struct arg	*next;		/* linked list pointer */
87 } ARG;
88 static ARG	*arglist;		/* head of linked list */
89 
90 typedef struct ttytab {
91 	time_t	logout;			/* log out time */
92 	char	tty[128];		/* terminal name */
93 	struct ttytab	*next;		/* linked list pointer */
94 } TTY;
95 static TTY	*ttylist;		/* head of linked list */
96 
97 static time_t	currentout;		/* current logout value */
98 static long	maxrec;			/* records to display */
99 static int	fulltime = 0;		/* Display seconds? */
100 
101 static void	 addarg(int, char *);
102 static TTY	*addtty(const char *);
103 static void	 hostconv(char *);
104 static char	*ttyconv(char *);
105 #ifdef SUPPORT_UTMPX
106 static void	 wtmpx(const char *, int, int, int);
107 #endif
108 #ifdef SUPPORT_UTMP
109 static void	 wtmp(const char *, int, int, int);
110 #endif
111 static char	*fmttime(time_t, int);
112 static void	 usage(void);
113 
114 static
115 void usage(void)
116 {
117 	(void)fprintf(stderr, "Usage: %s [-#%s] [-f file] [-t tty]"
118 	    " [-h hostname] [-T] [user ...]\n", getprogname(),
119 #ifdef SUPPORT_UTMPX
120 	    "w"
121 #else
122 	    ""
123 #endif
124 	);
125 	exit(1);
126 }
127 
128 int
129 main(int argc, char *argv[])
130 {
131 	int ch;
132 	char *p;
133 	const char *file = NULL;
134 	int namesize = UT_NAMESIZE;
135 	int linesize = UT_LINESIZE;
136 	int hostsize = UT_HOSTSIZE;
137 
138 	maxrec = -1;
139 
140 	while ((ch = getopt(argc, argv, "0123456789f:h:H:L:N:t:T")) != -1)
141 		switch (ch) {
142 		case '0': case '1': case '2': case '3': case '4':
143 		case '5': case '6': case '7': case '8': case '9':
144 			/*
145 			 * kludge: last was originally designed to take
146 			 * a number after a dash.
147 			 */
148 			if (maxrec == -1) {
149 				p = argv[optind - 1];
150 				if (p[0] == '-' && p[1] == ch && !p[2])
151 					maxrec = atol(++p);
152 				else
153 					maxrec = atol(argv[optind] + 1);
154 				if (!maxrec)
155 					exit(0);
156 			}
157 			break;
158 		case 'f':
159 			file = optarg;
160 			break;
161 		case 'h':
162 			hostconv(optarg);
163 			addarg(HOST_TYPE, optarg);
164 			break;
165 		case 't':
166 			addarg(TTY_TYPE, ttyconv(optarg));
167 			break;
168 		case 'U':
169 			namesize = atoi(optarg);
170 			break;
171 		case 'L':
172 			linesize = atoi(optarg);
173 			break;
174 		case 'H':
175 			hostsize = atoi(optarg);
176 			break;
177 		case 'T':
178 			fulltime = 1;
179 			break;
180 		case '?':
181 		default:
182 			usage();
183 		}
184 
185 	if (argc) {
186 		setlinebuf(stdout);
187 		for (argv += optind; *argv; ++argv) {
188 #define	COMPATIBILITY
189 #ifdef	COMPATIBILITY
190 			/* code to allow "last p5" to work */
191 			addarg(TTY_TYPE, ttyconv(*argv));
192 #endif
193 			addarg(USER_TYPE, *argv);
194 		}
195 	}
196 	if (file == NULL) {
197 		/* XXX: for now, default to utmp */
198 #ifdef SUPPORT_UTMP
199 		if (access(_PATH_WTMP, R_OK) == 0)
200 			file = _PATH_WTMP;
201 		else
202 #endif
203 #ifdef SUPPORT_UTMPX
204 		if (access(_PATH_WTMPX, R_OK) == 0)
205 			file = _PATH_WTMPX;
206 #endif
207 		if (file == NULL)
208 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
209 			errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
210 			    _PATH_WTMP);
211 #elif defined(SUPPORT_UTMPX)
212 			errx(1, "Cannot access `%s'", _PATH_WTMPX);
213 #elif defined(SUPPORT_UTMP)
214 			errx(1, "Cannot access `%s'", _PATH_WTMP);
215 #else
216 			errx(1, "No utmp or utmpx support compiled in.");
217 #endif
218 	}
219 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
220 	if (file[strlen(file) - 1] == 'x')
221 		wtmpx(file, namesize, linesize, hostsize);
222 	else
223 		wtmp(file, namesize, linesize, hostsize);
224 #elif defined(SUPPORT_UTMPX)
225 	wtmpx(file, namesize, linesize, hostsize);
226 #elif defined(SUPPORT_UTMP)
227 	wtmp(file, namesize, linesize, hostsize);
228 #else
229 	errx(1, "Nu utmp or utmpx support compiled in.");
230 #endif
231 	exit(0);
232 }
233 
234 
235 /*
236  * addarg --
237  *	add an entry to a linked list of arguments
238  */
239 static void
240 addarg(int type, char *arg)
241 {
242 	ARG *cur;
243 
244 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
245 		err(1, "malloc failure");
246 	cur->next = arglist;
247 	cur->type = type;
248 	cur->name = arg;
249 	arglist = cur;
250 }
251 
252 /*
253  * addtty --
254  *	add an entry to a linked list of ttys
255  */
256 static TTY *
257 addtty(const char *tty)
258 {
259 	TTY *cur;
260 
261 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
262 		err(1, "malloc failure");
263 	cur->next = ttylist;
264 	cur->logout = currentout;
265 	memmove(cur->tty, tty, sizeof(cur->tty));
266 	return (ttylist = cur);
267 }
268 
269 /*
270  * hostconv --
271  *	convert the hostname to search pattern; if the supplied host name
272  *	has a domain attached that is the same as the current domain, rip
273  *	off the domain suffix since that's what login(1) does.
274  */
275 static void
276 hostconv(char *arg)
277 {
278 	static int first = 1;
279 	static char *hostdot, name[MAXHOSTNAMELEN + 1];
280 	char *argdot;
281 
282 	if (!(argdot = strchr(arg, '.')))
283 		return;
284 	if (first) {
285 		first = 0;
286 		if (gethostname(name, sizeof(name)))
287 			err(1, "gethostname");
288 		name[sizeof(name) - 1] = '\0';
289 		hostdot = strchr(name, '.');
290 	}
291 	if (hostdot && !strcasecmp(hostdot, argdot))
292 		*argdot = '\0';
293 }
294 
295 /*
296  * ttyconv --
297  *	convert tty to correct name.
298  */
299 static char *
300 ttyconv(char *arg)
301 {
302 	char *mval;
303 
304 	/*
305 	 * kludge -- we assume that all tty's end with
306 	 * a two character suffix.
307 	 */
308 	if (strlen(arg) == 2) {
309 		/* either 6 for "ttyxx" or 8 for "console" */
310 		if (!(mval = malloc((u_int)8)))
311 			err(1, "malloc failure");
312 		if (!strcmp(arg, "co"))
313 			(void)strcpy(mval, "console");
314 		else {
315 			(void)strcpy(mval, "tty");
316 			(void)strcpy(mval + 3, arg);
317 		}
318 		return (mval);
319 	}
320 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
321 		return (arg + 5);
322 	return (arg);
323 }
324 
325 /*
326  * fmttime --
327  *	return pointer to (static) formatted time string.
328  */
329 static char *
330 fmttime(time_t t, int flags)
331 {
332 	struct tm *tm;
333 	static char tbuf[TBUFLEN];
334 
335 	tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
336 	strftime(tbuf, sizeof(tbuf),
337 	    (flags & TIMEONLY)
338 	     ? (flags & FULLTIME ? LTFMTS : TFMTS)
339 	     : (flags & FULLTIME ? LTFMT : TFMT),
340 	    tm);
341 	return (tbuf);
342 }
343 
344 #ifdef SUPPORT_UTMP
345 #define TYPE(a)	0
346 #define NAMESIZE UT_NAMESIZE
347 #define LINESIZE UT_LINESIZE
348 #define HOSTSIZE UT_HOSTSIZE
349 #define ut_timefld ut_time
350 #define FIRSTVALID 0
351 #include "want.c"
352 #undef TYPE
353 #undef NAMESIZE
354 #undef LINESIZE
355 #undef HOSTSIZE
356 #undef ut_timefld
357 #undef FIRSTVALID
358 #endif
359 
360 #ifdef SUPPORT_UTMPX
361 #define utmp utmpx
362 #define want wantx
363 #define wtmp wtmpx
364 #define buf bufx
365 #define onintr onintrx
366 #define TYPE(a) (a)->ut_type
367 #define NAMESIZE UTX_USERSIZE
368 #define LINESIZE UTX_LINESIZE
369 #define HOSTSIZE UTX_HOSTSIZE
370 #define ut_timefld ut_xtime
371 #define FIRSTVALID 1
372 #include "want.c"
373 #endif
374