blob: 45eab8ae78fb4efe4e2d6663aec7225d4541f1fa [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>
16#include <utmp.h>
17
Eric Andersene77ae3a1999-10-19 20:03:34 +000018static const char dutmp_usage[] = "dutmp\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 "\n"
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Erik Andersene49d5ec2000-02-08 19:58:47 +000021 "\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
22 "\tdutmp /var/run/utmp\n";
23
24extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000025{
26
Erik Andersene49d5ec2000-02-08 19:58:47 +000027 FILE *f = stdin;
28 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 if ((argc < 2) || (**(argv + 1) == '-')) {
31 usage(dutmp_usage);
32 }
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 if (**(++argv) == 0) {
35 f = fopen(*(++argv), "r");
36 if (f < 0) {
37 perror(*argv);
38 exit(FALSE);
39 }
40 }
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 while (fread(&ut, 1, sizeof(struct utmp), f)) {
43 // printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n",
44 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
45 ut.ut_type, ut.ut_pid, ut.ut_line,
46 ut.ut_id, ut.ut_user, ut.ut_host,
47 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
48 ut.ut_session,
49 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, ut.ut_addr);
50 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 exit(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000053}