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 | * |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 11 | * Modified to support all sort of libcs by |
| 12 | * Erik Andersen <andersen@lineo.com> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 13 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 14 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 15 | #include "internal.h" |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <fcntl.h> |
| 19 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 21 | #define BB_DECLARE_EXTERN |
| 22 | #define bb_need_io_error |
| 23 | #include "messages.c" |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 24 | #include <utmp.h> |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 25 | |
Erik Andersen | 5afc864 | 2000-05-02 00:07:56 +0000 | [diff] [blame] | 26 | |
Erik Andersen | 59b9e87 | 2000-05-10 05:05:45 +0000 | [diff] [blame] | 27 | static const char dutmp_usage[] = "dutmp [FILE]\n" |
| 28 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 29 | "\nDump utmp file format (pipe delimited) from FILE\n" |
| 30 | "or stdin to stdout. (i.e. 'dutmp /var/run/utmp')\n" |
| 31 | #endif |
| 32 | ; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | |
| 34 | extern int dutmp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | { |
| 36 | |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 37 | int file; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | struct utmp ut; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 40 | if (argc<2) { |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 41 | file = fileno(stdin); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 42 | } else if (*argv[1] == '-' ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | usage(dutmp_usage); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 44 | } else { |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 45 | file = open(argv[1], O_RDONLY); |
| 46 | if (file < 0) { |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 47 | fatalError(io_error, argv[1], strerror(errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 51 | while (read(file, (void*)&ut, sizeof(struct utmp))) { |
| 52 | printf("%d|%d|%s|%s|%s|%s|%s|%lx\n", |
| 53 | ut.ut_type, ut.ut_pid, ut.ut_line, |
| 54 | ut.ut_id, ut.ut_user, ut.ut_host, |
| 55 | ctime(&(ut.ut_time)), |
| 56 | (long)ut.ut_addr); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 58 | |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 59 | return(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | } |