build/names.c

Go to the documentation of this file.
00001 /*@-mods@*/
00008 #include "system.h"
00009 
00010 #include "rpmbuild.h"
00011 #include "debug.h"
00012 
00013 typedef /*@owned@*/ /*@null@*/ const char * ugstr_t;
00014 
00015 /*@unchecked@*/
00016 static uid_t uids[1024];
00017 /*@unchecked@*/
00018 static ugstr_t unames[1024];
00019 /*@unchecked@*/
00020 static int uid_used = 0;
00021 
00022 /*@unchecked@*/
00023 static gid_t gids[1024];
00024 /*@unchecked@*/
00025 static ugstr_t gnames[1024];
00026 /*@unchecked@*/
00027 static int gid_used = 0;
00028     
00029 /*@-boundswrite@*/
00030 void freeNames(void)
00031 {
00032     int x;
00033     for (x = 0; x < uid_used; x++)
00034         unames[x] = _free(unames[x]);
00035     for (x = 0; x < gid_used; x++)
00036         gnames[x] = _free(gnames[x]);
00037 }
00038 /*@=boundswrite@*/
00039 
00040 /*@-boundswrite@*/
00041 const char *getUname(uid_t uid)
00042         /*@globals uid_used, uids, unames @*/
00043         /*@modifies uid_used, uids, unames @*/
00044 {
00045     struct passwd *pw;
00046     int x;
00047 
00048     for (x = 0; x < uid_used; x++) {
00049         if (unames[x] == NULL) continue;
00050         if (uids[x] == uid)
00051             return unames[x];
00052     }
00053 
00054     /* XXX - This is the other hard coded limit */
00055     if (x == 1024)
00056         rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
00057     
00058     if ((pw = getpwuid(uid)) == NULL)
00059         return NULL;
00060     uids[uid_used] = uid;
00061     unames[uid_used] = xstrdup(pw->pw_name);
00062     return unames[uid_used++];
00063 }
00064 /*@=boundswrite@*/
00065 
00066 /*@-boundswrite@*/
00067 const char *getUnameS(const char *uname)
00068         /*@globals uid_used, uids, unames @*/
00069         /*@modifies uid_used, uids, unames @*/
00070 {
00071     struct passwd *pw;
00072     int x;
00073 
00074     for (x = 0; x < uid_used; x++) {
00075         if (unames[x] == NULL) continue;
00076         if (!strcmp(unames[x],uname))
00077             return unames[x];
00078     }
00079 
00080     /* XXX - This is the other hard coded limit */
00081     if (x == 1024)
00082         rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
00083     
00084     if ((pw = getpwnam(uname)) == NULL) {
00085         uids[uid_used] = -1;
00086         unames[uid_used] = xstrdup(uname);
00087     } else {
00088         uids[uid_used] = pw->pw_uid;
00089         unames[uid_used] = xstrdup(pw->pw_name);
00090     }
00091     return unames[uid_used++];
00092 }
00093 /*@=boundswrite@*/
00094 
00095 /*@-boundswrite@*/
00096 uid_t getUidS(const char *uname)
00097         /*@globals uid_used, uids, unames @*/
00098         /*@modifies uid_used, uids, unames @*/
00099 {
00100     struct passwd *pw;
00101     int x;
00102 
00103     for (x = 0; x < uid_used; x++) {
00104         if (unames[x] == NULL) continue;
00105         if (!strcmp(unames[x],uname))
00106             return uids[x];
00107     }
00108 
00109     /* XXX - This is the other hard coded limit */
00110     if (x == 1024)
00111         rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
00112     
00113     if ((pw = getpwnam(uname)) == NULL) {
00114         uids[uid_used] = -1;
00115         unames[uid_used] = xstrdup(uname);
00116     } else {
00117         uids[uid_used] = pw->pw_uid;
00118         unames[uid_used] = xstrdup(pw->pw_name);
00119     }
00120     return uids[uid_used++];
00121 }
00122 /*@=boundswrite@*/
00123 
00124 /*@-boundswrite@*/
00125 const char *getGname(gid_t gid)
00126         /*@globals gid_used, gids, gnames @*/
00127         /*@modifies gid_used, gids, gnames @*/
00128 {
00129     struct group *gr;
00130     int x;
00131 
00132     for (x = 0; x < gid_used; x++) {
00133         if (gnames[x] == NULL) continue;
00134         if (gids[x] == gid)
00135             return gnames[x];
00136     }
00137 
00138     /* XXX - This is the other hard coded limit */
00139     if (x == 1024)
00140         rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
00141     
00142     if ((gr = getgrgid(gid)) == NULL)
00143         return NULL;
00144     gids[gid_used] = gid;
00145     gnames[gid_used] = xstrdup(gr->gr_name);
00146     return gnames[gid_used++];
00147 }
00148 /*@=boundswrite@*/
00149 
00150 /*@-boundswrite@*/
00151 const char *getGnameS(const char *gname)
00152         /*@globals gid_used, gids, gnames @*/
00153         /*@modifies gid_used, gids, gnames @*/
00154 {
00155     struct group *gr;
00156     int x;
00157 
00158     for (x = 0; x < gid_used; x++) {
00159         if (gnames[x] == NULL) continue;
00160         if (!strcmp(gnames[x], gname))
00161             return gnames[x];
00162     }
00163 
00164     /* XXX - This is the other hard coded limit */
00165     if (x == 1024)
00166         rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
00167     
00168     if ((gr = getgrnam(gname)) == NULL) {
00169         gids[gid_used] = -1;
00170         gnames[gid_used] = xstrdup(gname);
00171     } else {
00172         gids[gid_used] = gr->gr_gid;
00173         gnames[gid_used] = xstrdup(gr->gr_name);
00174     }
00175     return gnames[gid_used++];
00176 }
00177 /*@=boundswrite@*/
00178 
00179 /*@-boundswrite@*/
00180 gid_t getGidS(const char *gname)
00181         /*@globals gid_used, gids, gnames @*/
00182         /*@modifies gid_used, gids, gnames @*/
00183 {
00184     struct group *gr;
00185     int x;
00186 
00187     for (x = 0; x < gid_used; x++) {
00188         if (gnames[x] == NULL) continue;
00189         if (!strcmp(gnames[x], gname))
00190             return gids[x];
00191     }
00192 
00193     /* XXX - This is the other hard coded limit */
00194     if (x == 1024)
00195         rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
00196     
00197     if ((gr = getgrnam(gname)) == NULL) {
00198         gids[gid_used] = -1;
00199         gnames[gid_used] = xstrdup(gname);
00200     } else {
00201         gids[gid_used] = gr->gr_gid;
00202         gnames[gid_used] = xstrdup(gr->gr_name);
00203     }
00204     return gids[gid_used++];
00205 }
00206 /*@=boundswrite@*/
00207 
00208 int_32 *const getBuildTime(void)
00209 {
00210     static int_32 buildTime[1];
00211 
00212 /*@-boundsread@*/
00213     if (buildTime[0] == 0)
00214         buildTime[0] = (int_32) time(NULL);
00215 /*@=boundsread@*/
00216     return buildTime;
00217 }
00218 
00219 /*@-boundswrite@*/
00220 const char *const buildHost(void)
00221 {
00222     static char hostname[1024];
00223     static int oneshot = 0;
00224     struct hostent *hbn;
00225 
00226     if (! oneshot) {
00227         (void) gethostname(hostname, sizeof(hostname));
00228         /*@-unrecog -multithreaded @*/
00229         /*@-globs@*/    /* FIX: h_errno access */
00230         hbn = gethostbyname(hostname);
00231         /*@=globs@*/
00232         /*@=unrecog =multithreaded @*/
00233         if (hbn)
00234             strcpy(hostname, hbn->h_name);
00235         else
00236             rpmMessage(RPMMESS_WARNING,
00237                         _("Could not canonicalize hostname: %s\n"), hostname);
00238         oneshot = 1;
00239     }
00240     return(hostname);
00241 }
00242 /*@=boundswrite@*/
00243 /*@=mods@*/

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2