|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sfluid.h" char * get_uid_name (uid_t uid)
Get user name from passwd file. We optimise by keeping a table of uids and names in memory. Note that this will cause problems if the program stays running when the passwd file has been changed. Returns a string containing the translated user name, or "<none>" if the uid could not be translated. Under MS- DOS the uid 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_uid_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 uids { /* Table of cached uids */
uid_t id;
char *name;
} cache [UID_CACHE_MAX];
static int
cache_size = 0, /* Number of uid's in cache */
cache_oldest = 0; /* Oldest entry in cache */
int
cache_scan; /* Scan through cache */
struct passwd
*passwd_entry;
/* First, look for uid in cache */
for (cache_scan = 0; cache_scan < cache_size; cache_scan++)
if (cache [cache_scan].id == uid)
return (cache [cache_scan].name);
/* Add new name to cache: if cache was full, kick-out oldest entry */
if (cache_size == UID_CACHE_MAX)
{
cache_scan = cache_oldest++;
cache_oldest %= UID_CACHE_MAX;
free (cache [cache_scan].name);
}
else
cache_scan = cache_size++;
cache [cache_scan].id = uid;
if ((passwd_entry = getpwuid (uid)) == NULL)
cache [cache_scan].name = "<none>";
else
cache [cache_scan].name = strdupl (passwd_entry-> pw_name);
return (cache [cache_scan].name);
# elif (defined (__MSDOS__))
return (uid == 0? "user": "<none>");
# endif
}
| | << | < | > | >> |
|