Back to index

glibc  2.9
utmpdump.c
Go to the documentation of this file.
00001 /* utmpdump - dump utmp-like files.
00002    Copyright (C) 1997, 2002, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <time.h>
00024 #include <unistd.h>
00025 #include <utmp.h>
00026 
00027 static void
00028 print_entry (struct utmp *up)
00029 {
00030   /* Mixed 32-/64-bit systems may have timeval structs of different sixe
00031      but need struct utmp to be the same size.  So in 64-bit up->ut_tv may
00032      not be a timeval but a struct of __int32_t's.  This would cause a compile
00033      time warning and a formating error when 32-bit int is passed where
00034      a 64-bit long is expected. So copy up->up_tv to a temporary timeval.
00035      This is 32-/64-bit agnostic and expands the timeval fields to the
00036      expected size as needed. */
00037   struct timeval temp_tv;
00038   temp_tv.tv_sec = up->ut_tv.tv_sec;
00039   temp_tv.tv_usec = up->ut_tv.tv_usec;
00040 
00041   (printf) (
00042            /* The format string.  */
00043 #if _HAVE_UT_TYPE
00044            "[%d] "
00045 #endif
00046 #if _HAVE_UT_PID
00047            "[%05d] "
00048 #endif
00049 #if _HAVE_UT_ID
00050            "[%-4.4s] "
00051 #endif
00052            "[%-8.8s] [%-12.12s]"
00053 #if _HAVE_UT_HOST
00054            " [%-16.16s]"
00055 #endif
00056            " [%-15.15s]"
00057 #if _HAVE_UT_TV
00058            " [%ld]"
00059 #endif
00060            "\n"
00061            /* The arguments.  */
00062 #if _HAVE_UT_TYPE
00063            , up->ut_type
00064 #endif
00065 #if _HAVE_UT_PID
00066            , up->ut_pid
00067 #endif
00068 #if _HAVE_UT_ID
00069            , up->ut_id
00070 #endif
00071            , up->ut_user, up->ut_line
00072 #if _HAVE_UT_HOST
00073            , up->ut_host
00074 #endif
00075 #if _HAVE_UT_TV
00076            , 4 + ctime (&temp_tv.tv_sec)
00077            , (long int) temp_tv.tv_usec
00078 #else
00079            , 4 + ctime (&up->ut_time)
00080 #endif
00081           );
00082 }
00083 
00084 int
00085 main (int argc, char *argv[])
00086 {
00087   struct utmp *up;
00088 
00089   if (argc > 1)
00090     utmpname (argv[1]);
00091 
00092   setutent ();
00093 
00094   while ((up = getutent ()))
00095     print_entry (up);
00096 
00097   endutent ();
00098 
00099   return EXIT_SUCCESS;
00100 }