blob: 192871f1e051291f14f0627cf06aa26c0c91df18 [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 *
11 * made against libc6
12 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000013
Eric Andersencc8ed391999-10-05 16:24:54 +000014#include "internal.h"
15#include <stdio.h>
Erik Andersen61677fe2000-04-13 01:18:56 +000016#include <errno.h>
Erik Andersen61677fe2000-04-13 01:18:56 +000017#define BB_DECLARE_EXTERN
18#define bb_need_io_error
19#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Erik Andersen4f3f7572000-04-28 00:18:56 +000021#if defined(__GLIBC__)
22#include <utmp.h>
23#else
24#include <utmp-wrap.h>
25#define utmp new_utmp
26#endif
27
Erik Andersen5afc8642000-05-02 00:07:56 +000028
Erik Andersen9cf3bfa2000-04-13 18:49:43 +000029static const char dutmp_usage[] = "dutmp [FILE]\n\n"
30 "Dump utmp file format (pipe delimited) from FILE\n"
31 "or stdin to stdout. (i.e. 'dutmp /var/run/utmp')\n";
Erik Andersene49d5ec2000-02-08 19:58:47 +000032
33extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000034{
35
Erik Andersen61677fe2000-04-13 01:18:56 +000036 FILE *f;
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Erik Andersen61677fe2000-04-13 01:18:56 +000039 if (argc<2) {
40 f = stdin;
41 } else if (*argv[1] == '-' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 usage(dutmp_usage);
Erik Andersen61677fe2000-04-13 01:18:56 +000043 } else {
44 f = fopen(argv[1], "r");
45 if (f == NULL) {
46 fatalError(io_error, argv[1], strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 }
48 }
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Erik Andersen61677fe2000-04-13 01:18:56 +000050 while (fread(&ut, sizeof(struct utmp), 1, f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
52 ut.ut_type, ut.ut_pid, ut.ut_line,
53 ut.ut_id, ut.ut_user, ut.ut_host,
54 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
Erik Andersen4f3f7572000-04-28 00:18:56 +000055 ut.ut_session, ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
56 ut.ut_addr_v6[0]);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 exit(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000060}