PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/top/username.c

https://bitbucket.org/freebsd/freebsd-head/
C | 189 lines | 85 code | 35 blank | 69 comment | 17 complexity | f140f21cf7e14cbae2f2340739e95d64 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Top users/processes display for Unix
  3. * Version 3
  4. *
  5. * This program may be freely redistributed,
  6. * but this entire comment MUST remain intact.
  7. *
  8. * Copyright (c) 1984, 1989, William LeFebvre, Rice University
  9. * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
  10. *
  11. * $FreeBSD$
  12. */
  13. /*
  14. * Username translation code for top.
  15. *
  16. * These routines handle uid to username mapping.
  17. * They use a hashing table scheme to reduce reading overhead.
  18. * For the time being, these are very straightforward hashing routines.
  19. * Maybe someday I'll put in something better. But with the advent of
  20. * "random access" password files, it might not be worth the effort.
  21. *
  22. * Changes to these have been provided by John Gilmore (gnu@toad.com).
  23. *
  24. * The hash has been simplified in this release, to avoid the
  25. * table overflow problems of previous releases. If the value
  26. * at the initial hash location is not right, it is replaced
  27. * by the right value. Collisions will cause us to call getpw*
  28. * but hey, this is a cache, not the Library of Congress.
  29. * This makes the table size independent of the passwd file size.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/types.h>
  33. #include <stdio.h>
  34. #include <pwd.h>
  35. #include "top.local.h"
  36. #include "utils.h"
  37. struct hash_el {
  38. int uid;
  39. char name[MAXLOGNAME];
  40. };
  41. #define is_empty_hash(x) (hash_table[x].name[0] == 0)
  42. /* simple minded hashing function */
  43. /* Uid "nobody" is -2 results in hashit(-2) = -2 which is out of bounds for
  44. the hash_table. Applied abs() function to fix. 2/16/96 tpugh
  45. */
  46. #define hashit(i) (abs(i) % Table_size)
  47. /* K&R requires that statically declared tables be initialized to zero. */
  48. /* We depend on that for hash_table and YOUR compiler had BETTER do it! */
  49. struct hash_el hash_table[Table_size];
  50. init_hash()
  51. {
  52. /*
  53. * There used to be some steps we had to take to initialize things.
  54. * We don't need to do that anymore, but we will leave this stub in
  55. * just in case future changes require initialization steps.
  56. */
  57. }
  58. char *username(uid)
  59. register int uid;
  60. {
  61. register int hashindex;
  62. hashindex = hashit(uid);
  63. if (is_empty_hash(hashindex) || (hash_table[hashindex].uid != uid))
  64. {
  65. /* not here or not right -- get it out of passwd */
  66. hashindex = get_user(uid);
  67. }
  68. return(hash_table[hashindex].name);
  69. }
  70. int userid(username)
  71. char *username;
  72. {
  73. struct passwd *pwd;
  74. /* Eventually we want this to enter everything in the hash table,
  75. but for now we just do it simply and remember just the result.
  76. */
  77. if ((pwd = getpwnam(username)) == NULL)
  78. {
  79. return(-1);
  80. }
  81. /* enter the result in the hash table */
  82. enter_user(pwd->pw_uid, username, 1);
  83. /* return our result */
  84. return(pwd->pw_uid);
  85. }
  86. int enter_user(uid, name, wecare)
  87. register int uid;
  88. register char *name;
  89. int wecare; /* 1 = enter it always, 0 = nice to have */
  90. {
  91. register int hashindex;
  92. #ifdef DEBUG
  93. fprintf(stderr, "enter_hash(%d, %s, %d)\n", uid, name, wecare);
  94. #endif
  95. hashindex = hashit(uid);
  96. if (!is_empty_hash(hashindex))
  97. {
  98. if (!wecare)
  99. return 0; /* Don't clobber a slot for trash */
  100. if (hash_table[hashindex].uid == uid)
  101. return(hashindex); /* Fortuitous find */
  102. }
  103. /* empty or wrong slot -- fill it with new value */
  104. hash_table[hashindex].uid = uid;
  105. (void) strncpy(hash_table[hashindex].name, name, MAXLOGNAME - 1);
  106. return(hashindex);
  107. }
  108. /*
  109. * Get a userid->name mapping from the system.
  110. * If the passwd database is hashed (#define RANDOM_PW), we
  111. * just handle this uid. Otherwise we scan the passwd file
  112. * and cache any entries we pass over while looking.
  113. */
  114. int get_user(uid)
  115. register int uid;
  116. {
  117. struct passwd *pwd;
  118. #ifdef RANDOM_PW
  119. /* no performance penalty for using getpwuid makes it easy */
  120. if ((pwd = getpwuid(uid)) != NULL)
  121. {
  122. return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
  123. }
  124. #else
  125. int from_start = 0;
  126. /*
  127. * If we just called getpwuid each time, things would be very slow
  128. * since that just iterates through the passwd file each time. So,
  129. * we walk through the file instead (using getpwent) and cache each
  130. * entry as we go. Once the right record is found, we cache it and
  131. * return immediately. The next time we come in, getpwent will get
  132. * the next record. In theory, we never have to read the passwd file
  133. * a second time (because we cache everything we read). But in
  134. * practice, the cache may not be large enough, so if we don't find
  135. * it the first time we have to scan the file a second time. This
  136. * is not very efficient, but it will do for now.
  137. */
  138. while (from_start++ < 2)
  139. {
  140. while ((pwd = getpwent()) != NULL)
  141. {
  142. if (pwd->pw_uid == uid)
  143. {
  144. return(enter_user(pwd->pw_uid, pwd->pw_name, 1));
  145. }
  146. (void) enter_user(pwd->pw_uid, pwd->pw_name, 0);
  147. }
  148. /* try again */
  149. setpwent();
  150. }
  151. #endif
  152. /* if we can't find the name at all, then use the uid as the name */
  153. return(enter_user(uid, itoa7(uid), 1));
  154. }