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 | * |
Eric Andersen | 75610e1 | 2000-09-22 00:22:10 +0000 | [diff] [blame] | 11 | * Modified to support all sorts of libcs by |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 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 | |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
| 17 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 18 | #include <errno.h> |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 19 | #include <utmp.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 22 | #include "busybox.h" |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 23 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 24 | extern int dutmp_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | { |
| 26 | |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 27 | int file; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 28 | struct utmp ut; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 30 | if (argc<2) { |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 31 | file = fileno(stdin); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 32 | } else if (*argv[1] == '-' ) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 33 | show_usage(); |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 34 | } else { |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 35 | file = open(argv[1], O_RDONLY); |
| 36 | if (file < 0) { |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 37 | perror_msg_and_die(io_error, argv[1]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 40 | |
Eric Andersen | b6b519b | 2001-04-09 23:52:18 +0000 | [diff] [blame] | 41 | /* Kludge around the fact that the binary format for utmp has changed. */ |
Eric Andersen | e703f7e | 2001-04-10 19:03:03 +0000 | [diff] [blame] | 42 | #if __GNU_LIBRARY__ < 5 || defined __UCLIBC__ |
Eric Andersen | b6b519b | 2001-04-09 23:52:18 +0000 | [diff] [blame] | 43 | /* Linux libc5 */ |
Erik Andersen | 499f65f | 2000-05-16 20:07:38 +0000 | [diff] [blame] | 44 | while (read(file, (void*)&ut, sizeof(struct utmp))) { |
| 45 | printf("%d|%d|%s|%s|%s|%s|%s|%lx\n", |
| 46 | ut.ut_type, ut.ut_pid, ut.ut_line, |
| 47 | ut.ut_id, ut.ut_user, ut.ut_host, |
| 48 | ctime(&(ut.ut_time)), |
| 49 | (long)ut.ut_addr); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
Eric Andersen | 75610e1 | 2000-09-22 00:22:10 +0000 | [diff] [blame] | 51 | #else |
Eric Andersen | 77d9268 | 2001-05-23 20:32:09 +0000 | [diff] [blame] | 52 | /* Glibc, uClibc, etc. */ |
Eric Andersen | 75610e1 | 2000-09-22 00:22:10 +0000 | [diff] [blame] | 53 | while (read(file, (void*)&ut, sizeof(struct utmp))) { |
| 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, |
| 58 | ut.ut_session, |
| 59 | ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, |
| 60 | ut.ut_addr); |
| 61 | } |
| 62 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 63 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | } |