# 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 void freeNames(void)
    00030 {
    00031     int x;
    00032     for (x = 0; x < uid_used; x++)
    00033         unames[x] = _free(unames[x]);
    00034     for (x = 0; x < gid_used; x++)
    00035         gnames[x] = _free(gnames[x]);
    00036 }
    00037
    00038 const char *getUname(uid_t uid)
    00039 {
    00040     struct passwd *pw;
    00041     int x;
    00042
    00043     for (x = 0; x < uid_used; x++) {
    00044         if (unames[x] == NULL) continue;
    00045         if (uids[x] == uid)
    00046             return unames[x];
    00047     }
    00048
    00049     /* XXX - This is the other hard coded limit */
    00050     if (x == 1024)
    00051         rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
    00052     uid_used++;
    00053
    00054     pw = getpwuid(uid);
    00055     uids[x] = uid;
    00056     unames[x] = (pw ? xstrdup(pw->pw_name) : NULL);
    00057     return unames[x];
    00058 }
    00059
    00060 const char *getUnameS(const char *uname)
    00061 {
    00062     struct passwd *pw;
    00063     int x;
    00064
    00065     for (x = 0; x < uid_used; x++) {
    00066         if (unames[x] == NULL) continue;
    00067         if (!strcmp(unames[x],uname))
    00068             return unames[x];
    00069     }
    00070
    00071     /* XXX - This is the other hard coded limit */
    00072     if (x == 1024)
    00073         rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
    00074     uid_used++;
    00075
    00076     pw = getpwnam(uname);
    00077     uids[x] = (pw ? pw->pw_uid : -1);
    00078     unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname));
    00079     return unames[x];
    00080 }
    00081
    00082 uid_t getUidS(const char *uname)
    00083 {
    00084     struct passwd *pw;
    00085     int x;
    00086
    00087     for (x = 0; x < uid_used; x++) {
    00088         if (unames[x] == NULL) continue;
    00089         if (!strcmp(unames[x],uname))
    00090             return uids[x];
    00091     }
    00092
    00093     /* XXX - This is the other hard coded limit */
    00094     if (x == 1024)
    00095         rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
    00096     uid_used++;
    00097
    00098     pw = getpwnam(uname);
    00099     uids[x] = (pw ? pw->pw_uid : -1);
    00100     unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname));
    00101     return uids[x];
    00102 }
    00103
    00104 const char *getGname(gid_t gid)
    00105 {
    00106     struct group *gr;
    00107     int x;
    00108
    00109     for (x = 0; x < gid_used; x++) {
    00110         if (gnames[x] == NULL) continue;
    00111         if (gids[x] == gid)
    00112             return gnames[x];
    00113     }
    00114
    00115     /* XXX - This is the other hard coded limit */
    00116     if (x == 1024)
    00117         rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
    00118     gid_used++;
    00119
    00120     gr = getgrgid(gid);
    00121     gids[x] = gid;
    00122     gnames[x] = (gr ? xstrdup(gr->gr_name) : NULL);
    00123     return gnames[x];
    00124 }
    00125
    00126 const char *getGnameS(const char *gname)
    00127 {
    00128     struct group *gr;
    00129     int x;
    00130
    00131     for (x = 0; x < gid_used; x++) {
    00132         if (gnames[x] == NULL) continue;
    00133         if (!strcmp(gnames[x], gname))
    00134             return gnames[x];
    00135     }
    00136
    00137     /* XXX - This is the other hard coded limit */
    00138     if (x == 1024)
    00139         rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
    00140     gid_used++;
    00141
    00142     gr = getgrnam(gname);
    00143     gids[x] = (gr ? gr->gr_gid : -1);
    00144     gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname));
    00145     return gnames[x];
    00146 }
    00147
    00148 gid_t getGidS(const char *gname)
    00149 {
    00150     struct group *gr;
    00151     int x;
    00152
    00153     for (x = 0; x < gid_used; x++) {
    00154         if (gnames[x] == NULL) continue;
    00155         if (!strcmp(gnames[x], gname))
    00156             return gids[x];
    00157     }
    00158
    00159     /* XXX - This is the other hard coded limit */
    00160     if (x == 1024)
    00161         rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
    00162     gid_used++;
    00163
    00164     gr = getgrnam(gname);
    00165     gids[x] = (gr ? gr->gr_gid : -1);
    00166     gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname));
    00167     return gids[x];
    00168 }
    00169
    00170 int_32 *const getBuildTime(void)
    00171 {
    00172     static int_32 buildTime[1];
    00173
    00174     if (buildTime[0] == 0)
    00175         buildTime[0] = (int_32) time(NULL);
    00176     return buildTime;
    00177 }
    00178
    00179 const char *const buildHost(void)
    00180 {
    00181     static char hostname[1024];
    00182     static int gotit = 0;
    00183     struct hostent *hbn;
    00184
    00185     if (! gotit) {
    00186         (void) gethostname(hostname, sizeof(hostname));
    00187         /*@-unrecog -multithreaded @*/
    00188         /*@-globs@*/    /* FIX: h_errno access */
    00189         hbn = gethostbyname(hostname);
    00190         /*@=globs@*/
    00191         /*@=unrecog =multithreaded @*/
    00192         if (hbn)
    00193             strcpy(hostname, hbn->h_name);
    00194         else
    00195             rpmMessage(RPMMESS_WARNING,
    00196                         _("Could not canonicalize hostname: %s\n"), hostname);
    00197         gotit = 1;
    00198     }
    00199     return(hostname);
    00200 }
    00201 /*@=mods@*/