5 /* unameToUid(), uidTouname() and the group variants are really poorly
6 implemented. They really ought to use hash tables. I just made the
7 guess that most files would be owned by root or the same person/group
8 who owned the last file. Those two values are cached, everything else
9 is looked up via getpw() and getgr() functions. If this performs
10 too poorly I'll have to implement it properly :-( */
12 int unameToUid(const char * thisUname, uid_t * uid)
14 /*@only@*/ static char * lastUname = NULL;
15 static int lastUnameLen = 0;
16 static int lastUnameAlloced;
18 struct passwd * pwent;
24 } else if (!strcmp(thisUname, "root")) {
29 thisUnameLen = strlen(thisUname);
30 if (!lastUname || thisUnameLen != lastUnameLen ||
31 strcmp(thisUname, lastUname)) {
32 if (lastUnameAlloced < thisUnameLen + 1) {
33 lastUnameAlloced = thisUnameLen + 10;
34 lastUname = xrealloc(lastUname, lastUnameAlloced); /* XXX memory leak */
36 strcpy(lastUname, thisUname);
38 pwent = getpwnam(thisUname);
41 pwent = getpwnam(thisUname);
42 if (!pwent) return -1;
45 lastUid = pwent->pw_uid;
53 int gnameToGid(const char * thisGname, gid_t * gid)
55 /*@only@*/ static char * lastGname = NULL;
56 static int lastGnameLen = 0;
57 static int lastGnameAlloced;
65 } else if (!strcmp(thisGname, "root")) {
70 thisGnameLen = strlen(thisGname);
71 if (!lastGname || thisGnameLen != lastGnameLen ||
72 strcmp(thisGname, lastGname)) {
73 if (lastGnameAlloced < thisGnameLen + 1) {
74 lastGnameAlloced = thisGnameLen + 10;
75 lastGname = xrealloc(lastGname, lastGnameAlloced); /* XXX memory leak */
77 strcpy(lastGname, thisGname);
79 grent = getgrnam(thisGname);
82 grent = getgrnam(thisGname);
83 if (!grent) return -1;
85 lastGid = grent->gr_gid;
93 char * uidToUname(uid_t uid)
95 static int lastUid = -1;
96 /*@only@*/ static char * lastUname = NULL;
97 static int lastUnameLen = 0;
98 struct passwd * pwent;
101 if (uid == (uid_t) -1) {
106 } else if (uid == lastUid) {
109 pwent = getpwuid(uid);
110 if (!pwent) return NULL;
113 len = strlen(pwent->pw_name);
114 if (lastUnameLen < len + 1) {
115 lastUnameLen = len + 20;
116 lastUname = xrealloc(lastUname, lastUnameLen);
118 strcpy(lastUname, pwent->pw_name);
124 char * gidToGname(gid_t gid)
126 static int lastGid = -1;
127 /*@only@*/ static char * lastGname = NULL;
128 static int lastGnameLen = 0;
129 struct group * grent;
132 if (gid == (gid_t) -1) {
137 } else if (gid == lastGid) {
140 grent = getgrgid(gid);
141 if (!grent) return NULL;
144 len = strlen(grent->gr_name);
145 if (lastGnameLen < len + 1) {
146 lastGnameLen = len + 20;
147 lastGname = xrealloc(lastGname, lastGnameLen);
149 strcpy(lastGname, grent->gr_name);