3 * Simple user/group name/id cache (plus hostname and buildtime)
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
15 typedef char * ugstr_t;
17 static uid_t uids[UGIDMAX];
18 static ugstr_t unames[UGIDMAX];
19 static int uid_used = 0;
21 static gid_t gids[UGIDMAX];
22 static ugstr_t gnames[UGIDMAX];
23 static int gid_used = 0;
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]);
34 const char *getUname(uid_t uid)
39 for (x = 0; x < uid_used; x++) {
40 if (unames[x] == NULL) continue;
45 /* XXX - This is the other hard coded limit */
47 rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
49 if ((pw = getpwuid(uid)) == NULL)
52 unames[uid_used] = xstrdup(pw->pw_name);
53 return unames[uid_used++];
56 const char *getUnameS(const char *uname)
61 for (x = 0; x < uid_used; x++) {
62 if (unames[x] == NULL) continue;
63 if (rstreq(unames[x],uname))
67 /* XXX - This is the other hard coded limit */
69 rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
71 if ((pw = getpwnam(uname)) == NULL) {
73 unames[uid_used] = xstrdup(uname);
75 uids[uid_used] = pw->pw_uid;
76 unames[uid_used] = xstrdup(pw->pw_name);
78 return unames[uid_used++];
81 uid_t getUidS(const char *uname)
86 for (x = 0; x < uid_used; x++) {
87 if (unames[x] == NULL) continue;
88 if (rstreq(unames[x],uname))
92 /* XXX - This is the other hard coded limit */
94 rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
96 if ((pw = getpwnam(uname)) == NULL) {
98 unames[uid_used] = xstrdup(uname);
100 uids[uid_used] = pw->pw_uid;
101 unames[uid_used] = xstrdup(pw->pw_name);
103 return uids[uid_used++];
106 const char *getGname(gid_t gid)
111 for (x = 0; x < gid_used; x++) {
112 if (gnames[x] == NULL) continue;
117 /* XXX - This is the other hard coded limit */
119 rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
121 if ((gr = getgrgid(gid)) == NULL)
123 gids[gid_used] = gid;
124 gnames[gid_used] = xstrdup(gr->gr_name);
125 return gnames[gid_used++];
128 const char *getGnameS(const char *gname)
133 for (x = 0; x < gid_used; x++) {
134 if (gnames[x] == NULL) continue;
135 if (rstreq(gnames[x], gname))
139 /* XXX - This is the other hard coded limit */
141 rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
143 if ((gr = getgrnam(gname)) == NULL) {
145 gnames[gid_used] = xstrdup(gname);
147 gids[gid_used] = gr->gr_gid;
148 gnames[gid_used] = xstrdup(gr->gr_name);
150 return gnames[gid_used++];
153 gid_t getGidS(const char *gname)
158 for (x = 0; x < gid_used; x++) {
159 if (gnames[x] == NULL) continue;
160 if (rstreq(gnames[x], gname))
164 /* XXX - This is the other hard coded limit */
166 rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
168 if ((gr = getgrnam(gname)) == NULL) {
170 gnames[gid_used] = xstrdup(gname);
172 gids[gid_used] = gr->gr_gid;
173 gnames[gid_used] = xstrdup(gr->gr_name);
175 return gids[gid_used++];
178 rpm_time_t * getBuildTime(void)
180 static rpm_time_t buildTime[1];
182 if (buildTime[0] == 0)
183 buildTime[0] = (int32_t) time(NULL);
187 const char * buildHost(void)
189 static char hostname[1024];
190 static int oneshot = 0;
194 (void) gethostname(hostname, sizeof(hostname));
195 hbn = gethostbyname(hostname);
197 strcpy(hostname, hbn->h_name);
199 rpmlog(RPMLOG_WARNING,
200 _("Could not canonicalize hostname: %s\n"), hostname);