]>
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(void);
94 static void addarg(int, char *);
95 static void hostconv(char *);
96 static void onintr(int);
97 static int want(struct utmp
*, int);
99 static char *ttyconv(char *);
102 main(int argc
, char **argv
) {
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(struct utmp
*bp
) {
166 ct
= ctime(&bp
->ut_time
);
167 printf("%-*.*s %-*.*s ", nmax
, nmax
, bp
->ut_name
,
168 lmax
, lmax
, bp
->ut_line
);
173 foo
.s_addr
= bp
->ut_addr
;
174 printf("%-*.*s ", hmax
, hmax
, inet_ntoa(foo
));
176 printf("%-*.*s ", hmax
, hmax
, "");
179 printf("%-*.*s ", hmax
, hmax
, bp
->ut_host
);
183 printf("%10.10s %4.4s %5.5s ", ct
, ct
+ 20, ct
+ 11);
185 printf("%10.10s %5.5s ", ct
, ct
+ 11);
191 * read through the wtmp file
195 register struct utmp
*bp
; /* current structure */
196 register TTY
*T
; /* tty list entry */
197 long delta
; /* time difference */
200 struct utmp
**utmplist
= NULL
;
207 (void)time(&utmpbuf
.ut_time
);
208 (void)signal(SIGINT
, onintr
);
209 (void)signal(SIGQUIT
, onintr
);
212 while((bp
= getutent())) {
213 if(listnr
>= listlen
) {
215 listlen
*= 2; /* avoid quadratic behaviour */
216 utmplist
= realloc(utmplist
, sizeof(bp
) * listlen
);
219 utmplist
[listnr
] = malloc(sizeof(*bp
));
220 memcpy(utmplist
[listnr
++], bp
, sizeof(*bp
));
225 ct
= ctime(&utmplist
[0]->ut_time
);
227 for(i
= listnr
- 1; i
>= 0; i
--) {
230 * if the terminal line is '~', the machine stopped.
231 * see utmp(5) for more info.
233 if (!strncmp(bp
->ut_line
, "~", LMAX
)) {
235 * utmp(5) also mentions that the user
236 * name should be 'shutdown' or 'reboot'.
237 * Not checking the name causes e.g. runlevel
238 * changes to be displayed as 'crash'. -thaele
240 if (!strncmp(bp
->ut_user
, "reboot", NMAX
) ||
241 !strncmp(bp
->ut_user
, "shutdown", NMAX
)) {
242 /* everybody just logged out */
243 for (T
= ttylist
; T
; T
= T
->next
)
244 T
->logout
= -bp
->ut_time
;
247 currentout
= -bp
->ut_time
;
248 crmsg
= strncmp(bp
->ut_name
, "shutdown", NMAX
) ? "crash" : "down ";
250 (void)strcpy(bp
->ut_name
, "reboot");
252 ct
= ctime(&bp
->ut_time
);
253 if(bp
->ut_type
!= LOGIN_PROCESS
) {
254 print_partial_line(bp
);
257 if (maxrec
&& !--maxrec
)
262 /* find associated tty */
263 for (T
= ttylist
;; T
= T
->next
) {
266 T
= addtty(bp
->ut_line
);
269 if (!strncmp(T
->tty
, bp
->ut_line
, LMAX
))
272 if (bp
->ut_name
[0] && bp
->ut_type
!= LOGIN_PROCESS
273 && bp
->ut_type
!= DEAD_PROCESS
276 print_partial_line(bp
);
279 puts(_(" still logged in"));
282 T
->logout
= -T
->logout
;
283 printf("- %s", crmsg
);
286 printf("- %5.5s", ctime(&T
->logout
)+11);
287 delta
= T
->logout
- bp
->ut_time
;
289 printf(" (%5.5s)\n", asctime(gmtime(&delta
))+11);
291 printf(" (%ld+%5.5s)\n", delta
/ SECDAY
, asctime(gmtime(&delta
))+11);
293 if (maxrec
!= -1 && !--maxrec
)
296 T
->logout
= bp
->ut_time
;
297 utmpbuf
.ut_time
= bp
->ut_time
;
300 if(utmplist
) free(utmplist
);
301 if(ct
) printf(_("\nwtmp begins %s"), ct
); /* ct already ends in \n */
306 * see if want this entry
309 want(struct utmp
*bp
, int check
) {
314 * when uucp and ftp log in over a network, the entry in
315 * the utmp file is the name plus their process id. See
316 * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
318 if (!strncmp(bp
->ut_line
, "ftp", sizeof("ftp") - 1))
319 bp
->ut_line
[3] = '\0';
320 else if (!strncmp(bp
->ut_line
, "uucp", sizeof("uucp") - 1))
321 bp
->ut_line
[4] = '\0';
326 for (step
= arglist
; step
; step
= step
->next
)
329 if (!strncmp(step
->name
, bp
->ut_host
, HMAX
))
333 if (!strncmp(step
->name
, bp
->ut_line
, LMAX
))
337 if (!strncmp(step
->name
, bp
->ut_name
, NMAX
))
341 if (bp
->ut_addr
== inet_addr(step
->name
))
350 * add an entry to a linked list of arguments
353 addarg(int type
, char *arg
) {
356 if (!(cur
= (ARG
*)malloc((unsigned int)sizeof(ARG
)))) {
357 fputs(_("last: malloc failure.\n"), stderr
);
368 * add an entry to a linked list of ttys
371 addtty(char *ttyname
) {
374 if (!(cur
= (TTY
*)malloc((unsigned int)sizeof(TTY
)))) {
375 fputs(_("last: malloc failure.\n"), stderr
);
379 cur
->logout
= currentout
;
380 memcpy(cur
->tty
, ttyname
, LMAX
);
381 return(ttylist
= cur
);
386 * convert the hostname to search pattern; if the supplied host name
387 * has a domain attached that is the same as the current domain, rip
388 * off the domain suffix since that's what login(1) does.
391 hostconv(char *arg
) {
392 static int first
= 1;
393 static char *hostdot
,
394 name
[MAXHOSTNAMELEN
];
397 if (!(argdot
= strchr(arg
, '.')))
401 if (gethostname(name
, sizeof(name
))) {
402 perror(_("last: gethostname"));
405 hostdot
= strchr(name
, '.');
407 if (hostdot
&& !strcmp(hostdot
, argdot
))
413 * convert tty to correct name.
420 * kludge -- we assume that all tty's end with
421 * a two character suffix.
423 if (strlen(arg
) == 2) {
424 /* either 6 for "ttyxx" or 8 for "console" */
425 if (!(mval
= malloc((unsigned int)8))) {
426 fputs(_("last: malloc failure.\n"), stderr
);
429 if (!strcmp(arg
, "co"))
430 (void)strcpy(mval
, "console");
432 (void)strcpy(mval
, "tty");
433 (void)strcpy(mval
+ 3, arg
);
437 if (!strncmp(arg
, "/dev/", sizeof("/dev/") - 1))
444 * on interrupt, we inform the user how far we've gotten
450 ct
= ctime(&utmpbuf
.ut_time
);
451 printf(_("\ninterrupted %10.10s %5.5s \n"), ct
, ct
+ 11);
454 (void)fflush(stdout
); /* fix required for rsh */