blob: cf5f1cff802871be7a0710cf8be14f6534f39b23 [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
Eric Andersen3570a342000-09-25 21:45:58 +000015#include "busybox.h"
Erik Andersen499f65f2000-05-16 20:07:38 +000016#include <sys/types.h>
Erik Andersen499f65f2000-05-16 20:07:38 +000017#include <fcntl.h>
18
Erik Andersen61677fe2000-04-13 01:18:56 +000019#include <errno.h>
Erik Andersen61677fe2000-04-13 01:18:56 +000020#define BB_DECLARE_EXTERN
21#define bb_need_io_error
22#include "messages.c"
Erik Andersen4f3f7572000-04-28 00:18:56 +000023#include <utmp.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000024
Erik Andersene49d5ec2000-02-08 19:58:47 +000025extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000026{
27
Erik Andersen499f65f2000-05-16 20:07:38 +000028 int file;
Erik Andersene49d5ec2000-02-08 19:58:47 +000029 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000030
Erik Andersen61677fe2000-04-13 01:18:56 +000031 if (argc<2) {
Erik Andersen499f65f2000-05-16 20:07:38 +000032 file = fileno(stdin);
Erik Andersen61677fe2000-04-13 01:18:56 +000033 } else if (*argv[1] == '-' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 usage(dutmp_usage);
Erik Andersen61677fe2000-04-13 01:18:56 +000035 } else {
Erik Andersen499f65f2000-05-16 20:07:38 +000036 file = open(argv[1], O_RDONLY);
37 if (file < 0) {
Erik Andersen61677fe2000-04-13 01:18:56 +000038 fatalError(io_error, argv[1], strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 }
40 }
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Eric Andersen75610e12000-09-22 00:22:10 +000042/* Kludge around the fact that the binary format for utmp has changed, and the
43 * fact the stupid libc doesn't have a reliable #define to announce that libc5
44 * is being used. sigh.
45 */
46#if ! defined __GLIBC__
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
55 while (read(file, (void*)&ut, sizeof(struct utmp))) {
56 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
57 ut.ut_type, ut.ut_pid, ut.ut_line,
58 ut.ut_id, ut.ut_user, ut.ut_host,
59 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
60 ut.ut_session,
61 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
62 ut.ut_addr);
63 }
64#endif
Eric Andersenb6106152000-06-19 17:25:40 +000065 return(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000066}