aboutsummaryrefslogtreecommitdiff
path: root/miscutils/dutmp.c
diff options
context:
space:
mode:
authormjn3 <mjn3@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-19 09:13:01 +0000
committermjn3 <mjn3@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-19 09:13:01 +0000
commite901c15d890dbbdce4c086963cb1513653fc46b5 (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91 /miscutils/dutmp.c
parent40758c00616c3b2c85d83eb4afdeb04b1f65c9f1 (diff)
downloadbusybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.tar.gz
busybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.tar.bz2
busybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.zip
Major coreutils update.
git-svn-id: svn://busybox.net/trunk/busybox@6751 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'miscutils/dutmp.c')
-rw-r--r--miscutils/dutmp.c95
1 files changed, 57 insertions, 38 deletions
diff --git a/miscutils/dutmp.c b/miscutils/dutmp.c
index 19e09fbb0..113f850fe 100644
--- a/miscutils/dutmp.c
+++ b/miscutils/dutmp.c
@@ -8,57 +8,76 @@
8 * versions of 'who', 'last', etc. IP Addr is output in hex, 8 * versions of 'who', 'last', etc. IP Addr is output in hex,
9 * little endian on x86. 9 * little endian on x86.
10 * 10 *
11 * Modified to support all sorts of libcs by
12 * Erik Andersen <andersen@lineo.com>
13 */ 11 */
14 12
15#include <sys/types.h> 13/* Mar 13, 2003 Manuel Novoa III
16#include <fcntl.h> 14 *
17#include <errno.h> 15 * 1) Added proper error checking.
18#include <utmp.h> 16 * 2) Allow '-' arg for stdin.
17 * 3) For modern libcs, take into account that utmp char[] members
18 * need not be nul-terminated.
19 */
20
19#include <stdlib.h> 21#include <stdlib.h>
20#include <unistd.h> 22#include <unistd.h>
21#include <time.h> 23#include <fcntl.h>
24#include <utmp.h>
22#include "busybox.h" 25#include "busybox.h"
23 26
27/* Grr... utmp char[] members do not have to be nul-terminated.
28 * Do what we can while still keeping this reasonably small.
29 * Note: We are assuming the ut_id[] size is fixed at 4. */
30
31#if __GNU_LIBRARY__ < 5
32#warning the format string needs to be changed
33#else
34#if (UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256)
35#error struct utmp member char[] size(s) have changed!
36#endif
37#endif
38
24extern int dutmp_main(int argc, char **argv) 39extern int dutmp_main(int argc, char **argv)
25{ 40{
26 41 int file = STDIN_FILENO;
27 int file; 42 ssize_t n;
28 struct utmp ut; 43 struct utmp ut;
29 44
30 if (argc<2) { 45 if (argc > 2) {
31 file = fileno(stdin); 46 bb_show_usage();
32 } else if (*argv[1] == '-' ) { 47 }
33 show_usage(); 48 ++argv;
34 } else { 49 if ((argc == 2) && ((argv[0][0] != '-') || argv[0][1])) {
35 file = open(argv[1], O_RDONLY); 50 file = bb_xopen(*argv, O_RDONLY);
36 if (file < 0) {
37 error_msg_and_die(io_error, argv[1]);
38 }
39 } 51 }
40 52
41/* Kludge around the fact that the binary format for utmp has changed. */ 53
54 while ((n = safe_read(file, (void*)&ut, sizeof(struct utmp))) != 0) {
55
56 if (n != sizeof(struct utmp)) {
57 bb_perror_msg_and_die("short read");
58 }
59
60 /* Kludge around the fact that the binary format for utmp has changed. */
42#if __GNU_LIBRARY__ < 5 61#if __GNU_LIBRARY__ < 5
43 /* Linux libc5 */ 62 /* Linux libc5 */
44 while (read(file, (void*)&ut, sizeof(struct utmp))) { 63
45 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n", 64 bb_printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
46 ut.ut_type, ut.ut_pid, ut.ut_line, 65 ut.ut_type, ut.ut_pid, ut.ut_line,
47 ut.ut_id, ut.ut_user, ut.ut_host, 66 ut.ut_id, ut.ut_user, ut.ut_host,
48 ctime(&(ut.ut_time)), 67 ctime(&(ut.ut_time)),
49 (long)ut.ut_addr); 68 (long)ut.ut_addr);
50 }
51#else 69#else
52 /* Glibc, uClibc, etc. */ 70 /* Glibc, uClibc, etc. */
53 while (read(file, (void*)&ut, sizeof(struct utmp))) { 71
54 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n", 72 bb_printf("%d|%d|%.32s|%.4s|%.32s|%.256s|%d|%d|%ld|%ld|%ld|%x\n",
55 ut.ut_type, ut.ut_pid, ut.ut_line, 73 ut.ut_type, ut.ut_pid, ut.ut_line,
56 ut.ut_id, ut.ut_user, ut.ut_host, 74 ut.ut_id, ut.ut_user, ut.ut_host,
57 ut.ut_exit.e_termination, ut.ut_exit.e_exit, 75 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
58 ut.ut_session, 76 ut.ut_session,
59 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, 77 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
60 ut.ut_addr); 78 ut.ut_addr);
61 }
62#endif 79#endif
63 return EXIT_SUCCESS; 80 }
81
82 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
64} 83}