names.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Look up user and/or group names.
  2. Copyright (C) 1988, 1992 Free Software Foundation
  3. From GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Library General Public License as published
  6. by 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 Library General Public License for more details.
  12. * Namespace: finduname, finduid, findgname, findgid.
  13. */
  14. /*
  15. * Look up user and/or group names.
  16. *
  17. * This file should be modified for non-unix systems to do something
  18. * reasonable.
  19. */
  20. #include <config.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #define TAR_NAMES
  25. #include "tar.h"
  26. #include "names.h"
  27. #include <stdio.h>
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #ifndef TUNMLEN
  31. #define TUNMLEN 256
  32. #endif
  33. #ifndef TGNMLEN
  34. #define TGNMLEN 256
  35. #endif
  36. static int saveuid = -993;
  37. static char saveuname[TUNMLEN];
  38. static int my_uid = -993;
  39. static int savegid = -993;
  40. static char savegname[TGNMLEN];
  41. static int my_gid = -993;
  42. #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
  43. #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
  44. /*
  45. * Look up a user or group name from a uid/gid, maintaining a cache.
  46. * FIXME, for now it's a one-entry cache.
  47. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  48. *
  49. * This is ifdef'd because on Suns, it drags in about 38K of "yellow
  50. * pages" code, roughly doubling the program size. Thanks guys.
  51. */
  52. void finduname (char *uname, int uid)
  53. {
  54. struct passwd *pw;
  55. #ifndef HAVE_GETPWUID
  56. extern struct passwd *getpwuid ();
  57. #endif
  58. if (uid != saveuid) {
  59. saveuid = uid;
  60. saveuname[0] = '\0';
  61. pw = getpwuid (uid);
  62. if (pw)
  63. strncpy (saveuname, pw->pw_name, TUNMLEN);
  64. }
  65. strncpy (uname, saveuname, TUNMLEN);
  66. }
  67. int finduid (char *uname)
  68. {
  69. struct passwd *pw;
  70. extern struct passwd *getpwnam ();
  71. if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  72. ||0 != strncmp (uname, saveuname, TUNMLEN)) {
  73. strncpy (saveuname, uname, TUNMLEN);
  74. pw = getpwnam (uname);
  75. if (pw) {
  76. saveuid = pw->pw_uid;
  77. } else {
  78. saveuid = myuid;
  79. }
  80. }
  81. return saveuid;
  82. }
  83. void findgname (char *gname, int gid)
  84. {
  85. struct group *gr;
  86. #ifndef HAVE_GETGRGID
  87. extern struct group *getgrgid ();
  88. #endif
  89. if (gid != savegid) {
  90. savegid = gid;
  91. savegname[0] = '\0';
  92. (void) setgrent ();
  93. gr = getgrgid (gid);
  94. if (gr)
  95. strncpy (savegname, gr->gr_name, TGNMLEN);
  96. }
  97. (void) strncpy (gname, savegname, TGNMLEN);
  98. }
  99. int findgid (char *gname)
  100. {
  101. struct group *gr;
  102. extern struct group *getgrnam ();
  103. if (gname[0] != savegname[0]/* Quick test w/o proc call */
  104. ||0 != strncmp (gname, savegname, TUNMLEN)) {
  105. strncpy (savegname, gname, TUNMLEN);
  106. gr = getgrnam (gname);
  107. if (gr) {
  108. savegid = gr->gr_gid;
  109. } else {
  110. savegid = mygid;
  111. }
  112. }
  113. return savegid;
  114. }