chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / login / programs / utmpdump.c
1 /* utmpdump - dump utmp-like files.
2    Copyright (C) 1997, 2002, 2003 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
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.
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
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <utmp.h>
26
27 static void
28 print_entry (struct utmp *up)
29 {
30   /* Mixed 32-/64-bit systems may have timeval structs of different sixe
31      but need struct utmp to be the same size.  So in 64-bit up->ut_tv may
32      not be a timeval but a struct of __int32_t's.  This would cause a compile
33      time warning and a formating error when 32-bit int is passed where
34      a 64-bit long is expected. So copy up->up_tv to a temporary timeval.
35      This is 32-/64-bit agnostic and expands the timeval fields to the
36      expected size as needed. */
37   struct timeval temp_tv;
38   temp_tv.tv_sec = up->ut_tv.tv_sec;
39   temp_tv.tv_usec = up->ut_tv.tv_usec;
40
41   (printf) (
42             /* The format string.  */
43 #if _HAVE_UT_TYPE
44             "[%d] "
45 #endif
46 #if _HAVE_UT_PID
47             "[%05d] "
48 #endif
49 #if _HAVE_UT_ID
50             "[%-4.4s] "
51 #endif
52             "[%-8.8s] [%-12.12s]"
53 #if _HAVE_UT_HOST
54             " [%-16.16s]"
55 #endif
56             " [%-15.15s]"
57 #if _HAVE_UT_TV
58             " [%ld]"
59 #endif
60             "\n"
61             /* The arguments.  */
62 #if _HAVE_UT_TYPE
63             , up->ut_type
64 #endif
65 #if _HAVE_UT_PID
66             , up->ut_pid
67 #endif
68 #if _HAVE_UT_ID
69             , up->ut_id
70 #endif
71             , up->ut_user, up->ut_line
72 #if _HAVE_UT_HOST
73             , up->ut_host
74 #endif
75 #if _HAVE_UT_TV
76             , 4 + ctime (&temp_tv.tv_sec)
77             , (long int) temp_tv.tv_usec
78 #else
79             , 4 + ctime (&up->ut_time)
80 #endif
81            );
82 }
83
84 int
85 main (int argc, char *argv[])
86 {
87   struct utmp *up;
88
89   if (argc > 1)
90     utmpname (argv[1]);
91
92   setutent ();
93
94   while ((up = getutent ()))
95     print_entry (up);
96
97   endutent ();
98
99   return EXIT_SUCCESS;
100 }