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