]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/tar/names.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / gnu / usr.bin / tar / names.c
1 /* Look up user and/or group names.
2    Copyright (C) 1988, 1992 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 <stdio.h>
34 #include <pwd.h>
35 #include <grp.h>
36
37 static int saveuid = -993;
38 static char saveuname[TUNMLEN];
39 static int my_uid = -993;
40
41 static int savegid = -993;
42 static char savegname[TGNMLEN];
43 static int my_gid = -993;
44
45 #define myuid   ( my_uid < 0? (my_uid = getuid()): my_uid )
46 #define mygid   ( my_gid < 0? (my_gid = getgid()): my_gid )
47
48 /*
49  * Look up a user or group name from a uid/gid, maintaining a cache.
50  * FIXME, for now it's a one-entry cache.
51  * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
52  *
53  * This is ifdef'd because on Suns, it drags in about 38K of "yellow
54  * pages" code, roughly doubling the program size.  Thanks guys.
55  */
56 void
57 finduname (uname, uid)
58      char uname[TUNMLEN];
59      int uid;
60 {
61   struct passwd *pw;
62 #ifndef HAVE_GETPWUID
63   extern struct passwd *getpwuid ();
64 #endif
65
66   if (uid != saveuid)
67     {
68       saveuid = uid;
69       saveuname[0] = '\0';
70       pw = getpwuid (uid);
71       if (pw)
72         strncpy (saveuname, pw->pw_name, TUNMLEN);
73     }
74   strncpy (uname, saveuname, TUNMLEN);
75 }
76
77 int
78 finduid (uname)
79      char uname[TUNMLEN];
80 {
81   struct passwd *pw;
82   extern struct passwd *getpwnam ();
83
84   if (uname[0] != saveuname[0]  /* Quick test w/o proc call */
85       || 0 != strncmp (uname, saveuname, TUNMLEN))
86     {
87       strncpy (saveuname, uname, TUNMLEN);
88       pw = getpwnam (uname);
89       if (pw)
90         {
91           saveuid = pw->pw_uid;
92         }
93       else
94         {
95           saveuid = myuid;
96         }
97     }
98   return saveuid;
99 }
100
101
102 void
103 findgname (gname, gid)
104      char gname[TGNMLEN];
105      int gid;
106 {
107   struct group *gr;
108 #ifndef HAVE_GETGRGID
109   extern struct group *getgrgid ();
110 #endif
111
112   if (gid != savegid)
113     {
114       savegid = gid;
115       savegname[0] = '\0';
116       (void) setgrent ();
117       gr = getgrgid (gid);
118       if (gr)
119         strncpy (savegname, gr->gr_name, TGNMLEN);
120     }
121   (void) strncpy (gname, savegname, TGNMLEN);
122 }
123
124
125 int
126 findgid (gname)
127      char gname[TUNMLEN];
128 {
129   struct group *gr;
130   extern struct group *getgrnam ();
131
132   if (gname[0] != savegname[0]  /* Quick test w/o proc call */
133       || 0 != strncmp (gname, savegname, TUNMLEN))
134     {
135       strncpy (savegname, gname, TUNMLEN);
136       gr = getgrnam (gname);
137       if (gr)
138         {
139           savegid = gr->gr_gid;
140         }
141       else
142         {
143           savegid = mygid;
144         }
145     }
146   return savegid;
147 }
148
149 #endif