blob: df7f64d30dfaaff27a573443263d7a57e5b618c5 [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"
Erik Andersen4f3f7572000-04-28 00:18:56 +000023
Erik Andersene49d5ec2000-02-08 19:58:47 +000024extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000025{
26
Erik Andersen499f65f2000-05-16 20:07:38 +000027 int file;
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Erik Andersen61677fe2000-04-13 01:18:56 +000030 if (argc<2) {
Erik Andersen499f65f2000-05-16 20:07:38 +000031 file = fileno(stdin);
Erik Andersen61677fe2000-04-13 01:18:56 +000032 } else if (*argv[1] == '-' ) {
Eric Andersen67991cf2001-02-14 21:23:06 +000033 show_usage();
Erik Andersen61677fe2000-04-13 01:18:56 +000034 } else {
Erik Andersen499f65f2000-05-16 20:07:38 +000035 file = open(argv[1], O_RDONLY);
36 if (file < 0) {
Eric Andersened3ef502001-01-27 08:24:39 +000037 perror_msg_and_die(io_error, argv[1]);
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 }
39 }
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Eric Andersenb6b519b2001-04-09 23:52:18 +000041/* Kludge around the fact that the binary format for utmp has changed. */
Eric Andersene703f7e2001-04-10 19:03:03 +000042#if __GNU_LIBRARY__ < 5 || defined __UCLIBC__
Eric Andersenb6b519b2001-04-09 23:52:18 +000043 /* Linux libc5 */
Erik Andersen499f65f2000-05-16 20:07:38 +000044 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 Andersene49d5ec2000-02-08 19:58:47 +000050 }
Eric Andersen75610e12000-09-22 00:22:10 +000051#else
Eric Andersen77d92682001-05-23 20:32:09 +000052 /* Glibc, uClibc, etc. */
Eric Andersen75610e12000-09-22 00:22:10 +000053 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 Kraai3e856ce2000-12-01 02:55:13 +000063 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000064}