home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / Mail / getname.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  3KB  |  135 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #
  3.  
  4.  
  5. /*
  6.  * Getname / getuserid for those with no
  7.  * hashed passwd data base).
  8.  * Do not compile this module in if you DO have hashed
  9.  * passwd's -- this is slower.
  10.  *
  11.  * Also provided here is a getpw routine which can share
  12.  * the open file.  This is used for the Version 6 getenv
  13.  * implementation.
  14.  */
  15.  
  16. #include "rcv.h"
  17.  
  18. static FILE *pwfile =    NULL;        /* Pw file held open */
  19. static char *pwname =     "/etc/passwd";    /* Name of passwd file */
  20.  
  21. /*
  22.  * Search the passwd file for a uid.  Return name through ref parameter
  23.  * if found, indicating success with 0 return.  Return -1 on error.
  24.  * If -1 is passed as the user id, close the passwd file.
  25.  */
  26.  
  27. getname(uid, namebuf)
  28.     char namebuf[];
  29. {
  30.     register char *cp, *cp2;
  31.     char linebuf[BUFSIZ];
  32.  
  33.     if (uid == -1) {
  34.         if (pwfile != NULL)
  35.             fclose(pwfile);
  36.         pwfile = NULL;
  37.         return(0);
  38.     }
  39.     if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
  40.         return(-1);
  41.     rewind(pwfile);
  42.     while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
  43.         if (pweval(linebuf) == uid) {
  44.             for (cp = linebuf, cp2 = namebuf; *cp != ':';
  45.                 *cp2++ = *cp++)
  46.                 ;
  47.             *cp2 = '\0';
  48.             return(0);
  49.         }
  50.     return(-1);
  51. }
  52.  
  53. /*
  54.  * Read the users password file line into the passed line
  55.  * buffer.
  56.  */
  57.  
  58. getpw(uid, linebuf)
  59.     char linebuf[];
  60. {
  61.     register char *cp, *cp2;
  62.  
  63.     if (uid == -1) {
  64.         if (pwfile != NULL)
  65.             fclose(pwfile);
  66.         pwfile = NULL;
  67.         return(0);
  68.     }
  69.     if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
  70.         return(-1);
  71.     rewind(pwfile);
  72.     while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
  73.         if (pweval(linebuf) == uid) {
  74.             if (linebuf[0] != '\0')
  75.                 linebuf[strlen(linebuf)-1] = '\0';
  76.             return(0);
  77.         }
  78.     return(-1);
  79. }
  80.  
  81. /*
  82.  * Convert the passed name to a user id and return it.  Return -1
  83.  * on error.  Iff the name passed is -1 (yech) close the pwfile.
  84.  */
  85.  
  86. getuserid(name)
  87.     char name[];
  88. {
  89.     register char *cp, *cp2;
  90.     char linebuf[BUFSIZ];
  91.  
  92.     if (name == (char *) -1) {
  93.         if (pwfile != NULL)
  94.             fclose(pwfile);
  95.         pwfile = NULL;
  96.         return(0);
  97.     }
  98.     if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
  99.         return(-1);
  100.     rewind(pwfile);
  101.     while (fgets(linebuf, BUFSIZ, pwfile) != NULL) {
  102.         for (cp = name, cp2 = linebuf; *cp++ == *cp2++;)
  103.             ;
  104.         if (*--cp == '\0' && *--cp2 == ':')
  105.             return(pweval(linebuf));
  106.     }
  107.     return(-1);
  108. }
  109.  
  110. /*
  111.  * Evaluate the user id of the passed passwd line and return it.
  112.  */
  113.  
  114. static
  115. pweval(line)
  116.     char line[];
  117. {
  118.     register char *cp;
  119.     register int i;
  120.     register int uid;
  121.  
  122.     for (cp = line, i = 0; i < 2; i += (*cp++ == ':'))
  123.         ;
  124.     uid = atoi(cp);
  125.  
  126. #ifdef UIDGID
  127.     while (*cp && *cp != ':')
  128.         cp++;
  129.     cp++;
  130.     uid |= atoi(cp) << 8;
  131. #endif
  132.  
  133.     return(uid);
  134. }
  135.