last.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * last implementation for busybox
  4. *
  5. * Copyright (C) 2003-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sys/types.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <utmp.h>
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include "busybox.h"
  32. #ifndef SHUTDOWN_TIME
  33. # define SHUTDOWN_TIME 254
  34. #endif
  35. /* Grr... utmp char[] members do not have to be nul-terminated.
  36. * Do what we can while still keeping this reasonably small.
  37. * Note: We are assuming the ut_id[] size is fixed at 4. */
  38. #if (UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256)
  39. #error struct utmp member char[] size(s) have changed!
  40. #endif
  41. int last_main(int argc, char **argv)
  42. {
  43. struct utmp ut;
  44. int n, file = STDIN_FILENO;
  45. time_t t_tmp;
  46. if (argc > 1) {
  47. bb_show_usage();
  48. }
  49. file = bb_xopen(_PATH_WTMP, O_RDONLY);
  50. printf("%-10s %-14s %-18s %-12.12s %s\n", "USER", "TTY", "HOST", "LOGIN", "TIME");
  51. while ((n = safe_read(file, (void*)&ut, sizeof(struct utmp))) != 0) {
  52. if (n != sizeof(struct utmp)) {
  53. bb_perror_msg_and_die("short read");
  54. }
  55. if (strncmp(ut.ut_line, "~", 1) == 0) {
  56. if (strncmp(ut.ut_user, "shutdown", 8) == 0)
  57. ut.ut_type = SHUTDOWN_TIME;
  58. else if (strncmp(ut.ut_user, "reboot", 6) == 0)
  59. ut.ut_type = BOOT_TIME;
  60. else if (strncmp(ut.ut_user, "runlevel", 7) == 0)
  61. ut.ut_type = RUN_LVL;
  62. } else {
  63. if (!ut.ut_name[0] || strcmp(ut.ut_name, "LOGIN") == 0 ||
  64. ut.ut_name[0] == 0)
  65. {
  66. /* Don't bother. This means we can't find how long
  67. * someone was logged in for. Oh well. */
  68. continue;
  69. }
  70. if (ut.ut_type != DEAD_PROCESS &&
  71. ut.ut_name[0] && ut.ut_line[0])
  72. {
  73. ut.ut_type = USER_PROCESS;
  74. }
  75. if (strcmp(ut.ut_name, "date") == 0) {
  76. if (ut.ut_line[0] == '|') ut.ut_type = OLD_TIME;
  77. if (ut.ut_line[0] == '{') ut.ut_type = NEW_TIME;
  78. }
  79. }
  80. if (ut.ut_type!=USER_PROCESS) {
  81. switch (ut.ut_type) {
  82. case OLD_TIME:
  83. case NEW_TIME:
  84. case RUN_LVL:
  85. case SHUTDOWN_TIME:
  86. continue;
  87. case BOOT_TIME:
  88. strcpy(ut.ut_line, "system boot");
  89. break;
  90. }
  91. }
  92. t_tmp = (time_t)ut.ut_tv.tv_sec;
  93. printf("%-10s %-14s %-18s %-12.12s\n", ut.ut_user, ut.ut_line, ut.ut_host,
  94. ctime(&t_tmp) + 4);
  95. }
  96. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  97. }