home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / grp.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  4KB  |  201 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. /*
  8.   Revised by Hildo Biersma (boender@dutiws.twi.tudelft.nl)
  9.   Revision 1 (January 31, 1993): include fgetgrent()
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <grp.h>
  17.  
  18. static char *grpfile = "/etc/group";  /* default group file */
  19. static FILE *fp;
  20.  
  21. static struct group curentry;   /* static data to return */
  22.  
  23. static int nextent __PROTO((FILE *fp));
  24.  
  25. /* Rewind the group file to allow repeated searches */
  26. void setgrent()
  27. {
  28.   if (fp != NULL)
  29.     rewind (fp);
  30.   else if ((fp = fopen (grpfile, "rt")) == NULL)
  31.   {
  32. #ifdef VERBOSE
  33.     fprintf (stderr,
  34.       "setgrent: %s non-existant or unreadable.\n", grpfile);
  35. #endif
  36.   }
  37. } /* End of setgrent() */
  38.  
  39. /* Close the group file when processing is complete */
  40. void endgrent()
  41. {
  42.   if (fp != NULL)
  43.   {
  44.     fclose (fp);
  45.     fp = NULL;
  46.   }
  47. } /* End of endgrent() */
  48.  
  49. /* Get the next group structure in the file */
  50. struct group *getgrent()
  51. {
  52.   if (fp == NULL)
  53.     setgrent();
  54.   if (fp == NULL)
  55.     return NULL;
  56.  
  57.   if (nextent(fp) == 0)
  58.     return(NULL);
  59.   else
  60.     return(&curentry);
  61. } /* End of getgrent() */
  62.  
  63. /* Get first group with matching numerical group ID from file */
  64. struct group *getgrgid(gid)
  65. int gid;
  66. {
  67.   setgrent();
  68.  
  69.   while (nextent(fp) != 0)
  70.     if (curentry.gr_gid == gid)
  71.       return(&curentry);
  72.  
  73.   return(NULL);
  74. } /* End of getgrid() */
  75.  
  76. /* Get first group with matching group name from file */
  77. struct group *getgrnam(name)
  78. const char *name;
  79. {
  80.   setgrent();
  81.  
  82.   while (nextent(fp) != 0)
  83.     if (strcmp (curentry.gr_name, name) == 0)
  84.       return (&curentry);
  85.  
  86.   return(NULL);
  87. } /* End of getgrnam() */
  88.  
  89. /* Read the next group structure from a given file */
  90. struct group *fgetgrent(f)
  91. FILE *f;
  92. {
  93.   if (f == NULL)
  94.     return(NULL); /* Failure */
  95.     
  96.   if (nextent(f) != 0)
  97.     return(&curentry); /* Success */
  98.  
  99.   return(NULL); /* Failure */
  100. } /* End of fgetgrent() */
  101.  
  102. #define MAX_MEMBERS 128   /* max. number of members in a group */
  103.  
  104. static char savbuf[512];
  105. static char *memb[MAX_MEMBERS];
  106.  
  107. static int nextent(fp)
  108. FILE *fp;
  109. {
  110.   register char *cp;
  111.   register int i;
  112.  
  113.   if (fp == NULL)
  114.     setgrent();
  115.   if (fp == NULL)
  116.     return(0);
  117.  
  118.   if (fgets(savbuf, (int)sizeof(savbuf), fp) != NULL)
  119.   {
  120.     for (cp = savbuf; *cp && *cp != ':'; cp++)
  121.       ;
  122.     curentry.gr_name = savbuf;
  123.     *cp++ = 0x00;
  124.     curentry.gr_passwd = cp;
  125.     for (; *cp && *cp != ':'; cp++)
  126.       ;
  127.     *cp++ = 0x00;
  128.     curentry.gr_gid = atoi (cp);
  129.     for (; *cp && *cp != ':'; cp++)
  130.       ;
  131.  
  132.     *cp++ = 0x00;
  133.  
  134.     i = 0;
  135.     while( *cp )
  136.     {
  137.       if (i >= MAX_MEMBERS-1)
  138.         break;
  139.       memb[i++] = cp;
  140.       for(; *cp && *cp != ',' && *cp != '\n'; cp++)
  141.         ;
  142.       if (*cp == '\n')
  143.         *cp = 0;
  144.       else if (*cp)
  145.         *cp++ = 0;
  146.     }
  147.     memb[i] = NULL;
  148.     curentry.gr_mem = memb;
  149.     return(1);
  150.   }
  151.   return(0);
  152. } /* End of nextent() */
  153.  
  154. #ifdef TEST
  155. int
  156. main (argc, argv)
  157. int argc;
  158. char **argv;
  159. {
  160.   struct group *gr;
  161.   char **m;
  162.  
  163.   if (argc > 1) {
  164.     grpfile = argv[1];
  165.   }
  166.  
  167.   setgrent ();
  168.   while ((gr = getgrent ()) != NULL)
  169.   {
  170.     printf ("%s:[%s] %d\n",
  171.       gr->gr_name,
  172.       gr->gr_passwd,
  173.       gr->gr_gid);
  174.     for (m = gr->gr_mem; *m; m++) {
  175.       printf("\t%s\n", *m);
  176.     }
  177.   }
  178.   endgrent ();
  179.  
  180.   if (gr = getgrnam ("mail")) {
  181.     printf ("%s:[%s] %d\n",
  182.       gr->gr_name,
  183.       gr->gr_passwd,
  184.       gr->gr_gid);
  185.     for (m = gr->gr_mem; *m; m++) {
  186.       printf("\t%s\n", *m);
  187.     }
  188.   }
  189.   if (gr = getgrgid (1)) {
  190.     printf ("%s:[%s] %d\n",
  191.       gr->gr_name,
  192.       gr->gr_passwd,
  193.       gr->gr_gid);
  194.     for (m = gr->gr_mem; *m; m++) {
  195.       printf("\t%s\n", *m);
  196.     }
  197.   }
  198.   return 0;
  199. }
  200. #endif
  201.