]> git.ipfire.org Git - thirdparty/glibc.git/blame - login/programs/utmpdump.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / login / programs / utmpdump.c
CommitLineData
76b87c03 1/* utmpdump - dump utmp-like files.
b168057a 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
81e9dad6 3 This file is part of the GNU C Library.
76b87c03 4 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
81e9dad6
UD
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
81e9dad6
UD
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 14 Lesser General Public License for more details.
81e9dad6 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
81e9dad6 19
76b87c03
UD
20#include <stdio.h>
21#include <stdlib.h>
22#include <time.h>
23#include <unistd.h>
24#include <utmp.h>
81e9dad6 25
d54fb3b6 26static void
76b87c03
UD
27print_entry (struct utmp *up)
28{
4c98451b 29 /* Mixed 32-/64-bit systems may have timeval structs of different sixe
a49daee2 30 but need struct utmp to be the same size. So in 64-bit up->ut_tv may
4c98451b
UD
31 not be a timeval but a struct of __int32_t's. This would cause a compile
32 time warning and a formating error when 32-bit int is passed where
33 a 64-bit long is expected. So copy up->up_tv to a temporary timeval.
a49daee2 34 This is 32-/64-bit agnostic and expands the timeval fields to the
4c98451b
UD
35 expected size as needed. */
36 struct timeval temp_tv;
37 temp_tv.tv_sec = up->ut_tv.tv_sec;
38 temp_tv.tv_usec = up->ut_tv.tv_usec;
a49daee2 39
ffa8d2a0
RM
40 (printf) (
41 /* The format string. */
42#if _HAVE_UT_TYPE
43 "[%d] "
44#endif
45#if _HAVE_UT_PID
46 "[%05d] "
47#endif
48#if _HAVE_UT_ID
49 "[%-4.4s] "
50#endif
51 "[%-8.8s] [%-12.12s]"
52#if _HAVE_UT_HOST
53 " [%-16.16s]"
54#endif
55 " [%-15.15s]"
56#if _HAVE_UT_TV
57 " [%ld]"
58#endif
59 "\n"
60 /* The arguments. */
61#if _HAVE_UT_TYPE
62 , up->ut_type
63#endif
64#if _HAVE_UT_PID
65 , up->ut_pid
66#endif
67#if _HAVE_UT_ID
68 , up->ut_id
69#endif
70 , up->ut_user, up->ut_line
71#if _HAVE_UT_HOST
72 , up->ut_host
73#endif
74#if _HAVE_UT_TV
4c98451b 75 , 4 + ctime (&temp_tv.tv_sec)
a49daee2 76 , (long int) temp_tv.tv_usec
0413b54c 77#else
ffa8d2a0 78 , 4 + ctime (&up->ut_time)
0413b54c 79#endif
ffa8d2a0 80 );
76b87c03 81}
81e9dad6 82
76b87c03
UD
83int
84main (int argc, char *argv[])
85{
86 struct utmp *up;
87
88 if (argc > 1)
89 utmpname (argv[1]);
0413b54c 90
76b87c03
UD
91 setutent ();
92
93 while ((up = getutent ()))
94 print_entry (up);
95
96 endutent ();
0413b54c 97
76b87c03
UD
98 return EXIT_SUCCESS;
99}