blob: cf0d466ef0e40d4d3f30c125619f017917c98e35 [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>
Eric Andersencc8ed391999-10-05 16:24:54 +000017#include <utmp.h>
Erik Andersen61677fe2000-04-13 01:18:56 +000018#define BB_DECLARE_EXTERN
19#define bb_need_io_error
20#include "messages.c"
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Eric Andersene77ae3a1999-10-19 20:03:34 +000022static const char dutmp_usage[] = "dutmp\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000023 "\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000024 "\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
25 "\tdutmp /var/run/utmp\n";
26
27extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000028{
29
Erik Andersen61677fe2000-04-13 01:18:56 +000030 FILE *f;
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) {
34 f = stdin;
35 } else if (*argv[1] == '-' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 usage(dutmp_usage);
Erik Andersen61677fe2000-04-13 01:18:56 +000037 } else {
38 f = fopen(argv[1], "r");
39 if (f == NULL) {
40 fatalError(io_error, argv[1], strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 }
42 }
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Erik Andersen61677fe2000-04-13 01:18:56 +000044 while (fread(&ut, sizeof(struct utmp), 1, f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
46 ut.ut_type, ut.ut_pid, ut.ut_line,
47 ut.ut_id, ut.ut_user, ut.ut_host,
48 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
49 ut.ut_session,
50 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, ut.ut_addr);
51 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 exit(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000054}