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