home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / getpw / getpw.c
Encoding:
C/C++ Source or Header  |  1987-02-26  |  2.6 KB  |  165 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3.  
  4. static char *pwdfile = "/etc/passwd";    /* default passwd file */
  5. static FILE *fp = NULL;
  6. static struct passwd curentry;        /* static data to return */
  7.  
  8. void setpwfile (cp)
  9. char *cp;
  10. {
  11.     pwdfile = cp;
  12. }
  13.  
  14. int setpwent ()
  15. {
  16.     if (fp)
  17.         rewind (fp);
  18.     else if ((fp = fopen (pwdfile, "r")) == NULL)
  19.     {
  20. #ifdef VERBOSE
  21.         fprintf (stderr,
  22.             "setpwent: %s non-existant or unreadable.\n", pwdfile);
  23. #endif
  24.         return (0);
  25.     }
  26.     return (1);
  27. }
  28.  
  29. int endpwent ()
  30. {
  31.     if (fp)
  32.     {
  33.         fclose (fp);
  34.         fp = NULL;
  35.     }
  36.     return 1;
  37. }
  38.  
  39. struct passwd *getpwent ()
  40. {
  41.     if (! fp && ! setpwent ())
  42.         return (NULL);
  43.  
  44.     if (! nextent ())
  45.         return (NULL);
  46.     else
  47.         return (& curentry);
  48. }
  49.  
  50. struct passwd *getpwuid (uid)
  51. register int uid;
  52. {
  53.     if (! setpwent ())
  54.         return (NULL);
  55.  
  56.     while (nextent ())
  57.         if (curentry.pw_uid == uid)
  58.             return (& curentry);
  59.  
  60.     return (NULL);
  61. }
  62.  
  63. struct passwd *getpwnam (name)
  64. register char *name;
  65. {
  66.     if (! setpwent ())
  67.         return (NULL);
  68.  
  69.     while (nextent ())
  70.         if (strcmp (curentry.pw_name, name) == 0)
  71.             return (& curentry);
  72.  
  73.     return (NULL);
  74. }
  75.  
  76. static char savbuf[BUFSIZ];
  77.         
  78. static int nextent ()
  79. {
  80.     register char *cp;
  81.  
  82.     if (! fp && ! setpwent ())
  83.         return (0);
  84.  
  85.     while (fgets (savbuf, sizeof(savbuf), fp) != NULL)
  86.     {
  87.         for (cp = savbuf; *cp && *cp != ':'; cp++)
  88.             ;
  89.         curentry.pw_name = savbuf;
  90.         *cp++ = '\0';
  91.         curentry.pw_passwd = cp;
  92.         for (; *cp && *cp != ':'; cp++)
  93.             ;
  94.         *cp++ = '\0';
  95.         curentry.pw_uid = atoi (cp);
  96.         for (; *cp && *cp != ':'; cp++)
  97.             ;
  98.         *cp++ = '\0';
  99.         curentry.pw_gid = atoi (cp);
  100.         for (; *cp && *cp != ':'; cp++)
  101.             ;
  102.         *cp++ = '\0';
  103.         curentry.pw_gecos = cp;
  104.         for (; *cp && *cp != ':'; cp++)
  105.             ;
  106.         *cp++ = '\0';
  107.         curentry.pw_dir = cp;
  108.         for (; *cp && *cp != ':'; cp++)
  109.             ;
  110.         *cp++ = '\0';
  111.         curentry.pw_shell = cp;
  112.         for (; *cp && *cp != ':' && *cp != '\n'; cp++)
  113.             ;
  114.         *cp++ = '\0';
  115.         return (1);
  116.     }
  117.     return (0);
  118. }
  119.  
  120. #ifdef TEST
  121. main (argc, argv)
  122. int argc;
  123. char **argv;
  124. {
  125.     struct passwd *pwd;
  126.  
  127.     if (argc > 1)
  128.         setpwfile (argv[1]);
  129.  
  130.     setpwent ();
  131.     while ((pwd = getpwent ()) != NULL)
  132.     {
  133.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  134.             pwd->pw_name,
  135.             pwd->pw_passwd,
  136.             pwd->pw_uid,
  137.             pwd->pw_gid,
  138.             pwd->pw_gecos,
  139.             pwd->pw_dir,
  140.             pwd->pw_shell);
  141.     }
  142.     endpwent ();
  143.  
  144.     if (pwd = getpwnam ("operator"))
  145.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  146.             pwd->pw_name,
  147.             pwd->pw_passwd,
  148.             pwd->pw_uid,
  149.             pwd->pw_gid,
  150.             pwd->pw_gecos,
  151.             pwd->pw_dir,
  152.             pwd->pw_shell);
  153.  
  154.     if (pwd = getpwuid (1))
  155.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  156.             pwd->pw_name,
  157.             pwd->pw_passwd,
  158.             pwd->pw_uid,
  159.             pwd->pw_gid,
  160.             pwd->pw_gecos,
  161.             pwd->pw_dir,
  162.             pwd->pw_shell);
  163. }
  164. #endif
  165.