xref: /original-bsd/usr.bin/last/last.c (revision e58c8952)
1 /*
2  * Copyright (c) 1987, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1987, 1993, 1994\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)last.c	8.2 (Berkeley) 04/02/94";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 
21 #include <err.h>
22 #include <fcntl.h>
23 #include <paths.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <tzfile.h>
30 #include <unistd.h>
31 #include <utmp.h>
32 
33 #define	NO	0				/* false/no */
34 #define	YES	1				/* true/yes */
35 
36 static struct utmp	buf[1024];		/* utmp read buffer */
37 
38 typedef struct arg {
39 	char	*name;				/* argument */
40 #define	HOST_TYPE	-2
41 #define	TTY_TYPE	-3
42 #define	USER_TYPE	-4
43 	int	type;				/* type of arg */
44 	struct arg	*next;			/* linked list pointer */
45 } ARG;
46 ARG	*arglist;				/* head of linked list */
47 
48 typedef struct ttytab {
49 	long	logout;				/* log out time */
50 	char	tty[UT_LINESIZE + 1];		/* terminal name */
51 	struct ttytab	*next;			/* linked list pointer */
52 } TTY;
53 TTY	*ttylist;				/* head of linked list */
54 
55 static long	currentout,			/* current logout value */
56 		maxrec;				/* records to display */
57 static char	*file = _PATH_WTMP;		/* wtmp file */
58 
59 void	 addarg __P((int, char *));
60 TTY	*addtty __P((char *));
61 void	 hostconv __P((char *));
62 void	 onintr __P((int));
63 char	*ttyconv __P((char *));
64 int	 want __P((struct utmp *, int));
65 void	 wtmp __P((void));
66 
67 int
68 main(argc, argv)
69 	int argc;
70 	char *argv[];
71 {
72 	extern int optind;
73 	extern char *optarg;
74 	int ch;
75 	char *p;
76 
77 	maxrec = -1;
78 	while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF)
79 		switch (ch) {
80 		case '0': case '1': case '2': case '3': case '4':
81 		case '5': case '6': case '7': case '8': case '9':
82 			/*
83 			 * kludge: last was originally designed to take
84 			 * a number after a dash.
85 			 */
86 			if (maxrec == -1) {
87 				p = argv[optind - 1];
88 				if (p[0] == '-' && p[1] == ch && !p[2])
89 					maxrec = atol(++p);
90 				else
91 					maxrec = atol(argv[optind] + 1);
92 				if (!maxrec)
93 					exit(0);
94 			}
95 			break;
96 		case 'f':
97 			file = optarg;
98 			break;
99 		case 'h':
100 			hostconv(optarg);
101 			addarg(HOST_TYPE, optarg);
102 			break;
103 		case 't':
104 			addarg(TTY_TYPE, ttyconv(optarg));
105 			break;
106 		case '?':
107 		default:
108 			(void)fprintf(stderr,
109 	"usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n");
110 			exit(1);
111 		}
112 
113 	if (argc) {
114 		setlinebuf(stdout);
115 		for (argv += optind; *argv; ++argv) {
116 #define	COMPATIBILITY
117 #ifdef	COMPATIBILITY
118 			/* code to allow "last p5" to work */
119 			addarg(TTY_TYPE, ttyconv(*argv));
120 #endif
121 			addarg(USER_TYPE, *argv);
122 		}
123 	}
124 	wtmp();
125 	exit(0);
126 }
127 
128 /*
129  * wtmp --
130  *	read through the wtmp file
131  */
132 void
133 wtmp()
134 {
135 	struct utmp	*bp;			/* current structure */
136 	TTY	*T;				/* tty list entry */
137 	struct stat	stb;			/* stat of file for size */
138 	long	bl, delta;			/* time difference */
139 	int	bytes, wfd;
140 	char	*ct, *crmsg;
141 
142 	if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
143 		err(1, "%s", file);
144 	bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
145 
146 	(void)time(&buf[0].ut_time);
147 	(void)signal(SIGINT, onintr);
148 	(void)signal(SIGQUIT, onintr);
149 
150 	while (--bl >= 0) {
151 		if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 ||
152 		    (bytes = read(wfd, buf, sizeof(buf))) == -1)
153 			err(1, "%s", file);
154 		for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
155 			/*
156 			 * if the terminal line is '~', the machine stopped.
157 			 * see utmp(5) for more info.
158 			 */
159 			if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
160 				/* everybody just logged out */
161 				for (T = ttylist; T; T = T->next)
162 					T->logout = -bp->ut_time;
163 				currentout = -bp->ut_time;
164 				crmsg = strncmp(bp->ut_name, "shutdown",
165 				    UT_NAMESIZE) ? "crash" : "shutdown";
166 				if (want(bp, NO)) {
167 					ct = ctime(&bp->ut_time);
168 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
169 					    UT_NAMESIZE, UT_NAMESIZE,
170 					    bp->ut_name, UT_LINESIZE,
171 					    UT_LINESIZE, bp->ut_line,
172 					    UT_HOSTSIZE, UT_HOSTSIZE,
173 					    bp->ut_host, ct, ct + 11);
174 					if (maxrec != -1 && !--maxrec)
175 						return;
176 				}
177 				continue;
178 			}
179 			/*
180 			 * if the line is '{' or '|', date got set; see
181 			 * utmp(5) for more info.
182 			 */
183 			if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
184 			    && !bp->ut_line[1]) {
185 				if (want(bp, NO)) {
186 					ct = ctime(&bp->ut_time);
187 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s \n",
188 				    UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
189 				    UT_LINESIZE, UT_LINESIZE, bp->ut_line,
190 				    UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
191 				    ct, ct + 11);
192 					if (maxrec && !--maxrec)
193 						return;
194 				}
195 				continue;
196 			}
197 			/* find associated tty */
198 			for (T = ttylist;; T = T->next) {
199 				if (!T) {
200 					/* add new one */
201 					T = addtty(bp->ut_line);
202 					break;
203 				}
204 				if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
205 					break;
206 			}
207 			if (bp->ut_name[0] && want(bp, YES)) {
208 				ct = ctime(&bp->ut_time);
209 				printf("%-*.*s  %-*.*s %-*.*s %10.10s %5.5s ",
210 				UT_NAMESIZE, UT_NAMESIZE, bp->ut_name,
211 				UT_LINESIZE, UT_LINESIZE, bp->ut_line,
212 				UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host,
213 				ct, ct + 11);
214 				if (!T->logout)
215 					puts("  still logged in");
216 				else {
217 					if (T->logout < 0) {
218 						T->logout = -T->logout;
219 						printf("- %s", crmsg);
220 					}
221 					else
222 						printf("- %5.5s",
223 						    ctime(&T->logout)+11);
224 					delta = T->logout - bp->ut_time;
225 					if (delta < SECSPERDAY)
226 						printf("  (%5.5s)\n",
227 						    asctime(gmtime(&delta))+11);
228 					else
229 						printf(" (%ld+%5.5s)\n",
230 						    delta / SECSPERDAY,
231 						    asctime(gmtime(&delta))+11);
232 				}
233 				if (maxrec != -1 && !--maxrec)
234 					return;
235 			}
236 			T->logout = bp->ut_time;
237 		}
238 	}
239 	ct = ctime(&buf[0].ut_time);
240 	printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
241 }
242 
243 /*
244  * want --
245  *	see if want this entry
246  */
247 int
248 want(bp, check)
249 	struct utmp *bp;
250 	int check;
251 {
252 	ARG *step;
253 
254 	if (check)
255 		/*
256 		 * when uucp and ftp log in over a network, the entry in
257 		 * the utmp file is the name plus their process id.  See
258 		 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
259 		 */
260 		if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
261 			bp->ut_line[3] = '\0';
262 		else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
263 			bp->ut_line[4] = '\0';
264 	if (!arglist)
265 		return (YES);
266 
267 	for (step = arglist; step; step = step->next)
268 		switch(step->type) {
269 		case HOST_TYPE:
270 			if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
271 				return (YES);
272 			break;
273 		case TTY_TYPE:
274 			if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
275 				return (YES);
276 			break;
277 		case USER_TYPE:
278 			if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
279 				return (YES);
280 			break;
281 	}
282 	return (NO);
283 }
284 
285 /*
286  * addarg --
287  *	add an entry to a linked list of arguments
288  */
289 void
290 addarg(type, arg)
291 	int type;
292 	char *arg;
293 {
294 	ARG *cur;
295 
296 	if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
297 		err(1, "malloc failure");
298 	cur->next = arglist;
299 	cur->type = type;
300 	cur->name = arg;
301 	arglist = cur;
302 }
303 
304 /*
305  * addtty --
306  *	add an entry to a linked list of ttys
307  */
308 TTY *
309 addtty(ttyname)
310 	char *ttyname;
311 {
312 	TTY *cur;
313 
314 	if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
315 		err(1, "malloc failure");
316 	cur->next = ttylist;
317 	cur->logout = currentout;
318 	memmove(cur->tty, ttyname, UT_LINESIZE);
319 	return (ttylist = cur);
320 }
321 
322 /*
323  * hostconv --
324  *	convert the hostname to search pattern; if the supplied host name
325  *	has a domain attached that is the same as the current domain, rip
326  *	off the domain suffix since that's what login(1) does.
327  */
328 void
329 hostconv(arg)
330 	char *arg;
331 {
332 	static int first = 1;
333 	static char *hostdot, name[MAXHOSTNAMELEN];
334 	char *argdot;
335 
336 	if (!(argdot = strchr(arg, '.')))
337 		return;
338 	if (first) {
339 		first = 0;
340 		if (gethostname(name, sizeof(name)))
341 			err(1, "gethostname");
342 		hostdot = strchr(name, '.');
343 	}
344 	if (hostdot && !strcasecmp(hostdot, argdot))
345 		*argdot = '\0';
346 }
347 
348 /*
349  * ttyconv --
350  *	convert tty to correct name.
351  */
352 char *
353 ttyconv(arg)
354 	char *arg;
355 {
356 	char *mval;
357 
358 	/*
359 	 * kludge -- we assume that all tty's end with
360 	 * a two character suffix.
361 	 */
362 	if (strlen(arg) == 2) {
363 		/* either 6 for "ttyxx" or 8 for "console" */
364 		if (!(mval = malloc((u_int)8)))
365 			err(1, "malloc failure");
366 		if (!strcmp(arg, "co"))
367 			(void)strcpy(mval, "console");
368 		else {
369 			(void)strcpy(mval, "tty");
370 			(void)strcpy(mval + 3, arg);
371 		}
372 		return (mval);
373 	}
374 	if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
375 		return (arg + 5);
376 	return (arg);
377 }
378 
379 /*
380  * onintr --
381  *	on interrupt, we inform the user how far we've gotten
382  */
383 void
384 onintr(signo)
385 	int signo;
386 {
387 	char *ct;
388 
389 	ct = ctime(&buf[0].ut_time);
390 	printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
391 	if (signo == SIGINT)
392 		exit(1);
393 	(void)fflush(stdout);			/* fix required for rsh */
394 }
395