home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / shadow-2.pt3 / pwent.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  2KB  |  112 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3. #include <string.h>
  4.  
  5. #define    SBUFSIZ    64
  6.  
  7. static    char    *tokcpy (buf, token)
  8. char    *buf;
  9. char    *token;
  10. {
  11.     static    char    *cp;
  12.     char    *start;
  13.  
  14.     if (buf == (char *) 0)
  15.         buf = cp;
  16.     else
  17.         cp = buf;
  18.  
  19.     start = cp;
  20.  
  21.     if (*buf == '\0')
  22.         return ((char *) 0);
  23.  
  24.     while (*buf && *buf != ':')
  25.         *token++ = *buf++;
  26.  
  27.     *token = '\0';
  28.  
  29.     if (*buf)
  30.         cp = buf + 1;
  31.  
  32.     return (start);
  33. }
  34.  
  35. struct    passwd    *sgetpwent (buf)
  36. char    *buf;
  37. {
  38.     static    struct    passwd    pwent;
  39.     static    char    name[SBUFSIZ];
  40.     static    char    password[SBUFSIZ];
  41.     static    char    gecos[SBUFSIZ];
  42.     static    char    home[SBUFSIZ];
  43.     static    char    shell[SBUFSIZ];
  44.     static    char    age[SBUFSIZ];
  45.     char    tmp[BUFSIZ];
  46.     char    *cp;
  47.  
  48.     pwent.pw_name = name;
  49.     pwent.pw_passwd = password;
  50.     pwent.pw_uid = -1;
  51.     pwent.pw_gid = -1;
  52.     pwent.pw_age = age;
  53.     pwent.pw_comment = (char *) 0;
  54.     pwent.pw_gecos = gecos;
  55.     pwent.pw_dir = home;
  56.     pwent.pw_shell = shell;
  57.  
  58.     (void) strcpy (tmp, buf);
  59.  
  60.     if (! tokcpy (tmp, name) || ! name[0])
  61.         return ((struct passwd *) 0);
  62.  
  63.     if (! tokcpy ((char *) 0, password))
  64.         return ((struct passwd *) 0);
  65.  
  66.     if (tokcpy ((char *) 0, tmp) && *tmp)
  67.         pwent.pw_uid = atoi (tmp);
  68.     else
  69.         return ((struct passwd *) 0);
  70.  
  71.     if (tokcpy ((char *) 0, tmp) && *tmp)
  72.         pwent.pw_gid = atoi (tmp);
  73.     else
  74.         return ((struct passwd *) 0);
  75.  
  76.     if (cp = strchr (password, ',')) {
  77.         (void) strcpy (age, cp + 1);
  78.         *cp = '\0';
  79.     } else
  80.         pwent.pw_age = (char *) 0;
  81.  
  82.     if (! tokcpy ((char *) 0, gecos))
  83.         return ((struct passwd *) 0);
  84.  
  85.     if (! tokcpy ((char *) 0, home))
  86.         return ((struct passwd *) 0);
  87.  
  88.     if (! tokcpy ((char *) 0, shell) && *shell)
  89.         pwent.pw_shell = (char *) 0;
  90.  
  91.     if (pwent.pw_passwd && pwent.pw_passwd[0] == '\0')
  92.         pwent.pw_passwd = (char *) 0;
  93.  
  94.     return (&pwent);
  95. }
  96. #ifdef FGETPWENT
  97. struct    passwd    *fgetpwent (fp)
  98. FILE    *fp;
  99. {
  100.     char    buf[BUFSIZ];
  101.  
  102.     while (fgets (buf, BUFSIZ, fp) != (char *) 0) {
  103.         if (buf[0] == '#')
  104.             continue;
  105.  
  106.         buf[strlen (buf) - 1] = '\0';
  107.         return (sgetpwent (buf));
  108.     }
  109.     return ((struct passwd *) 0);
  110. }
  111. #endif
  112.