- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# build/names.c

Go to the documentation of this file.

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