names.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void finduname (char *uname, int uid)
  50. {
  51. struct passwd *pw;
  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 finduid (char *uname)
  62. {
  63. struct passwd *pw;
  64. extern struct passwd *getpwnam ();
  65. if (uname[0] != saveuname[0]/* Quick test w/o proc call */
  66. ||0 != strncmp (uname, saveuname, TUNMLEN)) {
  67. strncpy (saveuname, uname, TUNMLEN);
  68. pw = getpwnam (uname);
  69. if (pw) {
  70. saveuid = pw->pw_uid;
  71. } else {
  72. saveuid = myuid;
  73. }
  74. }
  75. return saveuid;
  76. }
  77. void findgname (char *gname, int gid)
  78. {
  79. struct group *gr;
  80. if (gid != savegid) {
  81. savegid = gid;
  82. savegname[0] = '\0';
  83. (void) setgrent ();
  84. gr = getgrgid (gid);
  85. if (gr)
  86. strncpy (savegname, gr->gr_name, TGNMLEN);
  87. }
  88. (void) strncpy (gname, savegname, TGNMLEN);
  89. }
  90. int findgid (char *gname)
  91. {
  92. struct group *gr;
  93. extern struct group *getgrnam ();
  94. if (gname[0] != savegname[0]/* Quick test w/o proc call */
  95. ||0 != strncmp (gname, savegname, TUNMLEN)) {
  96. strncpy (savegname, gname, TUNMLEN);
  97. gr = getgrnam (gname);
  98. if (gr) {
  99. savegid = gr->gr_gid;
  100. } else {
  101. savegid = mygid;
  102. }
  103. }
  104. return savegid;
  105. }