160 Chapter 8
if (name == NULL || *name == '\0') /* On NULL or empty string */
return -1; /* return an error */u = strtol(name, &endptr, 10); /* As a convenience to caller */
if (*endptr == '\0') /* allow a numeric string */
return u;pwd = getpwnam(name);
if (pwd == NULL)
return -1;return pwd->pw_uid;
}char * /* Return name corresponding to 'gid', or NULL on error */
groupNameFromId(gid_t gid)
{
struct group *grp;grp = getgrgid(gid);
return (grp == NULL)? NULL : grp->gr_name;
}gid_t /* Return GID corresponding to 'name', or -1 on error */
groupIdFromName(const char *name)
{
struct group *grp;
gid_t g;
char *endptr;if (name == NULL || *name == '\0') /* On NULL or empty string */
return -1; /* return an error */g = strtol(name, &endptr, 10); /* As a convenience to caller */
if (*endptr == '\0') /* allow a numeric string */
return g;grp = getgrnam(name);
if (grp == NULL)
return -1;return grp->gr_gid;
}
–––––––––––––––––––––––––––––––––––––––––––––– users_groups/ugid_functions.cScanning all records in the password and group files
The setpwent(), getpwent(), and endpwent() functions are used to perform sequential
scans of the records in the password file.