home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2288 / grent.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  1KB  |  70 lines

  1. /*
  2.  * Copyright 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Non-commercial distribution permitted.  You must provide this source
  6.  * code in any distribution.  This notice must remain intact.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <grp.h>
  11. #include <string.h>
  12. #include "config.h"
  13. #ifdef    DBM
  14. #include <dbm.h>
  15. #endif
  16.  
  17. #ifndef    lint
  18. static    char    _sccsid[] = "@(#)grent.c    1.1    08:14:07    6/20/90";
  19. #endif
  20.  
  21. #define    NFIELDS    4
  22. #define    MAXMEM    1024
  23.  
  24. static    char    grpbuf[4*BUFSIZ];
  25. static    char    *grpfields[NFIELDS];
  26. static    char    *members[MAXMEM+1];
  27.  
  28. static char **
  29. list (s)
  30. char    *s;
  31. {
  32.     int    nmembers = 0;
  33.  
  34.     while (*s) {
  35.         members[nmembers++] = s;
  36.         if (s = strchr (s, ','))
  37.             *s++ = '\0';
  38.     }
  39.     members[nmembers] = (char *) 0;
  40.     return members;
  41. }
  42.  
  43. struct    group    *sgetgrent (buf)
  44. char    *buf;
  45. {
  46.     int    i;
  47.     char    *cp;
  48.     static    struct    group    grent;
  49.  
  50.     strncpy (grpbuf, buf, sizeof grpbuf);
  51.     grpbuf[sizeof grpbuf - 1] = '\0';
  52.     if (cp = strrchr (grpbuf, '\n'))
  53.         *cp = '\0';
  54.  
  55.     for (cp = grpbuf, i = 0;i < NFIELDS && cp;i++) {
  56.         grpfields[i] = cp;
  57.         if (cp = strchr (cp, ':'))
  58.             *cp++ = 0;
  59.     }
  60.     if (i < (NFIELDS-1) || *grpfields[2] == '\0')
  61.         return ((struct group *) 0);
  62.  
  63.     grent.gr_name = grpfields[0];
  64.     grent.gr_passwd = grpfields[1];
  65.     grent.gr_gid = atoi (grpfields[2]);
  66.     grent.gr_mem = list (grpfields[3]);
  67.  
  68.     return (&grent);
  69. }
  70.