who.c 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* who.c - display who is on the system
  2. *
  3. * Copyright 2012 ProFUSION Embedded Systems
  4. *
  5. * by Luis Felipe Strano Moraes <lfelipe@profusion.mobi>
  6. *
  7. * See http://opengroup.org/onlinepubs/9699919799/utilities/who.html
  8. *
  9. * Posix says to support many options (-abdHlmpqrstTu) but this
  10. * isn't aimed at minicomputers with modem pools.
  11. *
  12. * TODO: -a doesn't know how to format other entries
  13. USE_WHO(NEWTOY(who, "a", TOYFLAG_USR|TOYFLAG_BIN))
  14. config WHO
  15. bool "who"
  16. default y
  17. depends on TOYBOX_UTMPX
  18. help
  19. usage: who
  20. Print information about logged in users.
  21. */
  22. #define FOR_who
  23. #include "toys.h"
  24. void who_main(void)
  25. {
  26. struct utmpx *entry;
  27. setutxent();
  28. while ((entry = getutxent())) {
  29. if (FLAG(a) || entry->ut_type == USER_PROCESS) {
  30. time_t t = entry->ut_tv.tv_sec;
  31. struct tm *tm = localtime(&t);
  32. strftime(toybuf, sizeof(toybuf), "%F %H:%M", tm);
  33. printf("%s\t%s\t%s (%s)\n", entry->ut_user, entry->ut_line,
  34. toybuf, entry->ut_host);
  35. }
  36. }
  37. endutxent();
  38. }