| 43 |
This code should also be modified for non-UNIX systems to do something |
This code should also be modified for non-UNIX systems to do something |
| 44 |
reasonable. */ |
reasonable. */ |
| 45 |
|
|
| 46 |
static char cached_uname[UNAME_FIELD_SIZE]; |
static char *cached_uname; |
| 47 |
static char cached_gname[GNAME_FIELD_SIZE]; |
static char *cached_gname; |
| 48 |
|
|
| 49 |
static uid_t cached_uid; /* valid only if cached_uname is not empty */ |
static uid_t cached_uid; /* valid only if cached_uname is not empty */ |
| 50 |
static gid_t cached_gid; /* valid only if cached_gname is not empty */ |
static gid_t cached_gid; /* valid only if cached_gname is not empty */ |
| 51 |
|
|
| 52 |
/* These variables are valid only if nonempty. */ |
/* These variables are valid only if nonempty. */ |
| 53 |
static char cached_no_such_uname[UNAME_FIELD_SIZE]; |
static char *cached_no_such_uname; |
| 54 |
static char cached_no_such_gname[GNAME_FIELD_SIZE]; |
static char *cached_no_such_gname; |
| 55 |
|
|
| 56 |
/* These variables are valid only if nonzero. It's not worth optimizing |
/* These variables are valid only if nonzero. It's not worth optimizing |
| 57 |
the case for weird systems where 0 is not a valid uid or gid. */ |
the case for weird systems where 0 is not a valid uid or gid. */ |
| 60 |
|
|
| 61 |
/* Given UID, find the corresponding UNAME. */ |
/* Given UID, find the corresponding UNAME. */ |
| 62 |
void |
void |
| 63 |
uid_to_uname (uid_t uid, char uname[UNAME_FIELD_SIZE]) |
uid_to_uname (uid_t uid, char **uname) |
| 64 |
{ |
{ |
| 65 |
struct passwd *passwd; |
struct passwd *passwd; |
| 66 |
|
|
| 67 |
if (uid != 0 && uid == cached_no_such_uid) |
if (uid != 0 && uid == cached_no_such_uid) |
| 68 |
{ |
{ |
| 69 |
*uname = '\0'; |
*uname = strdup (""); |
| 70 |
return; |
return; |
| 71 |
} |
} |
| 72 |
|
|
| 73 |
if (!cached_uname[0] || uid != cached_uid) |
if (!cached_uname || uid != cached_uid) |
| 74 |
{ |
{ |
| 75 |
passwd = getpwuid (uid); |
passwd = getpwuid (uid); |
| 76 |
if (passwd) |
if (passwd) |
| 77 |
{ |
{ |
| 78 |
cached_uid = uid; |
cached_uid = uid; |
| 79 |
strncpy (cached_uname, passwd->pw_name, UNAME_FIELD_SIZE); |
assign_string (&cached_uname, passwd->pw_name); |
| 80 |
} |
} |
| 81 |
else |
else |
| 82 |
{ |
{ |
| 83 |
cached_no_such_uid = uid; |
cached_no_such_uid = uid; |
| 84 |
*uname = '\0'; |
*uname = strdup (""); |
| 85 |
return; |
return; |
| 86 |
} |
} |
| 87 |
} |
} |
| 88 |
strncpy (uname, cached_uname, UNAME_FIELD_SIZE); |
*uname = strdup (cached_uname); |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
/* Given GID, find the corresponding GNAME. */ |
/* Given GID, find the corresponding GNAME. */ |
| 92 |
void |
void |
| 93 |
gid_to_gname (gid_t gid, char gname[GNAME_FIELD_SIZE]) |
gid_to_gname (gid_t gid, char **gname) |
| 94 |
{ |
{ |
| 95 |
struct group *group; |
struct group *group; |
| 96 |
|
|
| 97 |
if (gid != 0 && gid == cached_no_such_gid) |
if (gid != 0 && gid == cached_no_such_gid) |
| 98 |
{ |
{ |
| 99 |
*gname = '\0'; |
*gname = strdup (""); |
| 100 |
return; |
return; |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
if (!cached_gname[0] || gid != cached_gid) |
if (!cached_gname || gid != cached_gid) |
| 104 |
{ |
{ |
| 105 |
group = getgrgid (gid); |
group = getgrgid (gid); |
| 106 |
if (group) |
if (group) |
| 107 |
{ |
{ |
| 108 |
cached_gid = gid; |
cached_gid = gid; |
| 109 |
strncpy (cached_gname, group->gr_name, GNAME_FIELD_SIZE); |
assign_string (&cached_gname, group->gr_name); |
| 110 |
} |
} |
| 111 |
else |
else |
| 112 |
{ |
{ |
| 113 |
cached_no_such_gid = gid; |
cached_no_such_gid = gid; |
| 114 |
*gname = '\0'; |
*gname = strdup (""); |
| 115 |
return; |
return; |
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
strncpy (gname, cached_gname, GNAME_FIELD_SIZE); |
*gname = strdup (cached_gname); |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
/* Given UNAME, set the corresponding UID and return 1, or else, return 0. */ |
/* Given UNAME, set the corresponding UID and return 1, or else, return 0. */ |
| 122 |
int |
int |
| 123 |
uname_to_uid (char uname[UNAME_FIELD_SIZE], uid_t *uidp) |
uname_to_uid (char *uname, uid_t *uidp) |
| 124 |
{ |
{ |
| 125 |
struct passwd *passwd; |
struct passwd *passwd; |
| 126 |
|
|
| 127 |
if (cached_no_such_uname[0] |
if (cached_no_such_uname |
| 128 |
&& strncmp (uname, cached_no_such_uname, UNAME_FIELD_SIZE) == 0) |
&& strcmp (uname, cached_no_such_uname) == 0) |
| 129 |
return 0; |
return 0; |
| 130 |
|
|
| 131 |
if (!cached_uname[0] |
if (!cached_uname |
| 132 |
|| uname[0] != cached_uname[0] |
|| uname[0] != cached_uname[0] |
| 133 |
|| strncmp (uname, cached_uname, UNAME_FIELD_SIZE) != 0) |
|| strcmp (uname, cached_uname) != 0) |
| 134 |
{ |
{ |
| 135 |
passwd = getpwnam (uname); |
passwd = getpwnam (uname); |
| 136 |
if (passwd) |
if (passwd) |
| 137 |
{ |
{ |
| 138 |
cached_uid = passwd->pw_uid; |
cached_uid = passwd->pw_uid; |
| 139 |
strncpy (cached_uname, uname, UNAME_FIELD_SIZE); |
assign_string (&cached_uname, passwd->pw_name); |
| 140 |
} |
} |
| 141 |
else |
else |
| 142 |
{ |
{ |
| 143 |
strncpy (cached_no_such_uname, uname, UNAME_FIELD_SIZE); |
assign_string (&cached_no_such_uname, uname); |
| 144 |
return 0; |
return 0; |
| 145 |
} |
} |
| 146 |
} |
} |
| 150 |
|
|
| 151 |
/* Given GNAME, set the corresponding GID and return 1, or else, return 0. */ |
/* Given GNAME, set the corresponding GID and return 1, or else, return 0. */ |
| 152 |
int |
int |
| 153 |
gname_to_gid (char gname[GNAME_FIELD_SIZE], gid_t *gidp) |
gname_to_gid (char *gname, gid_t *gidp) |
| 154 |
{ |
{ |
| 155 |
struct group *group; |
struct group *group; |
| 156 |
|
|
| 157 |
if (cached_no_such_gname[0] |
if (cached_no_such_gname |
| 158 |
&& strncmp (gname, cached_no_such_gname, GNAME_FIELD_SIZE) == 0) |
&& strcmp (gname, cached_no_such_gname) == 0) |
| 159 |
return 0; |
return 0; |
| 160 |
|
|
| 161 |
if (!cached_gname[0] |
if (!cached_gname |
| 162 |
|| gname[0] != cached_gname[0] |
|| gname[0] != cached_gname[0] |
| 163 |
|| strncmp (gname, cached_gname, GNAME_FIELD_SIZE) != 0) |
|| strcmp (gname, cached_gname) != 0) |
| 164 |
{ |
{ |
| 165 |
group = getgrnam (gname); |
group = getgrnam (gname); |
| 166 |
if (group) |
if (group) |
| 167 |
{ |
{ |
| 168 |
cached_gid = group->gr_gid; |
cached_gid = group->gr_gid; |
| 169 |
strncpy (cached_gname, gname, GNAME_FIELD_SIZE); |
assign_string (&cached_gname, gname); |
| 170 |
} |
} |
| 171 |
else |
else |
| 172 |
{ |
{ |
| 173 |
strncpy (cached_no_such_gname, gname, GNAME_FIELD_SIZE); |
assign_string (&cached_no_such_gname, gname); |
| 174 |
return 0; |
return 0; |
| 175 |
} |
} |
| 176 |
} |
} |
| 177 |
*gidp = cached_gid; |
*gidp = cached_gid; |
| 178 |
return 1; |
return 1; |
| 179 |
} |
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
/* Names from the command call. */ |
/* Names from the command call. */ |
| 183 |
|
|