Some fixes and such
[oweals/busybox.git] / dutmp.c
1 /*
2  * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
3  * 
4  * dutmp
5  * Takes utmp formated file on stdin and dumps it's contents 
6  * out in colon delimited fields. Easy to 'cut' for shell based 
7  * versions of 'who', 'last', etc. IP Addr is output in hex, 
8  * little endian on x86.
9  * 
10  * made against libc6
11  */
12
13 #include "internal.h"
14 #include <stdio.h>
15 #include <utmp.h>
16
17 const char dutmp_usage[] = "dutmp\n"
18     "\n"
19     "\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
20     "\tdutmp /var/run/utmp\n";
21
22 extern int dutmp_fn (int argc, char **argv)
23 {
24
25     FILE *f = stdin;
26     struct utmp ut;
27
28     if ((argc < 2) || (**(argv + 1) == '-')) {
29         fprintf (stderr, "Usage: %s %s\n", *argv, dutmp_usage);
30         exit (FALSE);
31     }
32
33     if ( **(++argv) == 0 ) {
34         f = fopen (*(++argv), "r");
35            if (f < 0 ) {
36                 perror (*argv);
37                 exit (FALSE);
38            }
39     }
40
41     while (fread (&ut, 1, sizeof (struct utmp), f)) {
42         // printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n", 
43         printf ("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
44                 ut.ut_type, ut.ut_pid, ut.ut_line,
45                 ut.ut_id, ut.ut_user, ut.ut_host,
46                 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
47                 ut.ut_session,
48                 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, ut.ut_addr);
49     }
50
51     exit (TRUE);
52 }