blob: a75a6e3922fbbe1b1a10ac2e916be11d972be45b [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 Andersen75610e12000-09-22 00:22:10 +000044/* Kludge around the fact that the binary format for utmp has changed, and the
45 * fact the stupid libc doesn't have a reliable #define to announce that libc5
46 * is being used. sigh.
47 */
Eric Andersen67991cf2001-02-14 21:23:06 +000048#if ! defined __GLIBC__ || defined __UCLIBC__
Erik Andersen499f65f2000-05-16 20:07:38 +000049 while (read(file, (void*)&ut, sizeof(struct utmp))) {
50 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
51 ut.ut_type, ut.ut_pid, ut.ut_line,
52 ut.ut_id, ut.ut_user, ut.ut_host,
53 ctime(&(ut.ut_time)),
54 (long)ut.ut_addr);
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 }
Eric Andersen75610e12000-09-22 00:22:10 +000056#else
57 while (read(file, (void*)&ut, sizeof(struct utmp))) {
58 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
59 ut.ut_type, ut.ut_pid, ut.ut_line,
60 ut.ut_id, ut.ut_user, ut.ut_host,
61 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
62 ut.ut_session,
63 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
64 ut.ut_addr);
65 }
66#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000067 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000068}