names.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Look up user and/or group names.
  2. Copyright (C) 1988, 1992 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * Look up user and/or group names.
  17. *
  18. * This file should be modified for non-unix systems to do something
  19. * reasonable.
  20. */
  21. #include <sys/types.h>
  22. #include "tar.h"
  23. #include "port.h"
  24. #ifndef NONAMES
  25. /* Whole module goes away if NONAMES defined. Otherwise... */
  26. #include <stdio.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. static int saveuid = -993;
  30. static char saveuname[TUNMLEN];
  31. static int my_uid = -993;
  32. static int savegid = -993;
  33. static char savegname[TGNMLEN];
  34. static int my_gid = -993;
  35. #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
  36. #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
  37. /*
  38. * Look up a user or group name from a uid/gid, maintaining a cache.
  39. * FIXME, for now it's a one-entry cache.
  40. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  41. *
  42. * This is ifdef'd because on Suns, it drags in about 38K of "yellow
  43. * pages" code, roughly doubling the program size. Thanks guys.
  44. */
  45. void
  46. finduname(uname, uid)
  47. char uname[TUNMLEN];
  48. int uid;
  49. {
  50. struct passwd *pw;
  51. extern struct passwd *getpwuid ();
  52. if (uid != saveuid) {
  53. saveuid = uid;
  54. saveuname[0] = '\0';
  55. pw = getpwuid(uid);
  56. if (pw)
  57. strncpy(saveuname, pw->pw_name, TUNMLEN);
  58. }
  59. strncpy(uname, saveuname, TUNMLEN);
  60. }
  61. int
  62. finduid(uname)
  63. char uname[TUNMLEN];
  64. {
  65. struct passwd *pw;
  66. extern struct passwd *getpwnam();
  67. if (uname[0] != saveuname[0] /* Quick test w/o proc call */
  68. || 0!=strncmp(uname, saveuname, TUNMLEN)) {
  69. strncpy(saveuname, uname, TUNMLEN);
  70. pw = getpwnam(uname);
  71. if (pw) {
  72. saveuid = pw->pw_uid;
  73. } else {
  74. saveuid = myuid;
  75. }
  76. }
  77. return saveuid;
  78. }
  79. void
  80. findgname(gname, gid)
  81. char gname[TGNMLEN];
  82. int gid;
  83. {
  84. struct group *gr;
  85. extern struct group *getgrgid ();
  86. if (gid != savegid) {
  87. savegid = gid;
  88. savegname[0] = '\0';
  89. (void)setgrent();
  90. gr = getgrgid(gid);
  91. if (gr)
  92. strncpy(savegname, gr->gr_name, TGNMLEN);
  93. }
  94. (void) strncpy(gname, savegname, TGNMLEN);
  95. }
  96. int
  97. findgid(gname)
  98. char gname[TUNMLEN];
  99. {
  100. struct group *gr;
  101. extern struct group *getgrnam();
  102. if (gname[0] != savegname[0] /* Quick test w/o proc call */
  103. || 0!=strncmp(gname, savegname, TUNMLEN)) {
  104. strncpy(savegname, gname, TUNMLEN);
  105. gr = getgrnam(gname);
  106. if (gr) {
  107. savegid = gr->gr_gid;
  108. } else {
  109. savegid = mygid;
  110. }
  111. }
  112. return savegid;
  113. }
  114. #endif