procps/proc/pwcache.c

78 lines
2.0 KiB
C
Raw Normal View History

2002-02-02 04:17:29 +05:30
/***********************************************************************\
* Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com *
* *
* This file is placed under the conditions of the GNU Library *
* General Public License, version 2, or any later version. *
* See file ../COPYING for information on distribution conditions. *
\***********************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pwd.h>
2002-12-09 12:30:07 +05:30
#include "alloc.h"
#include "pwcache.h"
2002-02-02 04:17:29 +05:30
#include <grp.h>
2002-10-14 02:49:26 +05:30
// might as well fill cache lines... else we waste memory anyway
2002-12-09 12:30:07 +05:30
#define HASHSIZE 64 /* power of 2 */
2002-02-02 04:17:29 +05:30
#define HASH(x) ((x) & (HASHSIZE - 1))
2002-10-14 02:49:26 +05:30
#define NAMESIZE 20
#define NAMELENGTH "19"
2002-02-02 04:17:29 +05:30
static struct pwbuf {
2002-10-14 02:49:26 +05:30
struct pwbuf *next;
2002-02-02 04:17:29 +05:30
uid_t uid;
char name[NAMESIZE];
} *pwhash[HASHSIZE];
char *user_from_uid(uid_t uid)
{
struct pwbuf **p;
struct passwd *pw;
p = &pwhash[HASH(uid)];
while (*p) {
if ((*p)->uid == uid)
return((*p)->name);
p = &(*p)->next;
}
*p = (struct pwbuf *) xmalloc(sizeof(struct pwbuf));
(*p)->uid = uid;
if ((pw = getpwuid(uid)) == NULL)
sprintf((*p)->name, "#%d", uid);
else
sprintf((*p)->name, "%-." NAMELENGTH "s", pw->pw_name);
(*p)->next = NULL;
return((*p)->name);
}
static struct grpbuf {
2002-10-14 02:49:26 +05:30
struct grpbuf *next;
2002-02-02 04:17:29 +05:30
gid_t gid;
char name[NAMESIZE];
} *grphash[HASHSIZE];
char *group_from_gid(gid_t gid)
{
struct grpbuf **g;
struct group *gr;
g = &grphash[HASH(gid)];
while (*g) {
if ((*g)->gid == gid)
return((*g)->name);
g = &(*g)->next;
}
*g = (struct grpbuf *) malloc(sizeof(struct grpbuf));
(*g)->gid = gid;
if ((gr = getgrgid(gid)) == NULL)
sprintf((*g)->name, "#%d", gid);
else
sprintf((*g)->name, "%-." NAMELENGTH "s", gr->gr_name);
(*g)->next = NULL;
return((*g)->name);
}