blob: 26253b44551a2bc356ea5fd0f54ab482673e872b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
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 Andersen75610e12000-09-22 00:22:10 +000011 * Modified to support all sorts of libcs by
Erik Andersen499f65f2000-05-16 20:07:38 +000012 * Erik Andersen <andersen@lineo.com>
Eric Andersencc8ed391999-10-05 16:24:54 +000013 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000014
Erik Andersen499f65f2000-05-16 20:07:38 +000015#include <sys/types.h>
Erik Andersen499f65f2000-05-16 20:07:38 +000016#include <fcntl.h>
17
Erik Andersen61677fe2000-04-13 01:18:56 +000018#include <errno.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000019#include <utmp.h>
Eric Andersened3ef502001-01-27 08:24:39 +000020#include <stdlib.h>
21#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000022#include "busybox.h"
23#define BB_DECLARE_EXTERN
24#define bb_need_io_error
25#include "messages.c"
Erik Andersen4f3f7572000-04-28 00:18:56 +000026
Erik Andersene49d5ec2000-02-08 19:58:47 +000027extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000028{
29
Erik Andersen499f65f2000-05-16 20:07:38 +000030 int file;
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Erik Andersen61677fe2000-04-13 01:18:56 +000033 if (argc<2) {
Erik Andersen499f65f2000-05-16 20:07:38 +000034 file = fileno(stdin);
Erik Andersen61677fe2000-04-13 01:18:56 +000035 } else if (*argv[1] == '-' ) {
Eric Andersen67991cf2001-02-14 21:23:06 +000036 show_usage();
Erik Andersen61677fe2000-04-13 01:18:56 +000037 } else {
Erik Andersen499f65f2000-05-16 20:07:38 +000038 file = open(argv[1], O_RDONLY);
39 if (file < 0) {
Eric Andersened3ef502001-01-27 08:24:39 +000040 perror_msg_and_die(io_error, argv[1]);
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 }
42 }
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersenb6b519b2001-04-09 23:52:18 +000044/* Kludge around the fact that the binary format for utmp has changed. */
45#if __GNU_LIBRARY__ < 5
46 /* Linux libc5 */
Erik Andersen499f65f2000-05-16 20:07:38 +000047 while (read(file, (void*)&ut, sizeof(struct utmp))) {
48 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
49 ut.ut_type, ut.ut_pid, ut.ut_line,
50 ut.ut_id, ut.ut_user, ut.ut_host,
51 ctime(&(ut.ut_time)),
52 (long)ut.ut_addr);
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 }
Eric Andersen75610e12000-09-22 00:22:10 +000054#else
Eric Andersenb6b519b2001-04-09 23:52:18 +000055 /* Glibc, uClibc, etc */
Eric Andersen75610e12000-09-22 00:22:10 +000056 while (read(file, (void*)&ut, sizeof(struct utmp))) {
57 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
58 ut.ut_type, ut.ut_pid, ut.ut_line,
59 ut.ut_id, ut.ut_user, ut.ut_host,
60 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
61 ut.ut_session,
62 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
63 ut.ut_addr);
64 }
65#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000066 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000067}