who.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 tarball for details.
  16. *
  17. *----------------------------------------------------------------------
  18. */
  19. /* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -H, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
  20. #include "libbb.h"
  21. #include <utmp.h>
  22. #include <time.h>
  23. static void idle_string(char *str6, time_t t)
  24. {
  25. t = time(NULL) - t;
  26. /*if (t < 60) {
  27. str6[0] = '.';
  28. str6[1] = '\0';
  29. return;
  30. }*/
  31. if (t >= 0 && t < (24 * 60 * 60)) {
  32. sprintf(str6, "%02d:%02d",
  33. (int) (t / (60 * 60)),
  34. (int) ((t % (60 * 60)) / 60));
  35. return;
  36. }
  37. strcpy(str6, "old");
  38. }
  39. int who_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int who_main(int argc ATTRIBUTE_UNUSED, char **argv)
  41. {
  42. char str6[6];
  43. struct utmp *ut;
  44. struct stat st;
  45. char *name;
  46. unsigned opt;
  47. opt_complementary = "=0";
  48. opt = getopt32(argv, "a");
  49. setutent();
  50. printf("USER TTY IDLE TIME HOST\n");
  51. while ((ut = getutent()) != NULL) {
  52. if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
  53. time_t tmp;
  54. /* ut->ut_line is device name of tty - "/dev/" */
  55. name = concat_path_file("/dev", ut->ut_line);
  56. str6[0] = '?';
  57. str6[1] = '\0';
  58. if (stat(name, &st) == 0)
  59. idle_string(str6, st.st_atime);
  60. /* manpages say ut_tv.tv_sec *is* time_t,
  61. * but some systems have it wrong */
  62. tmp = ut->ut_tv.tv_sec;
  63. /* 15 chars for time: Nov 10 19:33:20 */
  64. printf("%-10s %-8s %-9s %-15.15s %s\n",
  65. ut->ut_user, ut->ut_line, str6,
  66. ctime(&tmp) + 4, ut->ut_host);
  67. if (ENABLE_FEATURE_CLEAN_UP)
  68. free(name);
  69. }
  70. }
  71. if (ENABLE_FEATURE_CLEAN_UP)
  72. endutent();
  73. return EXIT_SUCCESS;
  74. }