]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/last.c
2 * Berkeley last for Linux. Currently maintained by poe@daimi.aau.dk at
3 * ftp://ftp.daimi.aau.dk/pub/linux/poe/admutil*
5 * Copyright (c) 1987 Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
22 * - added Native Language Support
28 #include <sys/param.h>
31 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #include "pathnames.h"
48 #define SECDAY (24*60*60) /* seconds in a day */
49 #define NO 0 /* false/no */
50 #define YES 1 /* true/yes */
52 static struct utmp utmpbuf
;
54 #define HMAX (int)sizeof(utmpbuf.ut_host) /* size of utmp host field */
55 #define LMAX (int)sizeof(utmpbuf.ut_line) /* size of utmp tty field */
56 #define NMAX (int)sizeof(utmpbuf.ut_name) /* size of utmp name field */
59 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
62 /* maximum sizes used for printing */
63 /* probably we want a two-pass version that computes the right length */
64 int hmax
= MIN(HMAX
, 16);
65 int lmax
= MIN(LMAX
, 8);
66 int nmax
= MIN(NMAX
, 16);
69 char *name
; /* argument */
74 int type
; /* type of arg */
75 struct arg
*next
; /* linked list pointer */
77 ARG
*arglist
; /* head of linked list */
79 typedef struct ttytab
{
80 long logout
; /* log out time */
81 char tty
[LMAX
+ 1]; /* terminal name */
82 struct ttytab
*next
; /* linked list pointer */
84 TTY
*ttylist
; /* head of linked list */
86 static long currentout
, /* current logout value */
87 maxrec
; /* records to display */
88 static char *file
= _PATH_WTMP
; /* wtmp file */
90 static int doyear
= 0; /* output year in dates */
91 static int dolong
= 0; /* print also ip-addr */
93 static void wtmp(), addarg(), hostconv();
96 static char *ttyconv();
107 setlocale(LC_ALL
, "");
108 bindtextdomain(PACKAGE
, LOCALEDIR
);
111 while ((ch
= getopt(argc
, argv
, "0123456789yli:f:h:t:")) != EOF
)
113 case '0': case '1': case '2': case '3': case '4':
114 case '5': case '6': case '7': case '8': case '9':
116 * kludge: last was originally designed to take
117 * a number after a dash.
120 maxrec
= atol(argv
[optind
- 1] + 1);
127 addarg(HOST_TYPE
, optarg
);
130 addarg(TTY_TYPE
, ttyconv(optarg
));
139 addarg(INET_TYPE
, optarg
);
143 fputs(_("usage: last [-#] [-f file] [-t tty] [-h hostname] [user ...]\n"), stderr
);
146 for (argv
+= optind
; *argv
; ++argv
) {
147 #define COMPATIBILITY
149 /* code to allow "last p5" to work */
150 addarg(TTY_TYPE
, ttyconv(*argv
));
152 addarg(USER_TYPE
, *argv
);
159 * print_partial_line --
160 * print the first part of each output line according to specified format
163 print_partial_line(bp
)
168 ct
= ctime(&bp
->ut_time
);
169 printf("%-*.*s %-*.*s ", nmax
, nmax
, bp
->ut_name
,
170 lmax
, lmax
, bp
->ut_line
);
175 foo
.s_addr
= bp
->ut_addr
;
176 printf("%-*.*s ", hmax
, hmax
, inet_ntoa(foo
));
178 printf("%-*.*s ", hmax
, hmax
, "");
181 printf("%-*.*s ", hmax
, hmax
, bp
->ut_host
);
185 printf("%10.10s %4.4s %5.5s ", ct
, ct
+ 20, ct
+ 11);
187 printf("%10.10s %5.5s ", ct
, ct
+ 11);
193 * read through the wtmp file
198 register struct utmp
*bp
; /* current structure */
199 register TTY
*T
; /* tty list entry */
200 long delta
; /* time difference */
204 struct utmp
**utmplist
= NULL
;
211 (void)time(&utmpbuf
.ut_time
);
212 (void)signal(SIGINT
, onintr
);
213 (void)signal(SIGQUIT
, onintr
);
216 while((bp
= getutent())) {
217 if(listnr
>= listlen
) {
219 listlen
*= 2; /* avoid quadratic behaviour */
220 utmplist
= realloc(utmplist
, sizeof(bp
) * listlen
);
223 utmplist
[listnr
] = malloc(sizeof(*bp
));
224 memcpy(utmplist
[listnr
++], bp
, sizeof(*bp
));
229 ct
= ctime(&utmplist
[0]->ut_time
);
231 for(i
= listnr
- 1; i
>= 0; i
--) {
234 * if the terminal line is '~', the machine stopped.
235 * see utmp(5) for more info.
237 if (!strncmp(bp
->ut_line
, "~", LMAX
)) {
239 * utmp(5) also mentions that the user
240 * name should be 'shutdown' or 'reboot'.
241 * Not checking the name causes e.g. runlevel
242 * changes to be displayed as 'crash'. -thaele
244 if (!strncmp(bp
->ut_user
, "reboot", NMAX
) ||
245 !strncmp(bp
->ut_user
, "shutdown", NMAX
)) {
246 /* everybody just logged out */
247 for (T
= ttylist
; T
; T
= T
->next
)
248 T
->logout
= -bp
->ut_time
;
251 currentout
= -bp
->ut_time
;
252 crmsg
= strncmp(bp
->ut_name
, "shutdown", NMAX
) ? "crash" : "down ";
254 (void)strcpy(bp
->ut_name
, "reboot");
256 ct
= ctime(&bp
->ut_time
);
257 if(bp
->ut_type
!= LOGIN_PROCESS
) {
258 print_partial_line(bp
);
261 if (maxrec
&& !--maxrec
)
266 /* find associated tty */
267 for (T
= ttylist
;; T
= T
->next
) {
270 T
= addtty(bp
->ut_line
);
273 if (!strncmp(T
->tty
, bp
->ut_line
, LMAX
))
276 if (bp
->ut_name
[0] && bp
->ut_type
!= LOGIN_PROCESS
277 && bp
->ut_type
!= DEAD_PROCESS
280 print_partial_line(bp
);
283 puts(_(" still logged in"));
286 T
->logout
= -T
->logout
;
287 printf("- %s", crmsg
);
290 printf("- %5.5s", ctime(&T
->logout
)+11);
291 delta
= T
->logout
- bp
->ut_time
;
293 printf(" (%5.5s)\n", asctime(gmtime(&delta
))+11);
295 printf(" (%ld+%5.5s)\n", delta
/ SECDAY
, asctime(gmtime(&delta
))+11);
297 if (maxrec
!= -1 && !--maxrec
)
300 T
->logout
= bp
->ut_time
;
301 utmpbuf
.ut_time
= bp
->ut_time
;
304 if(utmplist
) free(utmplist
);
305 if(ct
) printf(_("\nwtmp begins %s"), ct
); /* ct already ends in \n */
310 * see if want this entry
314 register struct utmp
*bp
;
321 * when uucp and ftp log in over a network, the entry in
322 * the utmp file is the name plus their process id. See
323 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
325 if (!strncmp(bp
->ut_line
, "ftp", sizeof("ftp") - 1))
326 bp
->ut_line
[3] = '\0';
327 else if (!strncmp(bp
->ut_line
, "uucp", sizeof("uucp") - 1))
328 bp
->ut_line
[4] = '\0';
333 for (step
= arglist
; step
; step
= step
->next
)
336 if (!strncmp(step
->name
, bp
->ut_host
, HMAX
))
340 if (!strncmp(step
->name
, bp
->ut_line
, LMAX
))
344 if (!strncmp(step
->name
, bp
->ut_name
, NMAX
))
348 if (bp
->ut_addr
== inet_addr(step
->name
))
357 * add an entry to a linked list of arguments
366 if (!(cur
= (ARG
*)malloc((unsigned int)sizeof(ARG
)))) {
367 fputs(_("last: malloc failure.\n"), stderr
);
378 * add an entry to a linked list of ttys
386 if (!(cur
= (TTY
*)malloc((unsigned int)sizeof(TTY
)))) {
387 fputs(_("last: malloc failure.\n"), stderr
);
391 cur
->logout
= currentout
;
392 memcpy(cur
->tty
, ttyname
, LMAX
);
393 return(ttylist
= cur
);
398 * convert the hostname to search pattern; if the supplied host name
399 * has a domain attached that is the same as the current domain, rip
400 * off the domain suffix since that's what login(1) does.
406 static int first
= 1;
407 static char *hostdot
,
408 name
[MAXHOSTNAMELEN
];
411 if (!(argdot
= strchr(arg
, '.')))
415 if (gethostname(name
, sizeof(name
))) {
416 perror(_("last: gethostname"));
419 hostdot
= strchr(name
, '.');
421 if (hostdot
&& !strcmp(hostdot
, argdot
))
427 * convert tty to correct name.
436 * kludge -- we assume that all tty's end with
437 * a two character suffix.
439 if (strlen(arg
) == 2) {
440 /* either 6 for "ttyxx" or 8 for "console" */
441 if (!(mval
= malloc((unsigned int)8))) {
442 fputs(_("last: malloc failure.\n"), stderr
);
445 if (!strcmp(arg
, "co"))
446 (void)strcpy(mval
, "console");
448 (void)strcpy(mval
, "tty");
449 (void)strcpy(mval
+ 3, arg
);
453 if (!strncmp(arg
, "/dev/", sizeof("/dev/") - 1))
460 * on interrupt, we inform the user how far we've gotten
468 ct
= ctime(&utmpbuf
.ut_time
);
469 printf(_("\ninterrupted %10.10s %5.5s \n"), ct
, ct
+ 11);
472 (void)fflush(stdout
); /* fix required for rsh */