]> Dogcows Code - chaz/tar/blob - src/names.c
Initial revision
[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 * @(#)names.c 1.3 10/30/87 - gnu
27 */
28 #include <sys/types.h>
29 #include "tar.h"
30
31 extern char *strncpy();
32
33 #ifndef NONAMES
34 /* Whole module goes away if NONAMES defined. Otherwise... */
35 #include <pwd.h>
36 #include <grp.h>
37
38 static int saveuid = -993;
39 static char saveuname[TUNMLEN];
40 static int my_uid = -993;
41
42 static int savegid = -993;
43 static char savegname[TGNMLEN];
44 static int my_gid = -993;
45
46 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
47 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
48
49 /*
50 * Look up a user or group name from a uid/gid, maintaining a cache.
51 * FIXME, for now it's a one-entry cache.
52 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
53 *
54 * This is ifdef'd because on Suns, it drags in about 38K of "yellow
55 * pages" code, roughly doubling the program size. Thanks guys.
56 */
57 void
58 finduname(uname, uid)
59 char uname[TUNMLEN];
60 int uid;
61 {
62 struct passwd *pw;
63 extern struct passwd *getpwuid ();
64
65 if (uid != saveuid) {
66 saveuid = uid;
67 saveuname[0] = '\0';
68 pw = getpwuid(uid);
69 if (pw)
70 strncpy(saveuname, pw->pw_name, TUNMLEN);
71 }
72 strncpy(uname, saveuname, TUNMLEN);
73 }
74
75 int
76 finduid(uname)
77 char uname[TUNMLEN];
78 {
79 struct passwd *pw;
80 extern struct passwd *getpwnam();
81
82 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
83 || 0!=strncmp(uname, saveuname, TUNMLEN)) {
84 strncpy(saveuname, uname, TUNMLEN);
85 pw = getpwnam(uname);
86 if (pw) {
87 saveuid = pw->pw_uid;
88 } else {
89 saveuid = myuid;
90 }
91 }
92 return saveuid;
93 }
94
95
96 void
97 findgname(gname, gid)
98 char gname[TGNMLEN];
99 int gid;
100 {
101 struct group *gr;
102 extern struct group *getgrgid ();
103
104 if (gid != savegid) {
105 savegid = gid;
106 savegname[0] = '\0';
107 (void)setgrent();
108 gr = getgrgid(gid);
109 if (gr)
110 strncpy(savegname, gr->gr_name, TGNMLEN);
111 }
112 (void) strncpy(gname, savegname, TGNMLEN);
113 }
114
115
116 int
117 findgid(gname)
118 char gname[TUNMLEN];
119 {
120 struct group *gr;
121 extern struct group *getgrnam();
122
123 if (gname[0] != savegname[0] /* Quick test w/o proc call */
124 || 0!=strncmp(gname, savegname, TUNMLEN)) {
125 strncpy(savegname, gname, TUNMLEN);
126 gr = getgrnam(gname);
127 if (gr) {
128 savegid = gr->gr_gid;
129 } else {
130 savegid = mygid;
131 }
132 }
133 return savegid;
134 }
135 #endif
This page took 0.045581 seconds and 5 git commands to generate.