The Great Renaming
[platform/upstream/rpm.git] / build / names.c
1 /* names.c -- user/group name/id cache (plus hostname and buildtime) */
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <pwd.h>
6 #include <grp.h>
7 #include <string.h>
8 #include <time.h>
9 #include <unistd.h>
10 #include <netdb.h>
11 #include <errno.h>
12
13 #include "names.h"
14 #include "rpmlib.h"
15 #include "messages.h"
16
17 static uid_t uids[1024];
18 static char *unames[1024];
19 static int uid_used = 0;
20
21 static gid_t gids[1024];
22 static char *gnames[1024];
23 static int gid_used = 0;
24     
25 static time_t buildtime;
26
27 /*
28  * getUname() takes a uid, gets the username, and creates an entry in the
29  * table to hold a string containing the user name.
30  */
31 char *getUname(uid_t uid)
32 {
33     struct passwd *pw;
34     int x;
35
36     x = 0;
37     while (x < uid_used) {
38         if (uids[x] == uid) {
39             return unames[x];
40         }
41         x++;
42     }
43
44     /* XXX - This is the other hard coded limit */
45     if (x == 1024) {
46         fprintf(stderr, "RPMERR_INTERNAL: Hit limit in getUname()\n");
47         exit(RPMERR_INTERNAL);
48     }
49     
50     pw = getpwuid(uid);
51     uids[x] = uid;
52     uid_used++;
53     if (pw) {
54         unames[x] = strdup(pw->pw_name);
55     } else {
56         unames[x] = "";
57     }
58     return unames[x];
59 }
60
61 /*
62  * getUnameS() takes a username, gets the uid, and creates an entry in the
63  * table to hold a string containing the user name.
64  */
65 char *getUnameS(char *uname)
66 {
67     struct passwd *pw;
68     int x;
69
70     x = 0;
71     while (x < uid_used) {
72         if (!strcmp(unames[x],uname)) {
73             return unames[x];
74         }
75         x++;
76     }
77
78     /* XXX - This is the other hard coded limit */
79     if (x == 1024) {
80         fprintf(stderr, "RPMERR_INTERNAL: Hit limit in getUname()\n");
81         exit(RPMERR_INTERNAL);
82     }
83     
84     pw = getpwnam(uname);
85     uid_used++;
86     if (pw) {
87         uids[x] = pw->pw_uid;
88         unames[x] = strdup(pw->pw_name);
89     } else {
90         uids[x] = -1;
91         unames[x] = strdup(uname);
92     }
93     return unames[x];
94 }
95
96 /*
97  * getGname() takes a gid, gets the group name, and creates an entry in the
98  * table to hold a string containing the group name.
99  */
100 char *getGname(gid_t gid)
101 {
102     struct group *gr;
103     int x;
104
105     x = 0;
106     while (x < gid_used) {
107         if (gids[x] == gid) {
108             return gnames[x];
109         }
110         x++;
111     }
112
113     /* XXX - This is the other hard coded limit */
114     if (x == 1024) {
115         fprintf(stderr, "RPMERR_INTERNAL: Hit limit in getGname()\n");
116         exit(RPMERR_INTERNAL);
117     }
118     
119     gr = getgrgid(gid);
120     gids[x] = gid;
121     gid_used++;
122     if (gr) {
123         gnames[x] = strdup(gr->gr_name);
124     } else {
125         gnames[x] = "";
126     }
127     return gnames[x];
128 }
129
130 /*
131  * getGnameS() takes a group name, gets the gid, and creates an entry in the
132  * table to hold a string containing the group name.
133  */
134 char *getGnameS(char *gname)
135 {
136     struct group *gr;
137     int x;
138
139     x = 0;
140     while (x < gid_used) {
141         if (!strcmp(gnames[x], gname)) {
142             return gnames[x];
143         }
144         x++;
145     }
146
147     /* XXX - This is the other hard coded limit */
148     if (x == 1024) {
149         fprintf(stderr, "RPMERR_INTERNAL: Hit limit in getGname()\n");
150         exit(RPMERR_INTERNAL);
151     }
152     
153     gr = getgrnam(gname);
154     gid_used++;
155     if (gr) {
156         gids[x] = gr->gr_gid;
157         gnames[x] = strdup(gr->gr_name);
158     } else {
159         gids[x] = -1;
160         gnames[x] = strdup(gname);
161     }
162     return gnames[x];
163 }
164
165 void markBuildTime(void)
166 {
167     buildtime = time(NULL);
168 }
169
170 time_t *getBuildTime(void)
171 {
172     return &buildtime;
173 }
174
175 char *buildHost(void)
176 {
177     static char hostname[1024];
178     static int gotit = 0;
179     struct hostent *hbn;
180
181     if (! gotit) {
182         gethostname(hostname, sizeof(hostname));
183         if ((hbn = gethostbyname(hostname))) {
184             strcpy(hostname, hbn->h_name);
185         } else {
186             rpmMessage(RPMMESS_WARNING, "Could not canonicalize hostname: %s\n",
187                     hostname);
188         }
189         gotit = 1;
190     }
191     return(hostname);
192 }