utmpdump.c 1.9 KB

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