move utmp.h include to libbb.h
[oweals/busybox.git] / miscutils / last.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * last implementation for busybox
4  *
5  * Copyright (C) 2003-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 #include "libbb.h"
11
12 /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
13  * to reduce confusion */
14
15 #ifndef SHUTDOWN_TIME
16 #  define SHUTDOWN_TIME 254
17 #endif
18
19 /* Grr... utmp char[] members do not have to be nul-terminated.
20  * Do what we can while still keeping this reasonably small.
21  * Note: We are assuming the ut_id[] size is fixed at 4. */
22
23 #if defined UT_LINESIZE \
24         && ((UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256))
25 #error struct utmp member char[] size(s) have changed!
26 #elif defined __UT_LINESIZE \
27         && ((__UT_LINESIZE != 32) || (__UT_NAMESIZE != 64) || (__UT_HOSTSIZE != 256))
28 #error struct utmp member char[] size(s) have changed!
29 #endif
30
31 #if EMPTY != 0 || RUN_LVL != 1 || BOOT_TIME != 2 || NEW_TIME != 3 || \
32         OLD_TIME != 4
33 #error Values for the ut_type field of struct utmp changed
34 #endif
35
36 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int last_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
38 {
39         struct utmp ut;
40         int n, file = STDIN_FILENO;
41         time_t t_tmp;
42         off_t pos;
43         static const char _ut_usr[] ALIGN1 =
44                         "runlevel\0" "reboot\0" "shutdown\0";
45         static const char _ut_lin[] ALIGN1 =
46                         "~\0" "{\0" "|\0" /* "LOGIN\0" "date\0" */;
47         enum {
48                 TYPE_RUN_LVL = RUN_LVL,         /* 1 */
49                 TYPE_BOOT_TIME = BOOT_TIME,     /* 2 */
50                 TYPE_SHUTDOWN_TIME = SHUTDOWN_TIME
51         };
52         enum {
53                 _TILDE = EMPTY, /* 0 */
54                 TYPE_NEW_TIME,  /* NEW_TIME, 3 */
55                 TYPE_OLD_TIME   /* OLD_TIME, 4 */
56         };
57
58         if (argv[1]) {
59                 bb_show_usage();
60         }
61         file = xopen(bb_path_wtmp_file, O_RDONLY);
62
63         printf("%-10s %-14s %-18s %-12.12s %s\n",
64                "USER", "TTY", "HOST", "LOGIN", "TIME");
65         /* yikes. We reverse over the file and that is a not too elegant way */
66         pos = xlseek(file, 0, SEEK_END);
67         pos = lseek(file, pos - sizeof(ut), SEEK_SET);
68         while ((n = full_read(file, &ut, sizeof(ut))) > 0) {
69                 if (n != sizeof(ut)) {
70                         bb_perror_msg_and_die("short read");
71                 }
72                 n = index_in_strings(_ut_lin, ut.ut_line);
73                 if (n == _TILDE) { /* '~' */
74 #if 1
75 /* do we really need to be cautious here? */
76                         n = index_in_strings(_ut_usr, ut.ut_user);
77                         if (++n > 0)
78                                 ut.ut_type = n != 3 ? n : SHUTDOWN_TIME;
79 #else
80                         if (strncmp(ut.ut_user, "shutdown", 8) == 0)
81                                 ut.ut_type = SHUTDOWN_TIME;
82                         else if (strncmp(ut.ut_user, "reboot", 6) == 0)
83                                 ut.ut_type = BOOT_TIME;
84                         else if (strncmp(ut.ut_user, "runlevel", 8) == 0)
85                                 ut.ut_type = RUN_LVL;
86 #endif
87                 } else {
88                         if (ut.ut_user[0] == '\0' || strcmp(ut.ut_user, "LOGIN") == 0) {
89                                 /* Don't bother.  This means we can't find how long
90                                  * someone was logged in for.  Oh well. */
91                                 goto next;
92                         }
93                         if (ut.ut_type != DEAD_PROCESS
94                          && ut.ut_user[0]
95                          && ut.ut_line[0]
96                         ) {
97                                 ut.ut_type = USER_PROCESS;
98                         }
99                         if (strcmp(ut.ut_user, "date") == 0) {
100                                 if (n == TYPE_OLD_TIME) { /* '|' */
101                                         ut.ut_type = OLD_TIME;
102                                 }
103                                 if (n == TYPE_NEW_TIME) { /* '{' */
104                                         ut.ut_type = NEW_TIME;
105                                 }
106                         }
107                 }
108
109                 if (ut.ut_type != USER_PROCESS) {
110                         switch (ut.ut_type) {
111                                 case OLD_TIME:
112                                 case NEW_TIME:
113                                 case RUN_LVL:
114                                 case SHUTDOWN_TIME:
115                                         goto next;
116                                 case BOOT_TIME:
117                                         strcpy(ut.ut_line, "system boot");
118                         }
119                 }
120                 /* manpages say ut_tv.tv_sec *is* time_t,
121                  * but some systems have it wrong */
122                 t_tmp = (time_t)ut.ut_tv.tv_sec;
123                 printf("%-10s %-14s %-18s %-12.12s\n",
124                        ut.ut_user, ut.ut_line, ut.ut_host, ctime(&t_tmp) + 4);
125  next:
126                 pos -= sizeof(ut);
127                 if (pos <= 0)
128                         break; /* done. */
129                 xlseek(file, pos, SEEK_SET);
130         }
131
132         fflush_stdout_and_exit(EXIT_SUCCESS);
133 }