UsersTable.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. htop - UsersTable.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "UsersTable.h"
  9. #include <pwd.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "XUtils.h"
  14. UsersTable* UsersTable_new(void) {
  15. UsersTable* this;
  16. this = xMalloc(sizeof(UsersTable));
  17. this->users = Hashtable_new(10, true);
  18. return this;
  19. }
  20. void UsersTable_delete(UsersTable* this) {
  21. Hashtable_delete(this->users);
  22. free(this);
  23. }
  24. char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
  25. char* name = Hashtable_get(this->users, uid);
  26. if (name == NULL) {
  27. const struct passwd* userData = getpwuid(uid);
  28. if (userData != NULL) {
  29. name = xStrdup(userData->pw_name);
  30. Hashtable_put(this->users, uid, name);
  31. }
  32. }
  33. return name;
  34. }
  35. inline void UsersTable_foreach(UsersTable* this, Hashtable_PairFunction f, void* userData) {
  36. Hashtable_foreach(this->users, f, userData);
  37. }