It turns out that DODMALLOC was broken when I reorganized busybox.h
[oweals/busybox.git] / dutmp.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
4  * 
5  * dutmp
6  * Takes utmp formated file on stdin and dumps it's contents 
7  * out in colon delimited fields. Easy to 'cut' for shell based 
8  * versions of 'who', 'last', etc. IP Addr is output in hex, 
9  * little endian on x86.
10  * 
11  * Modified to support all sorts of libcs by 
12  * Erik Andersen <andersen@lineo.com>
13  */
14
15 #include <sys/types.h>
16 #include <fcntl.h>
17
18 #include <errno.h>
19 #include <utmp.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include "busybox.h"
23 #define BB_DECLARE_EXTERN
24 #define bb_need_io_error
25 #include "messages.c"
26
27 extern int dutmp_main(int argc, char **argv)
28 {
29
30         int file;
31         struct utmp ut;
32
33         if (argc<2) {
34                 file = fileno(stdin);
35         } else if (*argv[1] == '-' ) {
36                 show_usage();
37         } else  {
38                 file = open(argv[1], O_RDONLY);
39                 if (file < 0) {
40                         perror_msg_and_die(io_error, argv[1]);
41                 }
42         }
43
44 /* Kludge around the fact that the binary format for utmp has changed, and the
45  * fact the stupid libc doesn't have a reliable #define to announce that libc5
46  * is being used.  sigh.
47  */
48 #if ! defined __GLIBC__ || defined __UCLIBC__
49         while (read(file, (void*)&ut, sizeof(struct utmp))) {
50                 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
51                                 ut.ut_type, ut.ut_pid, ut.ut_line,
52                                 ut.ut_id, ut.ut_user, ut.ut_host,
53                                 ctime(&(ut.ut_time)), 
54                                 (long)ut.ut_addr);
55         }
56 #else
57         while (read(file, (void*)&ut, sizeof(struct utmp))) {
58                 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
59                 ut.ut_type, ut.ut_pid, ut.ut_line,
60                 ut.ut_id, ut.ut_user,   ut.ut_host,
61                 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
62                 ut.ut_session,
63                 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
64                 ut.ut_addr);
65         }
66 #endif
67         return EXIT_SUCCESS;
68 }