1 /* utmpdump - dump utmp-like files.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <utmp.h>
24
25 static void
print_entry(struct utmp * up)26 print_entry (struct utmp *up)
27 {
28 /* Mixed 32-/64-bit systems may have timeval structs of different sixe
29 but need struct utmp to be the same size. So in 64-bit up->ut_tv may
30 not be a timeval but a struct of __int32_t's. This would cause a compile
31 time warning and a formating error when 32-bit int is passed where
32 a 64-bit long is expected. So copy up->up_tv to a temporary timeval.
33 This is 32-/64-bit agnostic and expands the timeval fields to the
34 expected size as needed. */
35 struct timeval temp_tv;
36 temp_tv.tv_sec = up->ut_tv.tv_sec;
37 temp_tv.tv_usec = up->ut_tv.tv_usec;
38
39 printf ("[%d] [%05d] [%-4.4s] [%-8.8s] [%-12.12s] [%-16.16s] [%-15.15s]"
40 " [%ld]\n",
41 up->ut_type, up->ut_pid, up->ut_id, up->ut_user, up->ut_line,
42 up->ut_host, 4 + ctime (&temp_tv.tv_sec),
43 (long int) temp_tv.tv_usec);
44 }
45
46 int
main(int argc,char * argv[])47 main (int argc, char *argv[])
48 {
49 struct utmp *up;
50
51 if (argc > 1)
52 utmpname (argv[1]);
53
54 setutent ();
55
56 while ((up = getutent ()))
57 print_entry (up);
58
59 endutent ();
60
61 return EXIT_SUCCESS;
62 }
63