3
0

who.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* vi: set sw=4 ts=4: */
  2. /*----------------------------------------------------------------------
  3. * Mini who is used to display user name, login time,
  4. * idle time and host name.
  5. *
  6. * Author: Da Chen <dchen@ayrnetworks.com>
  7. *
  8. * This is a free document; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation:
  11. * http://www.gnu.org/copyleft/gpl.html
  12. *
  13. * Copyright (c) 2002 AYR Networks, Inc.
  14. *
  15. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  16. *
  17. *----------------------------------------------------------------------
  18. */
  19. /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
  20. //config:config WHO
  21. //config: bool "who"
  22. //config: default y
  23. //config: depends on FEATURE_UTMP
  24. //config: help
  25. //config: who is used to show who is logged on.
  26. //config:config USERS
  27. //config: bool "users"
  28. //config: default y
  29. //config: depends on FEATURE_UTMP
  30. //config: help
  31. //config: Print users currently logged on.
  32. //applet:IF_USERS(APPLET_ODDNAME(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
  33. //applet:IF_WHO( APPLET( who, BB_DIR_USR_BIN, BB_SUID_DROP))
  34. //kbuild:lib-$(CONFIG_USERS) += who.o
  35. //kbuild:lib-$(CONFIG_WHO) += who.o
  36. //usage:#define users_trivial_usage
  37. //usage: ""
  38. //usage:#define users_full_usage "\n\n"
  39. //usage: "Print the users currently logged on"
  40. //usage:#define who_trivial_usage
  41. //usage: "[-a]"
  42. //usage:#define who_full_usage "\n\n"
  43. //usage: "Show who is logged on\n"
  44. //usage: "\n -a Show all"
  45. //usage: "\n -H Print column headers"
  46. #include "libbb.h"
  47. static void idle_string(char *str6, time_t t)
  48. {
  49. t = time(NULL) - t;
  50. /*if (t < 60) {
  51. str6[0] = '.';
  52. str6[1] = '\0';
  53. return;
  54. }*/
  55. if (t >= 0 && t < (24 * 60 * 60)) {
  56. sprintf(str6, "%02d:%02d",
  57. (int) (t / (60 * 60)),
  58. (int) ((t % (60 * 60)) / 60));
  59. return;
  60. }
  61. strcpy(str6, "old");
  62. }
  63. int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  64. int who_main(int argc UNUSED_PARAM, char **argv)
  65. {
  66. struct utmpx *ut;
  67. unsigned opt;
  68. int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
  69. const char *fmt = "%s";
  70. opt_complementary = "=0";
  71. opt = getopt32(argv, do_users ? "" : "aH");
  72. if (opt & 2) // -H
  73. puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
  74. setutxent();
  75. while ((ut = getutxent()) != NULL) {
  76. if (ut->ut_user[0]
  77. && ((opt & 1) || ut->ut_type == USER_PROCESS)
  78. ) {
  79. if (!do_users) {
  80. char str6[6];
  81. char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
  82. struct stat st;
  83. time_t seconds;
  84. str6[0] = '?';
  85. str6[1] = '\0';
  86. strcpy(name, "/dev/");
  87. safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
  88. ut->ut_line,
  89. sizeof(ut->ut_line)+1
  90. );
  91. if (stat(name, &st) == 0)
  92. idle_string(str6, st.st_atime);
  93. /* manpages say ut_tv.tv_sec *is* time_t,
  94. * but some systems have it wrong */
  95. seconds = ut->ut_tv.tv_sec;
  96. /* How wide time field can be?
  97. * "Nov 10 19:33:20": 15 chars
  98. * "2010-11-10 19:33": 16 chars
  99. */
  100. printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
  101. (int)sizeof(ut->ut_user), ut->ut_user,
  102. (int)sizeof(ut->ut_line), ut->ut_line,
  103. str6,
  104. ctime(&seconds) + 4,
  105. (int)sizeof(ut->ut_host), ut->ut_host
  106. );
  107. } else {
  108. printf(fmt, ut->ut_user);
  109. fmt = " %s";
  110. }
  111. }
  112. }
  113. if (do_users)
  114. bb_putchar('\n');
  115. if (ENABLE_FEATURE_CLEAN_UP)
  116. endutxent();
  117. return EXIT_SUCCESS;
  118. }