home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2082 / grent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.8 KB  |  106 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    3.1    08:52:58    11/15/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.  
  71. int
  72. putgrent (g, f)
  73. struct    group    *g;
  74. FILE    *f;
  75. {
  76.     int    i;
  77.     char    *cp;
  78.  
  79.     if (! g || ! f)
  80.         return -1;
  81.  
  82.     sprintf (grpbuf, "%s:%s:%d:", g->gr_name, g->gr_passwd, g->gr_gid);
  83.     if (g->gr_mem) {
  84.         cp = strchr (grpbuf, '\0');
  85.         for (i = 0;g->gr_mem[i];i++) {
  86.             if (cp - grpbuf + strlen (g->gr_mem[i]) + 2
  87.                     >= sizeof grpbuf)
  88.                 return -1;
  89.  
  90.             if (i > 0) {
  91.                 strcpy (cp, ",");
  92.                 cp = strchr (cp, '\0');
  93.             }
  94.             strcpy (cp, g->gr_mem[i]);
  95.             cp = strchr (cp, '\0');
  96.         }
  97.         strcat (cp, "\n");
  98.     } else
  99.         strcat (grpbuf, "\n");
  100.  
  101.     if (fputs (grpbuf, f) == EOF || ferror (f))
  102.         return -1;
  103.  
  104.     return 0;
  105. }
  106.