blob: c6bc0c794faea98d0bcd425ab64584f8a0c6c6cd [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 *
Erik Andersen499f65f2000-05-16 20:07:38 +000011 * Modified to support all sort of libcs by
12 * Erik Andersen <andersen@lineo.com>
Eric Andersencc8ed391999-10-05 16:24:54 +000013 */
Eric Andersen3cf52d11999-10-12 22:26:06 +000014
Eric Andersencc8ed391999-10-05 16:24:54 +000015#include "internal.h"
Erik Andersen499f65f2000-05-16 20:07:38 +000016#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19
Erik Andersen61677fe2000-04-13 01:18:56 +000020#include <errno.h>
Erik Andersen61677fe2000-04-13 01:18:56 +000021#define BB_DECLARE_EXTERN
22#define bb_need_io_error
23#include "messages.c"
Erik Andersen4f3f7572000-04-28 00:18:56 +000024#include <utmp.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000025
Erik Andersen5afc8642000-05-02 00:07:56 +000026
Erik Andersen59b9e872000-05-10 05:05:45 +000027static const char dutmp_usage[] = "dutmp [FILE]\n"
28#ifndef BB_FEATURE_TRIVIAL_HELP
29 "\nDump utmp file format (pipe delimited) from FILE\n"
30 "or stdin to stdout. (i.e. 'dutmp /var/run/utmp')\n"
31#endif
32 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000033
34extern int dutmp_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000035{
36
Erik Andersen499f65f2000-05-16 20:07:38 +000037 int file;
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 struct utmp ut;
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Erik Andersen61677fe2000-04-13 01:18:56 +000040 if (argc<2) {
Erik Andersen499f65f2000-05-16 20:07:38 +000041 file = fileno(stdin);
Erik Andersen61677fe2000-04-13 01:18:56 +000042 } else if (*argv[1] == '-' ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 usage(dutmp_usage);
Erik Andersen61677fe2000-04-13 01:18:56 +000044 } else {
Erik Andersen499f65f2000-05-16 20:07:38 +000045 file = open(argv[1], O_RDONLY);
46 if (file < 0) {
Erik Andersen61677fe2000-04-13 01:18:56 +000047 fatalError(io_error, argv[1], strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 }
49 }
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Erik Andersen499f65f2000-05-16 20:07:38 +000051 while (read(file, (void*)&ut, sizeof(struct utmp))) {
52 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
53 ut.ut_type, ut.ut_pid, ut.ut_line,
54 ut.ut_id, ut.ut_user, ut.ut_host,
55 ctime(&(ut.ut_time)),
56 (long)ut.ut_addr);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000058
Eric Andersenb6106152000-06-19 17:25:40 +000059 return(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000060}