9
/* unameToUid(), uidTouname() and the group variants are really poorly
10
implemented. They really ought to use hash tables. I just made the
11
guess that most files would be owned by root or the same person/group
12
who owned the last file. Those two values are cached, everything else
13
is looked up via getpw() and getgr() functions. If this performs
14
too poorly I'll have to implement it properly :-( */
16
int unameToUid(const char * thisUname, uid_t * uid)
18
/*@only@*/ static char * lastUname = NULL;
19
static size_t lastUnameLen = 0;
20
static size_t lastUnameAlloced;
22
struct passwd * pwent;
28
} else if (strcmp(thisUname, "root") == 0) {
33
thisUnameLen = strlen(thisUname);
34
if (lastUname == NULL || thisUnameLen != lastUnameLen ||
35
strcmp(thisUname, lastUname) != 0) {
36
if (lastUnameAlloced < thisUnameLen + 1) {
37
lastUnameAlloced = thisUnameLen + 10;
38
lastUname = xrealloc(lastUname, lastUnameAlloced); /* XXX memory leak */
40
strcpy(lastUname, thisUname);
42
pwent = getpwnam(thisUname);
45
pwent = getpwnam(thisUname);
46
if (pwent == NULL) return -1;
49
lastUid = pwent->pw_uid;
57
int gnameToGid(const char * thisGname, gid_t * gid)
59
/*@only@*/ static char * lastGname = NULL;
60
static size_t lastGnameLen = 0;
61
static size_t lastGnameAlloced;
66
if (thisGname == NULL) {
69
} else if (strcmp(thisGname, "root") == 0) {
74
thisGnameLen = strlen(thisGname);
75
if (lastGname == NULL || thisGnameLen != lastGnameLen ||
76
strcmp(thisGname, lastGname) != 0)
78
if (lastGnameAlloced < thisGnameLen + 1) {
79
lastGnameAlloced = thisGnameLen + 10;
80
lastGname = xrealloc(lastGname, lastGnameAlloced); /* XXX memory leak */
82
strcpy(lastGname, thisGname);
84
grent = getgrnam(thisGname);
87
grent = getgrnam(thisGname);
88
if (grent == NULL) return -1;
90
lastGid = grent->gr_gid;
98
char * uidToUname(uid_t uid)
100
static uid_t lastUid = (uid_t) -1;
101
/*@only@*/ static char * lastUname = NULL;
102
static size_t lastUnameLen = 0;
104
if (uid == (uid_t) -1) {
105
lastUid = (uid_t) -1;
107
} else if (uid == (uid_t) 0) {
109
} else if (uid == lastUid) {
112
struct passwd * pwent = getpwuid(uid);
115
if (pwent == NULL) return NULL;
118
len = strlen(pwent->pw_name);
119
if (lastUnameLen < len + 1) {
120
lastUnameLen = len + 20;
121
lastUname = xrealloc(lastUname, lastUnameLen);
123
strcpy(lastUname, pwent->pw_name);
129
char * gidToGname(gid_t gid)
131
static gid_t lastGid = (gid_t) -1;
132
/*@only@*/ static char * lastGname = NULL;
133
static size_t lastGnameLen = 0;
135
if (gid == (gid_t) -1) {
136
lastGid = (gid_t) -1;
138
} else if (gid == (gid_t) 0) {
140
} else if (gid == lastGid) {
143
struct group * grent = getgrgid(gid);
146
if (grent == NULL) return NULL;
149
len = strlen(grent->gr_name);
150
if (lastGnameLen < len + 1) {
151
lastGnameLen = len + 20;
152
lastGname = xrealloc(lastGname, lastGnameLen);
154
strcpy(lastGname, grent->gr_name);