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