home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / 2bsd.tar.gz / 2bsd.tar / upgrade / libretro / getpwent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-04-21  |  1.1 KB  |  75 lines

  1. #include <pwd.h>
  2.  
  3. #define    BUFSIZ    160
  4.  
  5. static int pwf = -1;
  6. static char line[BUFSIZ+1];
  7. static struct passwd passwd;
  8.  
  9. setpwent()
  10. {
  11.     if( pwf == -1 )
  12.         pwf = open( "/etc/passwd", 0 );
  13.     else
  14.         lseek(pwf, 0l, 0);
  15. }
  16.  
  17. endpwent()
  18. {
  19.     if( pwf != -1 ){
  20.         close( pwf );
  21.         pwf = -1;
  22.     }
  23. }
  24.  
  25. static char *
  26. pwskip(p)
  27. register char *p;
  28. {
  29.     while( *p && *p != ':' )
  30.         ++p;
  31.     if( *p ) *p++ = 0;
  32.     return(p);
  33. }
  34.  
  35. struct passwd *
  36. getpwent()
  37. {
  38.     register char *p;
  39.     register int i, j;
  40.  
  41.     if (pwf == -1) {
  42.         if( (pwf = open( "/etc/passwd", 0 )) == -1 )
  43.             return(0);
  44.     }
  45.     i = read(pwf, line, BUFSIZ);
  46.     for (j = 0; j < i; j++)
  47.         if (line[j] == '\n')
  48.             break;
  49.     if (j >= i)
  50.         return(0);
  51.     line[++j] = 0;
  52.     lseek(pwf, (long) (j - i), 1);
  53.     p = line;
  54.     passwd.pw_name = p;
  55.     p = pwskip(p);
  56.     passwd.pw_passwd = p;
  57.     p = pwskip(p);
  58.     passwd.pw_uid = atoi(p);
  59.     p = pwskip(p);
  60. /* For Cory, e.g. only
  61.     passwd.pw_uid =+ atoi(p) << 8;
  62.  */
  63.     passwd.pw_quota = 0;
  64.     passwd.pw_comment = "";
  65.     p = pwskip(p);
  66.     passwd.pw_gecos = p;
  67.     p = pwskip(p);
  68.     passwd.pw_dir = p;
  69.     p = pwskip(p);
  70.     passwd.pw_shell = p;
  71.      while(*p && *p != '\n') p++;
  72.     *p = '\0';
  73.     return(&passwd);
  74. }
  75.