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

  1. /*
  2.  * Routines for reading information from /etc/group.
  3.  * Written by Eric Smith, based on Arnold Robbins' public domain
  4.  * getpwent routines (see getpw.c).
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <grp.h>
  12.  
  13. static char *grpfile = "/etc/group";    /* default group file */
  14. static FILE *fp;
  15.  
  16. static struct group curentry;        /* static data to return */
  17.  
  18. static int nextent __PROTO((void));
  19.  
  20. void setgrent ()
  21. {
  22.     if (fp)
  23.         rewind (fp);
  24. #ifdef atarist
  25.     else if ((fp = fopen (grpfile, "rt")) == NULL)
  26. #else
  27.     else if ((fp = fopen (grpfile, "r")) == NULL)
  28. #endif
  29.     {
  30. #ifdef VERBOSE
  31.         fprintf (stderr,
  32.             "setgrent: %s non-existant or unreadable.\n", grpfile);
  33. #endif
  34.     }
  35. }
  36.  
  37. void endgrent ()
  38. {
  39.     if (fp)
  40.     {
  41.         fclose (fp);
  42.         fp = NULL;
  43.     }
  44. }
  45.  
  46. struct group *getgrent ()
  47. {
  48.     if (!fp) setgrent();
  49.     if (!fp) return NULL;
  50.  
  51.     if (! nextent ())
  52.         return (NULL);
  53.     else
  54.         return (& curentry);
  55. }
  56.  
  57. struct group *getgrgid (gid)
  58.     int gid;
  59. {
  60.     setgrent();
  61.  
  62.     while (nextent ())
  63.         if (curentry.gr_gid == gid)
  64.             return (& curentry);
  65.  
  66.     return (NULL);
  67. }
  68.  
  69. struct group *getgrnam (name)
  70. register char *name;
  71. {
  72.     setgrent();
  73.  
  74.     while (nextent ())
  75.         if (strcmp (curentry.gr_name, name) == 0)
  76.             return (& curentry);
  77.  
  78.     return (NULL);
  79. }
  80.  
  81. #define MAX_MEMBERS 128        /* max. number of members in a group */
  82.  
  83. static char savbuf[512];
  84. static char *memb[MAX_MEMBERS];
  85.  
  86. static int nextent ()
  87. {
  88.     register char *cp;
  89.     register int i;
  90.  
  91.     if (!fp) setgrent();
  92.     if (!fp) return 0;
  93.  
  94.     if (fgets (savbuf, (int) sizeof(savbuf), fp) != NULL)
  95.     {
  96.         for (cp = savbuf; *cp && *cp != ':'; cp++)
  97.             ;
  98.         curentry.gr_name = savbuf;
  99.         *cp++ = '\0';
  100.         curentry.gr_passwd = cp;
  101.         for (; *cp && *cp != ':'; cp++)
  102.             ;
  103.         *cp++ = '\0';
  104.         curentry.gr_gid = atoi (cp);
  105.         for (; *cp && *cp != ':'; cp++)
  106.             ;
  107.  
  108.         *cp++ = '\0';
  109.  
  110.         i = 0;
  111.         while( *cp ) {
  112.             if (i >= MAX_MEMBERS-1) break;
  113.             memb[i++] = cp;
  114.             for(; *cp && *cp != ',' && *cp != '\n'; cp++)
  115.                 ;
  116.             if (*cp == '\n')
  117.                 *cp = 0;
  118.             else if (*cp)
  119.                 *cp++ = 0;
  120.         }
  121.         memb[i] = NULL;
  122.         curentry.gr_mem = memb;
  123.         return (1);
  124.     }
  125.     return (0);
  126. }
  127.  
  128. #ifdef TEST
  129. int
  130. main (argc, argv)
  131. int argc;
  132. char **argv;
  133. {
  134.     struct group *gr;
  135.     char **m;
  136.  
  137.     if (argc > 1) {
  138.         grpfile = argv[1];
  139.     }
  140.  
  141.     setgrent ();
  142.     while ((gr = getgrent ()) != NULL)
  143.     {
  144.         printf ("%s:[%s] %d\n",
  145.             gr->gr_name,
  146.             gr->gr_passwd,
  147.             gr->gr_gid);
  148.         for (m = gr->gr_mem; *m; m++) {
  149.             printf("\t%s\n", *m);
  150.         }
  151.     }
  152.     endgrent ();
  153.  
  154.     if (gr = getgrnam ("mail")) {
  155.         printf ("%s:[%s] %d\n",
  156.             gr->gr_name,
  157.             gr->gr_passwd,
  158.             gr->gr_gid);
  159.         for (m = gr->gr_mem; *m; m++) {
  160.             printf("\t%s\n", *m);
  161.         }
  162.     }
  163.     if (gr = getgrgid (1)) {
  164.         printf ("%s:[%s] %d\n",
  165.             gr->gr_name,
  166.             gr->gr_passwd,
  167.             gr->gr_gid);
  168.         for (m = gr->gr_mem; *m; m++) {
  169.             printf("\t%s\n", *m);
  170.         }
  171.     }
  172.     return 0;
  173. }
  174. #endif
  175.