last.c revision 1.2 1 /*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)last.c 5.18 (Berkeley) 3/1/91";*/
42 static char rcsid[] = "$Id: last.c,v 1.2 1993/08/01 18:14:08 mycroft Exp $";
43 #endif /* not lint */
44
45 /*
46 * last
47 */
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <signal.h>
52 #include <time.h>
53 #include <utmp.h>
54 #include <stdio.h>
55 #include <paths.h>
56
57 #define SECDAY (24*60*60) /* seconds in a day */
58 #define NO 0 /* false/no */
59 #define YES 1 /* true/yes */
60
61 static struct utmp buf[1024]; /* utmp read buffer */
62
63 typedef struct arg {
64 char *name; /* argument */
65 #define HOST_TYPE -2
66 #define TTY_TYPE -3
67 #define USER_TYPE -4
68 int type; /* type of arg */
69 struct arg *next; /* linked list pointer */
70 } ARG;
71 ARG *arglist; /* head of linked list */
72
73 typedef struct ttytab {
74 long logout; /* log out time */
75 char tty[UT_LINESIZE + 1]; /* terminal name */
76 struct ttytab *next; /* linked list pointer */
77 } TTY;
78 TTY *ttylist; /* head of linked list */
79
80 static long currentout, /* current logout value */
81 maxrec; /* records to display */
82 static char *file = _PATH_WTMP; /* wtmp file */
83
84 main(argc, argv)
85 int argc;
86 char **argv;
87 {
88 extern int optind;
89 extern char *optarg;
90 int ch;
91 long atol();
92 char *p, *ttyconv();
93
94 maxrec = -1;
95 while ((ch = getopt(argc, argv, "0123456789f:h:t:")) != EOF)
96 switch((char)ch) {
97 case '0': case '1': case '2': case '3': case '4':
98 case '5': case '6': case '7': case '8': case '9':
99 /*
100 * kludge: last was originally designed to take
101 * a number after a dash.
102 */
103 if (maxrec == -1) {
104 p = argv[optind - 1];
105 if (p[0] == '-' && p[1] == ch && !p[2])
106 maxrec = atol(++p);
107 else
108 maxrec = atol(argv[optind] + 1);
109 if (!maxrec)
110 exit(0);
111 }
112 break;
113 case 'f':
114 file = optarg;
115 break;
116 case 'h':
117 hostconv(optarg);
118 addarg(HOST_TYPE, optarg);
119 break;
120 case 't':
121 addarg(TTY_TYPE, ttyconv(optarg));
122 break;
123 case '?':
124 default:
125 fputs("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n", stderr);
126 exit(1);
127 }
128
129 if (argc) {
130 setlinebuf(stdout);
131 for (argv += optind; *argv; ++argv) {
132 #define COMPATIBILITY
133 #ifdef COMPATIBILITY
134 /* code to allow "last p5" to work */
135 addarg(TTY_TYPE, ttyconv(*argv));
136 #endif
137 addarg(USER_TYPE, *argv);
138 }
139 }
140 wtmp();
141 exit(0);
142 }
143
144 /*
145 * wtmp --
146 * read through the wtmp file
147 */
148 wtmp()
149 {
150 register struct utmp *bp; /* current structure */
151 register TTY *T; /* tty list entry */
152 struct stat stb; /* stat of file for size */
153 long bl, delta, /* time difference */
154 lseek(), time();
155 int bytes, wfd;
156 char *ct, *crmsg,
157 *asctime(), *ctime(), *strcpy();
158 TTY *addtty();
159 void onintr();
160
161 if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1) {
162 perror(file);
163 exit(1);
164 }
165 bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
166
167 (void)time(&buf[0].ut_time);
168 (void)signal(SIGINT, onintr);
169 (void)signal(SIGQUIT, onintr);
170
171 while (--bl >= 0) {
172 if (lseek(wfd, (long)(bl * sizeof(buf)), L_SET) == -1 ||
173 (bytes = read(wfd, (char *)buf, sizeof(buf))) == -1) {
174 fprintf(stderr, "last: %s: ", file);
175 perror((char *)NULL);
176 exit(1);
177 }
178 for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) {
179 /*
180 * if the terminal line is '~', the machine stopped.
181 * see utmp(5) for more info.
182 */
183 if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
184 /* everybody just logged out */
185 for (T = ttylist; T; T = T->next)
186 T->logout = -bp->ut_time;
187 currentout = -bp->ut_time;
188 crmsg = strncmp(bp->ut_name, "shutdown",
189 UT_NAMESIZE) ? "crash" : "shutdown";
190 if (want(bp, NO)) {
191 ct = ctime(&bp->ut_time);
192 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
193 if (maxrec != -1 && !--maxrec)
194 return;
195 }
196 continue;
197 }
198 /*
199 * if the line is '{' or '|', date got set; see
200 * utmp(5) for more info.
201 */
202 if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
203 && !bp->ut_line[1]) {
204 if (want(bp, NO)) {
205 ct = ctime(&bp->ut_time);
206 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s \n", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
207 if (maxrec && !--maxrec)
208 return;
209 }
210 continue;
211 }
212 /* find associated tty */
213 for (T = ttylist;; T = T->next) {
214 if (!T) {
215 /* add new one */
216 T = addtty(bp->ut_line);
217 break;
218 }
219 if (!strncmp(T->tty, bp->ut_line, UT_LINESIZE))
220 break;
221 }
222 if (bp->ut_name[0] && want(bp, YES)) {
223 ct = ctime(&bp->ut_time);
224 printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, UT_LINESIZE, UT_LINESIZE, bp->ut_line, UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, ct, ct + 11);
225 if (!T->logout)
226 puts(" still logged in");
227 else {
228 if (T->logout < 0) {
229 T->logout = -T->logout;
230 printf("- %s", crmsg);
231 }
232 else
233 printf("- %5.5s", ctime(&T->logout)+11);
234 delta = T->logout - bp->ut_time;
235 if (delta < SECDAY)
236 printf(" (%5.5s)\n", asctime(gmtime(&delta))+11);
237 else
238 printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta))+11);
239 }
240 if (maxrec != -1 && !--maxrec)
241 return;
242 }
243 T->logout = bp->ut_time;
244 }
245 }
246 ct = ctime(&buf[0].ut_time);
247 printf("\nwtmp begins %10.10s %5.5s \n", ct, ct + 11);
248 }
249
250 /*
251 * want --
252 * see if want this entry
253 */
254 want(bp, check)
255 register struct utmp *bp;
256 int check;
257 {
258 register ARG *step;
259
260 if (check)
261 /*
262 * when uucp and ftp log in over a network, the entry in
263 * the utmp file is the name plus their process id. See
264 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
265 */
266 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
267 bp->ut_line[3] = '\0';
268 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
269 bp->ut_line[4] = '\0';
270 if (!arglist)
271 return(YES);
272
273 for (step = arglist; step; step = step->next)
274 switch(step->type) {
275 case HOST_TYPE:
276 if (!strncasecmp(step->name, bp->ut_host, UT_HOSTSIZE))
277 return(YES);
278 break;
279 case TTY_TYPE:
280 if (!strncmp(step->name, bp->ut_line, UT_LINESIZE))
281 return(YES);
282 break;
283 case USER_TYPE:
284 if (!strncmp(step->name, bp->ut_name, UT_NAMESIZE))
285 return(YES);
286 break;
287 }
288 return(NO);
289 }
290
291 /*
292 * addarg --
293 * add an entry to a linked list of arguments
294 */
295 addarg(type, arg)
296 int type;
297 char *arg;
298 {
299 register ARG *cur;
300 char *malloc();
301
302 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG)))) {
303 fputs("last: malloc failure.\n", stderr);
304 exit(1);
305 }
306 cur->next = arglist;
307 cur->type = type;
308 cur->name = arg;
309 arglist = cur;
310 }
311
312 /*
313 * addtty --
314 * add an entry to a linked list of ttys
315 */
316 TTY *
317 addtty(ttyname)
318 char *ttyname;
319 {
320 register TTY *cur;
321 char *malloc();
322
323 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY)))) {
324 fputs("last: malloc failure.\n", stderr);
325 exit(1);
326 }
327 cur->next = ttylist;
328 cur->logout = currentout;
329 bcopy(ttyname, cur->tty, UT_LINESIZE);
330 return(ttylist = cur);
331 }
332
333 /*
334 * hostconv --
335 * convert the hostname to search pattern; if the supplied host name
336 * has a domain attached that is the same as the current domain, rip
337 * off the domain suffix since that's what login(1) does.
338 */
339 hostconv(arg)
340 char *arg;
341 {
342 static int first = 1;
343 static char *hostdot, name[MAXHOSTNAMELEN];
344 char *argdot, *index();
345
346 if (!(argdot = index(arg, '.')))
347 return;
348 if (first) {
349 first = 0;
350 if (gethostname(name, sizeof(name))) {
351 perror("last: gethostname");
352 exit(1);
353 }
354 hostdot = index(name, '.');
355 }
356 if (hostdot && !strcasecmp(hostdot, argdot))
357 *argdot = '\0';
358 }
359
360 /*
361 * ttyconv --
362 * convert tty to correct name.
363 */
364 char *
365 ttyconv(arg)
366 char *arg;
367 {
368 char *mval, *malloc(), *strcpy();
369
370 /*
371 * kludge -- we assume that all tty's end with
372 * a two character suffix.
373 */
374 if (strlen(arg) == 2) {
375 /* either 6 for "ttyxx" or 8 for "console" */
376 if (!(mval = malloc((u_int)8))) {
377 fputs("last: malloc failure.\n", stderr);
378 exit(1);
379 }
380 if (!strcmp(arg, "co"))
381 (void)strcpy(mval, "console");
382 else {
383 (void)strcpy(mval, "tty");
384 (void)strcpy(mval + 3, arg);
385 }
386 return(mval);
387 }
388 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
389 return(arg + 5);
390 return(arg);
391 }
392
393 /*
394 * onintr --
395 * on interrupt, we inform the user how far we've gotten
396 */
397 void
398 onintr(signo)
399 int signo;
400 {
401 char *ct, *ctime();
402
403 ct = ctime(&buf[0].ut_time);
404 printf("\ninterrupted %10.10s %5.5s \n", ct, ct + 11);
405 if (signo == SIGINT)
406 exit(1);
407 (void)fflush(stdout); /* fix required for rsh */
408 }
409