home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / A / PS / PROCPS-0.000 / PROCPS-0 / procps-0.97 / pwcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-25  |  683 b   |  35 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pwd.h>
  4.  
  5. #define    HASHSIZE    16            /* power of 2 */
  6. #define    HASH(x)        ((x) & (HASHSIZE - 1))
  7.  
  8.  
  9. static struct pwbuf {
  10.     int uid;
  11.     char name[12];
  12.     struct pwbuf *next;
  13. } *pwhash[HASHSIZE];
  14.  
  15. char *user_from_uid(int uid)
  16. {
  17.     struct pwbuf **p;
  18.     struct passwd *pw;
  19.  
  20.     p = &pwhash[HASH(uid)];
  21.     while (*p) {
  22.     if ((*p)->uid == uid)
  23.         return((*p)->name);
  24.     p = &(*p)->next;
  25.     }
  26.     *p = (struct pwbuf *) malloc(sizeof(struct pwbuf));
  27.     (*p)->uid = uid;
  28.     if ((pw = getpwuid(uid)) == NULL)
  29.     sprintf((*p)->name, "#%d", uid);
  30.     else
  31.     sprintf((*p)->name, "%-.8s", pw->pw_name);
  32.     (*p)->next = NULL;
  33.     return((*p)->name);
  34. }
  35.