home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / getpw.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  4KB  |  213 lines

  1. /*
  2. Subject:  v08i080:  Public-domain getpw*(3) routines
  3. Newsgroups: mod.sources
  4. Approved: mirror!rs
  5.  
  6. Submitted by: emoryu1!emoryu2!arnold (Arnold D. Robbins)
  7. Mod.sources: Volume 8, Issue 80
  8. Archive-name: getpw
  9.  
  10. Here is public domain re-implementation of the getpwent(3) routines. I have
  11. not included a manual page, since every Unix system has one. I also haven't
  12. even bothered to include a <pwd.h> file; you should be able to use the one
  13. on your system.
  14.  
  15. There is one additional routine:
  16.       setpwfile (file)
  17.       char *file;
  18. which will cause the routines to find password records from a file besides
  19. /etc/passwd.  This is useful should you need to use saved password files,
  20. for instance in doing Unix accounting, where you wish to keep info around on
  21. old accounts, but take the old accounts out of the live password file.
  22. (Can you guess why I just whipped these up?)
  23.  
  24. To switch files, call setpwfile as the very first thing, or call endpwent(),
  25. then setpwfile ("/some/file").
  26.  
  27. Anyway, I hope this is useful to somene out there.
  28. Arnold Robbins
  29. arnold@emoryu1.{CSNET, UUCP, ARPA, BITNET}
  30. */
  31.  
  32. /* minorly customized by ERS for the MiNT library */
  33.  
  34. #include <stdio.h>
  35. #include <pwd.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39.  
  40. static char *pwdfile = "/etc/passwd";    /* default passwd file */
  41.  
  42. static FILE *fp = NULL;
  43. static struct passwd curentry;        /* static data to return */
  44.  
  45. static int nextent __PROTO((void));
  46.  
  47. void setpwfile (cp)
  48. char *cp;
  49. {
  50.     endpwent();
  51.     if(cp) pwdfile = cp;
  52. }
  53.  
  54. void setpwent ()
  55. {
  56.     if (fp)
  57.         rewind (fp);
  58. #ifdef atarist
  59.     else if ((fp = fopen (pwdfile, "rt")) == NULL)
  60. #else
  61.     else if ((fp = fopen (pwdfile, "r")) == NULL)
  62. #endif
  63.     {
  64. #ifdef VERBOSE
  65.         fprintf (stderr,
  66.             "setpwent: %s non-existant or unreadable.\n", pwdfile);
  67. #endif
  68.     }
  69. }
  70.  
  71. void endpwent ()
  72. {
  73.     if (fp)
  74.     {
  75.         fclose (fp);
  76.         fp = NULL;
  77.     }
  78. }
  79.  
  80. struct passwd *getpwent ()
  81. {
  82.     if (! fp ) {
  83.         setpwent();
  84.         if (! fp )
  85.             return (NULL);
  86.     }
  87.  
  88.     if (! nextent ())
  89.         return (NULL);
  90.     else
  91.         return (& curentry);
  92. }
  93.  
  94. struct passwd *getpwuid (uid)
  95. register int uid;
  96. {
  97.     setpwent ();
  98.     while (nextent ())
  99.         if (curentry.pw_uid == uid)
  100.             return (& curentry);
  101.  
  102.     return (NULL);
  103. }
  104.  
  105. struct passwd *getpwnam (name)
  106. register const char *name;
  107. {
  108.     setpwent ();
  109.  
  110.     while (nextent ())
  111.         if (strcmp (curentry.pw_name, name) == 0)
  112.             return (& curentry);
  113.  
  114.     return (NULL);
  115. }
  116.  
  117. #ifdef atarist
  118. static char savbuf[256];    /* BUFSIZ seems bigger than necessary! */
  119. #else
  120. static char savbuf[BUFSIZ];
  121. #endif
  122.         
  123. static int nextent ()
  124. {
  125.     register char *cp;
  126.  
  127.     if (! fp ) {
  128.         setpwent ();
  129.         if (! fp )
  130.             return (0);
  131.     }
  132.  
  133.     while (fgets (savbuf, (int)sizeof(savbuf), fp) != NULL)
  134.     {
  135.         for (cp = savbuf; *cp && *cp != ':'; cp++)
  136.             ;
  137.         curentry.pw_name = savbuf;
  138.         *cp++ = '\0';
  139.         curentry.pw_passwd = cp;
  140.         for (; *cp && *cp != ':'; cp++)
  141.             ;
  142.         *cp++ = '\0';
  143.         curentry.pw_uid = atoi (cp);
  144.         for (; *cp && *cp != ':'; cp++)
  145.             ;
  146.         *cp++ = '\0';
  147.         curentry.pw_gid = atoi (cp);
  148.         for (; *cp && *cp != ':'; cp++)
  149.             ;
  150.         *cp++ = '\0';
  151.         curentry.pw_gecos = cp;
  152.         for (; *cp && *cp != ':'; cp++)
  153.             ;
  154.         *cp++ = '\0';
  155.         curentry.pw_dir = cp;
  156.         for (; *cp && *cp != ':'; cp++)
  157.             ;
  158.         *cp++ = '\0';
  159.         curentry.pw_shell = cp;
  160.         for (; *cp && *cp != ':' && *cp != '\n'; cp++)
  161.             ;
  162.         *cp++ = '\0';
  163.         return (1);
  164.     }
  165.     return (0);
  166. }
  167.  
  168. #ifdef TEST
  169. main (argc, argv)
  170. int argc;
  171. char **argv;
  172. {
  173.     struct passwd *pwd;
  174.  
  175.     if (argc > 1)
  176.         setpwfile (argv[1]);
  177.  
  178.     setpwent ();
  179.     while ((pwd = getpwent ()) != NULL)
  180.     {
  181.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  182.             pwd->pw_name,
  183.             pwd->pw_passwd,
  184.             pwd->pw_uid,
  185.             pwd->pw_gid,
  186.             pwd->pw_gecos,
  187.             pwd->pw_dir,
  188.             pwd->pw_shell);
  189.     }
  190.     endpwent ();
  191.  
  192.     if (pwd = getpwnam ("operator"))
  193.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  194.             pwd->pw_name,
  195.             pwd->pw_passwd,
  196.             pwd->pw_uid,
  197.             pwd->pw_gid,
  198.             pwd->pw_gecos,
  199.             pwd->pw_dir,
  200.             pwd->pw_shell);
  201.  
  202.     if (pwd = getpwuid (1))
  203.         printf ("%s:\n\t%s\n\t%d\n\t%d\n\t%s\n\t%s\n\t%s\n",
  204.             pwd->pw_name,
  205.             pwd->pw_passwd,
  206.             pwd->pw_uid,
  207.             pwd->pw_gid,
  208.             pwd->pw_gecos,
  209.             pwd->pw_dir,
  210.             pwd->pw_shell);
  211. }
  212. #endif
  213.