who.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. //config:config WHO
  18. //config: bool "who (3.9 kb)"
  19. //config: default y
  20. //config: depends on FEATURE_UTMP
  21. //config: help
  22. //config: Print users currently logged on.
  23. //config:
  24. // procps-ng has this variation of "who":
  25. //config:config W
  26. //config: bool "w (3.8 kb)"
  27. //config: default y
  28. //config: depends on FEATURE_UTMP
  29. //config: help
  30. //config: Print users currently logged on.
  31. //config:
  32. //config:config USERS
  33. //config: bool "users (3.4 kb)"
  34. //config: default y
  35. //config: depends on FEATURE_UTMP
  36. //config: help
  37. //config: Print users currently logged on.
  38. // APPLET_NOEXEC:name main location suid_type help
  39. //applet:IF_USERS(APPLET_NOEXEC(users, who, BB_DIR_USR_BIN, BB_SUID_DROP, users))
  40. //applet:IF_W( APPLET_NOEXEC(w, who, BB_DIR_USR_BIN, BB_SUID_DROP, w))
  41. //applet:IF_WHO( APPLET_NOEXEC(who, who, BB_DIR_USR_BIN, BB_SUID_DROP, who))
  42. //kbuild:lib-$(CONFIG_USERS) += who.o
  43. //kbuild:lib-$(CONFIG_W) += who.o
  44. //kbuild:lib-$(CONFIG_WHO) += who.o
  45. /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
  46. //usage:#define users_trivial_usage
  47. //usage: ""
  48. //usage:#define users_full_usage "\n\n"
  49. //usage: "Print the users currently logged on"
  50. //usage:#define w_trivial_usage
  51. //usage: ""
  52. //usage:#define w_full_usage "\n\n"
  53. //usage: "Show who is logged on"
  54. //
  55. // procps-ng 3.3.10:
  56. // "\n -h, --no-header"
  57. // "\n -u, --no-current"
  58. // Ignores the username while figuring out the current process
  59. // and cpu times. To demonstrate this, do a "su" and do a "w" and a "w -u".
  60. // "\n -s, --short"
  61. // Short format. Don't print the login time, JCPU or PCPU times.
  62. // "\n -f, --from"
  63. // Toggle printing the from (remote hostname) field.
  64. // The default is for the from field to not be printed
  65. // "\n -i, --ip-addr"
  66. // Display IP address instead of hostname for from field.
  67. // "\n -o, --old-style"
  68. // Old style output. Prints blank space for idle times less than one minute.
  69. // Example output:
  70. // 17:28:00 up 4 days, 22:41, 4 users, load average: 0.84, 0.97, 0.90
  71. // USER TTY LOGIN@ IDLE JCPU PCPU WHAT
  72. // root tty1 Thu18 4days 4:33m 0.07s /bin/sh /etc/xdg/xfce4/xinitrc -- vt
  73. // root pts/1 Mon13 3:24m 1:01 0.01s w
  74. //usage:#define who_trivial_usage
  75. //usage: "[-a]"
  76. //usage:#define who_full_usage "\n\n"
  77. //usage: "Show who is logged on\n"
  78. //usage: "\n -a Show all"
  79. //usage: "\n -H Print column headers"
  80. #include "libbb.h"
  81. static void idle_string(char *str6, time_t t)
  82. {
  83. t = time(NULL) - t;
  84. /*if (t < 60) {
  85. str6[0] = '.';
  86. str6[1] = '\0';
  87. return;
  88. }*/
  89. if (t >= 0 && t < (24 * 60 * 60)) {
  90. sprintf(str6, "%02d:%02d",
  91. (int) (t / (60 * 60)),
  92. (int) ((t % (60 * 60)) / 60));
  93. return;
  94. }
  95. strcpy(str6, "old");
  96. }
  97. int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  98. int who_main(int argc UNUSED_PARAM, char **argv)
  99. {
  100. #define CNT_APPLET (ENABLE_USERS + ENABLE_W + ENABLE_WHO)
  101. int do_users = (ENABLE_USERS && (CNT_APPLET == 1 || applet_name[0] == 'u'));
  102. int do_w = (ENABLE_W && (CNT_APPLET == 1 || applet_name[1] == '\0'));
  103. int do_who = (ENABLE_WHO && (CNT_APPLET == 1 || applet_name[1] == 'h'));
  104. struct utmpx *ut;
  105. unsigned opt;
  106. const char *fmt = "%s";
  107. opt = getopt32(argv, do_who ? "^" "aH" "\0" "=0": "^" "" "\0" "=0");
  108. if ((opt & 2) || do_w) /* -H or we are w */
  109. puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
  110. setutxent();
  111. while ((ut = getutxent()) != NULL) {
  112. if (ut->ut_user[0]
  113. && ((opt & 1) || ut->ut_type == USER_PROCESS)
  114. ) {
  115. if (!do_users) {
  116. char str6[6];
  117. char name[sizeof("/dev/") + sizeof(ut->ut_line) + 1];
  118. struct stat st;
  119. time_t seconds;
  120. str6[0] = '?';
  121. str6[1] = '\0';
  122. strcpy(name, "/dev/");
  123. safe_strncpy(ut->ut_line[0] == '/' ? name : name + sizeof("/dev/")-1,
  124. ut->ut_line,
  125. sizeof(ut->ut_line)+1
  126. );
  127. if (stat(name, &st) == 0)
  128. idle_string(str6, st.st_atime);
  129. /* manpages say ut_tv.tv_sec *is* time_t,
  130. * but some systems have it wrong */
  131. seconds = ut->ut_tv.tv_sec;
  132. /* How wide time field can be?
  133. * "Nov 10 19:33:20": 15 chars
  134. * "2010-11-10 19:33": 16 chars
  135. */
  136. printf("%-15.*s %-15.*s %-7s %-16.16s %.*s\n",
  137. (int)sizeof(ut->ut_user), ut->ut_user,
  138. (int)sizeof(ut->ut_line), ut->ut_line,
  139. str6,
  140. // TODO: with LANG=en_US.UTF-8, who from coreutils 8.25 shows
  141. // TIME col as "2017-04-06 18:47" (the default format is "Apr 6 18:47").
  142. // The former format looks saner to me. Switch to it unconditionally?
  143. ctime(&seconds) + 4,
  144. (int)sizeof(ut->ut_host), ut->ut_host
  145. );
  146. } else {
  147. printf(fmt, ut->ut_user);
  148. fmt = " %s";
  149. }
  150. }
  151. }
  152. if (do_users)
  153. bb_putchar('\n');
  154. if (ENABLE_FEATURE_CLEAN_UP)
  155. endutxent();
  156. return EXIT_SUCCESS;
  157. }