home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / grp / gshadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-19  |  4.1 KB  |  253 lines

  1. /*
  2.  * Copyright 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include <ansidecl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <shadow.h>
  17.  
  18. #if 0
  19. static char sccsid[] = "@(#)gshadow.c    3.7    08:45:58    9/12/91";
  20. #endif
  21.  
  22. #define MAXMEM 1024
  23. #define MAXBUF (BUFSIZ + MAXMEM * 9 + 1)
  24.  
  25. static FILE *shadow;
  26. static char *sgrbuf = NULL;
  27. static char **members;
  28. static char **admins;
  29. static struct sgrp sgroup;
  30.  
  31. #define FIELDS 4
  32.  
  33. static void
  34. DEFUN_VOID(initmem)
  35. {
  36.   if (sgrbuf)
  37.     return;
  38.  
  39.   sgrbuf = (char *) malloc (MAXBUF);
  40.   members = (char **) malloc (sizeof(char *) * (MAXMEM + 1));
  41.   admins = (char **) malloc (sizeof(char *) * (MAXMEM + 1));
  42. }
  43.  
  44. static char *
  45. DEFUN(fgetsx, (buf, cnt, f), char *buf AND int cnt AND FILE *f)
  46. {
  47.   char *cp = buf;
  48.   char *ep;
  49.  
  50.   while (cnt > 0)
  51.     {
  52.       if (!fgets (cp, cnt, f))
  53.     if (cp == buf)
  54.       return NULL;
  55.     else
  56.       break;
  57.  
  58.       if ((ep = strrchr (cp, '\\')) && *(ep + 1) == '\n')
  59.     {
  60.       if ((cnt -= ep - cp) > 0)
  61.         *(cp = ep) = '\0';
  62.     }
  63.       else
  64.     break;
  65.     }
  66.   return buf;
  67. }
  68.  
  69. static int
  70. DEFUN(fputsx, (s, stream), char *s AND FILE *stream)
  71. {
  72.   int i;
  73.  
  74.   for (i = 0; *s; i++, s++)
  75.     {
  76.       if (putc (*s, stream) == EOF)
  77.     return EOF;
  78.  
  79.       if (i > BUFSIZ / 2) {
  80.     if (putc ('\\', stream) == EOF
  81.         || putc ('\n', stream) == EOF)
  82.       return EOF;
  83.  
  84.     i = 0;
  85.       }
  86.     }
  87.   return 0;
  88. }
  89.  
  90. static char **
  91. DEFUN(list, (s, l), char *s AND char **l)
  92. {
  93.   int nmembers = 0;
  94.  
  95.   while (s && *s)
  96.     {
  97.       l[nmembers++] = s;
  98.       if ((s = strchr (s, ',')))
  99.     *s++ = '\0';
  100.     }
  101.   l[nmembers] = (char *) 0;
  102.   return l;
  103. }
  104.  
  105. void
  106. DEFUN_VOID(setsgent)
  107. {
  108.   if (shadow)
  109.     rewind (shadow);
  110.   else
  111.     shadow = fopen (GSHADOW, "r");
  112. }
  113.  
  114. void
  115. DEFUN_VOID(endsgent)
  116. {
  117.   if (shadow)
  118.     {
  119.       fclose (shadow);
  120.       shadow = NULL;
  121.     }
  122. }
  123.  
  124. struct sgrp *
  125. DEFUN(sgetsgent, (string), CONST char *string)
  126. {
  127.   char *fields[FIELDS];
  128.   char *cp;
  129.   int i;
  130.  
  131.   initmem();
  132.  
  133.   strncpy (sgrbuf, string, MAXBUF);
  134.   sgrbuf[MAXBUF] = '\0';
  135.  
  136.   if ((cp = strrchr (sgrbuf, '\n')))
  137.     *cp = '\0';
  138.  
  139.   for (cp = sgrbuf, i = 0; i < FIELDS && cp; i++)
  140.     {
  141.       fields[i] = cp;
  142.       if ((cp = strchr (cp, ':')))
  143.     *cp++ = '\0';
  144.     }
  145.   if ((cp && *cp) || i != FIELDS)
  146.     return NULL;
  147.  
  148.   sgroup.sg_name = fields[0];
  149.   sgroup.sg_passwd = fields[1];
  150.   sgroup.sg_adm = list (fields[2], admins);
  151.   sgroup.sg_mem = list (fields[3], members);
  152.  
  153.   return &sgroup;
  154. }
  155.  
  156. struct sgrp *
  157. DEFUN(fgetsgent, (fp), FILE *fp)
  158. {
  159.   char buf[sizeof sgrbuf];
  160.  
  161.   if (!fp)
  162.     return NULL;
  163.  
  164.   if (fgetsx (buf, sizeof buf, fp))
  165.     return NULL;
  166.  
  167.   return sgetsgent (buf);
  168. }
  169.  
  170. struct sgrp *
  171. DEFUN_VOID(getsgent)
  172. {
  173.   if (!shadow)
  174.     setsgent ();
  175.  
  176.   return fgetsgent (shadow);
  177. }
  178.  
  179. struct sgrp *
  180. DEFUN(getsgnam, (name), CONST char *name)
  181. {
  182.   struct sgrp *sgrp;
  183.  
  184.   setsgent ();
  185.  
  186.   while ((sgrp = getsgent ()))
  187.     {
  188.       if (strcmp (name, sgrp->sg_name) == 0)
  189.     return (sgrp);
  190.     }
  191.   return NULL;
  192. }
  193.  
  194. int
  195. DEFUN(putsgent, (sgrp, fp), CONST struct sgrp *sgrp AND FILE *fp)
  196. {
  197.   char buf[sizeof sgrbuf];
  198.   char *cp = buf;
  199.   int i;
  200.  
  201.   if (!fp || !sgrp)
  202.     return -1;
  203.  
  204.   /*
  205.    * Copy the group name and passwd.
  206.    */
  207.  
  208.   strcpy (cp, sgrp->sg_name);
  209.   cp += strlen (cp);
  210.   *cp++ = ':';
  211.  
  212.   strcpy (cp, sgrp->sg_passwd);
  213.   cp += strlen (cp);
  214.   *cp++ = ':';
  215.  
  216.   /*
  217.    * Copy the administrators, separating each from the other
  218.    * with a ",".
  219.    */
  220.  
  221.   for (i = 0; sgrp->sg_adm[i]; i++)
  222.     {
  223.       if (i > 0)
  224.     *cp++ = ',';
  225.  
  226.       strcpy (cp, sgrp->sg_adm[i]);
  227.       cp += strlen (cp);
  228.     }
  229.   *cp++ = ':';
  230.  
  231.   /*
  232.    * Now do likewise with the group members.
  233.    */
  234.  
  235.   for (i = 0; sgrp->sg_mem[i]; i++)
  236.     {
  237.       if (i > 0)
  238.     *cp++ = ',';
  239.  
  240.       strcpy (cp, sgrp->sg_mem[i]);
  241.       cp += strlen (cp);
  242.     }
  243.   *cp++ = '\n';
  244.   *cp = '\0';
  245.  
  246.   /*
  247.    * Output using the function which understands the line
  248.    * continuation conventions.
  249.    */
  250.  
  251.   return fputsx (buf, fp);
  252. }
  253.