Extract usage information into a separate file.
[oweals/busybox.git] / miscutils / 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 sort of libcs by 
12  * Erik Andersen <andersen@lineo.com>
13  */
14
15 #include "internal.h"
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19
20 #include <errno.h>
21 #define BB_DECLARE_EXTERN
22 #define bb_need_io_error
23 #include "messages.c"
24 #include <utmp.h>
25
26 extern int dutmp_main(int argc, char **argv)
27 {
28
29         int file;
30         struct utmp ut;
31
32         if (argc<2) {
33                 file = fileno(stdin);
34         } else if (*argv[1] == '-' ) {
35                 usage(dutmp_usage);
36         } else  {
37                 file = open(argv[1], O_RDONLY);
38                 if (file < 0) {
39                         fatalError(io_error, argv[1], strerror(errno));
40                 }
41         }
42
43         while (read(file, (void*)&ut, sizeof(struct utmp))) {
44                 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
45                                 ut.ut_type, ut.ut_pid, ut.ut_line,
46                                 ut.ut_id, ut.ut_user, ut.ut_host,
47                                 ctime(&(ut.ut_time)), 
48                                 (long)ut.ut_addr);
49         }
50
51         return(TRUE);
52 }