1 /* This file is part of GNU Rush.
2    Copyright (C) 2008-2019 Sergey Poznyakoff
3 
4    GNU Rush is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Rush is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Rush.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <rush.h>
18 #include "error.h"
19 
20 char *base_name = RUSH_DB;
21 struct rush_wtmp *wtmp = NULL;
22 int  display_header = 1;  /* Display header line */
23 char *format;
24 
25 #include "rwopt.h"
26 
27 void
xalloc_die()28 xalloc_die()
29 {
30 	error(1, 0, _("not enough memory"));
31 	abort();
32 }
33 
34 
35 char *default_format =
36 	"(user 10 Login) "
37 	"(rule 8 Rule) "
38 	"(start-time 0 Start) "
39 	"(duration 10 Time) "
40 	"(pid 10 PID) "
41 	"(command 28 Command)";
42 
43 int
main(int argc,char ** argv)44 main(int argc, char **argv)
45 {
46 	int status;
47 	rushdb_format_t form;
48 
49 	rush_set_program_name(argv[0]);
50 	rush_i18n_init();
51 
52 	format = getenv("RUSHWHO_FORMAT");
53 	if (!format)
54 		format = default_format;
55 
56 	get_options(argc, argv);
57 	argc -= optind;
58 	argv += optind;
59 
60 	if (argc)
61 		error(1, 0, _("extra arguments"));
62 
63 	if (format[0] == '@')
64 		format = rush_read_format(format + 1);
65 	form = rushdb_compile_format(format);
66 	if (!form)
67 		error(1, 0, _("invalid format: %s"), rushdb_error_string);
68 
69 	switch (rushdb_open(base_name, 0)) {
70 	case rushdb_result_ok:
71 		break;
72 
73 	case rushdb_result_eof:
74 		exit(0);
75 
76 	case rushdb_result_fail:
77                 error(1, errno, _("cannot open database file %s"), base_name);
78 	}
79 
80 	if (display_header)
81 		rushdb_print_header(form);
82 	while (rush_utmp_read(RUSH_STATUS_MAP_BIT(RUSH_STATUS_INUSE),
83 			      &status, &wtmp) == rushdb_result_ok) {
84 
85 		rushdb_print(form, wtmp, 1);
86 		free(wtmp);
87 	}
88 
89 	rushdb_close();
90 
91 	exit(0);
92 }
93