Permit file objects in python header constructor
[platform/upstream/rpm.git] / build / names.c
1 /** \ingroup rpmbuild
2  * \file build/names.c
3  * Simple user/group name/id cache (plus hostname and buildtime)
4  */
5
6
7 #include "system.h"
8
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
11 #include "debug.h"
12
13 #define UGIDMAX 1024
14
15 typedef char * ugstr_t;
16
17 static uid_t uids[UGIDMAX];
18 static ugstr_t unames[UGIDMAX];
19 static int uid_used = 0;
20
21 static gid_t gids[UGIDMAX];
22 static ugstr_t gnames[UGIDMAX];
23 static int gid_used = 0;
24     
25 void freeNames(void)
26 {
27     int x;
28     for (x = 0; x < uid_used; x++)
29         unames[x] = _free(unames[x]);
30     for (x = 0; x < gid_used; x++)
31         gnames[x] = _free(gnames[x]);
32 }
33
34 const char *getUname(uid_t uid)
35 {
36     struct passwd *pw;
37     int x;
38
39     for (x = 0; x < uid_used; x++) {
40         if (unames[x] == NULL) continue;
41         if (uids[x] == uid)
42             return unames[x];
43     }
44
45     /* XXX - This is the other hard coded limit */
46     if (x == UGIDMAX)
47         rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
48     
49     if ((pw = getpwuid(uid)) == NULL)
50         return NULL;
51     uids[uid_used] = uid;
52     unames[uid_used] = xstrdup(pw->pw_name);
53     return unames[uid_used++];
54 }
55
56 const char *getUnameS(const char *uname)
57 {
58     struct passwd *pw;
59     int x;
60
61     for (x = 0; x < uid_used; x++) {
62         if (unames[x] == NULL) continue;
63         if (rstreq(unames[x],uname))
64             return unames[x];
65     }
66
67     /* XXX - This is the other hard coded limit */
68     if (x == UGIDMAX)
69         rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
70     
71     if ((pw = getpwnam(uname)) == NULL) {
72         uids[uid_used] = -1;
73         unames[uid_used] = xstrdup(uname);
74     } else {
75         uids[uid_used] = pw->pw_uid;
76         unames[uid_used] = xstrdup(pw->pw_name);
77     }
78     return unames[uid_used++];
79 }
80
81 uid_t getUidS(const char *uname)
82 {
83     struct passwd *pw;
84     int x;
85
86     for (x = 0; x < uid_used; x++) {
87         if (unames[x] == NULL) continue;
88         if (rstreq(unames[x],uname))
89             return uids[x];
90     }
91
92     /* XXX - This is the other hard coded limit */
93     if (x == UGIDMAX)
94         rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
95     
96     if ((pw = getpwnam(uname)) == NULL) {
97         uids[uid_used] = -1;
98         unames[uid_used] = xstrdup(uname);
99     } else {
100         uids[uid_used] = pw->pw_uid;
101         unames[uid_used] = xstrdup(pw->pw_name);
102     }
103     return uids[uid_used++];
104 }
105
106 const char *getGname(gid_t gid)
107 {
108     struct group *gr;
109     int x;
110
111     for (x = 0; x < gid_used; x++) {
112         if (gnames[x] == NULL) continue;
113         if (gids[x] == gid)
114             return gnames[x];
115     }
116
117     /* XXX - This is the other hard coded limit */
118     if (x == UGIDMAX)
119         rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
120     
121     if ((gr = getgrgid(gid)) == NULL)
122         return NULL;
123     gids[gid_used] = gid;
124     gnames[gid_used] = xstrdup(gr->gr_name);
125     return gnames[gid_used++];
126 }
127
128 const char *getGnameS(const char *gname)
129 {
130     struct group *gr;
131     int x;
132
133     for (x = 0; x < gid_used; x++) {
134         if (gnames[x] == NULL) continue;
135         if (rstreq(gnames[x], gname))
136             return gnames[x];
137     }
138
139     /* XXX - This is the other hard coded limit */
140     if (x == UGIDMAX)
141         rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
142     
143     if ((gr = getgrnam(gname)) == NULL) {
144         gids[gid_used] = -1;
145         gnames[gid_used] = xstrdup(gname);
146     } else {
147         gids[gid_used] = gr->gr_gid;
148         gnames[gid_used] = xstrdup(gr->gr_name);
149     }
150     return gnames[gid_used++];
151 }
152
153 gid_t getGidS(const char *gname)
154 {
155     struct group *gr;
156     int x;
157
158     for (x = 0; x < gid_used; x++) {
159         if (gnames[x] == NULL) continue;
160         if (rstreq(gnames[x], gname))
161             return gids[x];
162     }
163
164     /* XXX - This is the other hard coded limit */
165     if (x == UGIDMAX)
166         rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
167     
168     if ((gr = getgrnam(gname)) == NULL) {
169         gids[gid_used] = -1;
170         gnames[gid_used] = xstrdup(gname);
171     } else {
172         gids[gid_used] = gr->gr_gid;
173         gnames[gid_used] = xstrdup(gr->gr_name);
174     }
175     return gids[gid_used++];
176 }
177
178 rpm_time_t * getBuildTime(void)
179 {
180     static rpm_time_t buildTime[1];
181
182     if (buildTime[0] == 0)
183         buildTime[0] = (int32_t) time(NULL);
184     return buildTime;
185 }
186
187 const char * buildHost(void)
188 {
189     static char hostname[1024];
190     static int oneshot = 0;
191     struct hostent *hbn;
192
193     if (! oneshot) {
194         (void) gethostname(hostname, sizeof(hostname));
195         hbn = gethostbyname(hostname);
196         if (hbn)
197             strcpy(hostname, hbn->h_name);
198         else
199             rpmlog(RPMLOG_WARNING,
200                         _("Could not canonicalize hostname: %s\n"), hostname);
201         oneshot = 1;
202     }
203     return(hostname);
204 }