|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sfluid.h" char * get_gid_name (gid_t gid)
Get group name from group file. We optimise by keeping a table of gids and names in memory. Note that this will cause problems if the program stays running when the group file has been changed. Returns a string containing the translated user name, or "<none>" if the gid could not be translated. Under MS- DOS the gid must be zero. The returned string is in a static area that is _not_ overwritten with each call, but which should be treated as read-only, and unstable: i.e. the value returned by one call to get_gid_name may change as a result of a later call. If you need persistent strings, use strdupl() after each call.
{
# if (defined (DOES_UID))
static struct gids { /* Table of cache'd gids */
gid_t id;
char *name;
} cache [GID_CACHE_MAX];
static int
cache_size = 0, /* Number of gid's in cache */
cache_oldest = 0; /* Oldest entry in cache */
int
cache_scan; /* Scan through cache */
struct group
*group_entry;
/* First, look for gid in cache */
for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
if (cache [cache_scan].id == gid)
return (cache [cache_scan].name);
/* Add new name to cache: if cache was full, kick-out oldest entry */
if (cache_size == GID_CACHE_MAX)
{
cache_scan = cache_oldest++;
cache_oldest %= GID_CACHE_MAX;
free (cache [cache_scan].name);
}
else
cache_scan = cache_size++;
cache [cache_scan].id = gid;
# if (defined (__VMS__))
cache [cache_scan].name = "<none>";
# else
if ((group_entry = getgrgid (gid)) == NULL)
cache [cache_scan].name = "<none>";
else
cache [cache_scan].name = strdupl (group_entry-> gr_name);
# endif
return (cache [cache_scan].name);
# elif (defined (__MSDOS__))
return (gid == 0? "group": "<none>");
# endif
}
| | << | < | > | >> |
|