886d7880db21c78fc5a84806ef8a2ecb32f3eb65
[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  * made against libc6
12  */
13
14 #include "internal.h"
15 #include <stdio.h>
16 #include <errno.h>
17 #define BB_DECLARE_EXTERN
18 #define bb_need_io_error
19 #include "messages.c"
20
21 #if defined(__GLIBC__)
22 #include <utmp.h>
23 #else
24 #include <utmp-wrap.h>
25 #define utmp new_utmp
26 #endif
27
28 static const char dutmp_usage[] = "dutmp [FILE]\n\n"
29         "Dump utmp file format (pipe delimited) from FILE\n"
30         "or stdin to stdout.  (i.e. 'dutmp /var/run/utmp')\n";
31
32 extern int dutmp_main(int argc, char **argv)
33 {
34
35         FILE *f;
36         struct utmp ut;
37
38         if (argc<2) {
39                 f = stdin;
40         } else if (*argv[1] == '-' ) {
41                 usage(dutmp_usage);
42         } else  {
43                 f = fopen(argv[1], "r");
44                 if (f == NULL) {
45                         fatalError(io_error, argv[1], strerror(errno));
46                 }
47         }
48
49         while (fread(&ut, sizeof(struct utmp), 1, f)) {
50                 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
51                            ut.ut_type, ut.ut_pid, ut.ut_line,
52                            ut.ut_id, ut.ut_user, ut.ut_host,
53                            ut.ut_exit.e_termination, ut.ut_exit.e_exit,
54                            ut.ut_session, ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, 
55                            ut.ut_addr_v6[0]);
56         }
57
58         exit(TRUE);
59 }