Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 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 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 13 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 14 | #include "internal.h" |
| 15 | #include <stdio.h> |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 16 | #include <errno.h> |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 17 | #define BB_DECLARE_EXTERN |
| 18 | #define bb_need_io_error |
| 19 | #include "messages.c" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 21 | #if defined(__GLIBC__) |
| 22 | #include <utmp.h> |
| 23 | #else |
| 24 | #include <utmp-wrap.h> |
| 25 | #define utmp new_utmp |
| 26 | #endif |
| 27 | |
Erik Andersen | 5afc864 | 2000-05-02 00:07:56 +0000 | [diff] [blame] | 28 | |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 29 | static const char dutmp_usage[] = "dutmp [FILE]\n" |
| 30 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 31 | "\nDump utmp file format (pipe delimited) from FILE\n" |
| 32 | "or stdin to stdout. (i.e. 'dutmp /var/run/utmp')\n" |
| 33 | #endif |
| 34 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | |
| 36 | extern int dutmp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | { |
| 38 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 39 | FILE *f; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | struct utmp ut; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 42 | if (argc<2) { |
| 43 | f = stdin; |
| 44 | } else if (*argv[1] == '-' ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | usage(dutmp_usage); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 46 | } else { |
| 47 | f = fopen(argv[1], "r"); |
| 48 | if (f == NULL) { |
| 49 | fatalError(io_error, argv[1], strerror(errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 53 | while (fread(&ut, sizeof(struct utmp), 1, f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n", |
| 55 | ut.ut_type, ut.ut_pid, ut.ut_line, |
| 56 | ut.ut_id, ut.ut_user, ut.ut_host, |
| 57 | ut.ut_exit.e_termination, ut.ut_exit.e_exit, |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 58 | ut.ut_session, ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, |
| 59 | ut.ut_addr_v6[0]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 61 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | exit(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | } |