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 / grpread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  5.4 KB  |  238 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <grp.h>
  27.  
  28. #ifdef YP
  29. extern struct group * __nis_parsegroupdata(char *, void *);
  30. #endif
  31.  
  32. /* This is the function that all the others are based on.
  33.    The format of the group file is known only here.  */
  34.  
  35. /* Structure containing info kept by each __grpread caller.  */
  36. typedef struct
  37. {
  38.   char *buf;
  39.   size_t buflen;
  40.   size_t max_members;
  41.   char **members;
  42.   struct group g;
  43. } grpread_info;
  44.  
  45.  
  46. /* Return a chunk of memory containing a pre-initialized `grpread_info'.  */
  47. PTR
  48. DEFUN_VOID(__grpalloc)
  49. {
  50.   grpread_info *info = (PTR) malloc (sizeof(grpread_info));
  51.   if (info == NULL)
  52.     return NULL;
  53.   
  54.   info->buf = NULL;
  55.   info->buflen = 0;
  56.   
  57.   info->max_members = 5;
  58.   info->members = (char **) malloc (5 * sizeof(char *));
  59.   if (info->members == NULL)
  60.     {
  61.       free ((PTR) info);
  62.       return NULL;
  63.     }
  64.   
  65.   return info;
  66. }
  67.  
  68. /* Read a group entry from STREAM, filling in G.  */
  69. struct group *
  70. DEFUN(__grpread, (stream, g), FILE *stream AND PTR CONST g)
  71. {
  72.   register grpread_info *CONST info = (grpread_info *) g;
  73.   char *start, *end;
  74.   register size_t i;
  75.   int is_nis_entry = 0;
  76.   
  77.       /* Idiocy checks.  */
  78.   if (stream == NULL)
  79.     {
  80.       errno = EINVAL;
  81.       return NULL;
  82.     }
  83.   
  84.   do
  85.     {
  86.       if (__getline (&info->buf, &info->buflen, stream) == -1)
  87.         return NULL;
  88.     } while (info->buf[0] == '#');
  89.   
  90.   start = info->buf;
  91. #ifdef YP
  92.   if ('+' == *start || '-' == *start)
  93.     {
  94.       is_nis_entry = 1;
  95.       info->g.gr_passwd = NULL;
  96.     }
  97. #endif
  98.   end = strchr (start, ':');
  99.   info->g.gr_name = start;
  100.   if (end == NULL)
  101.     {
  102.       if(!is_nis_entry)
  103.         return NULL;
  104.       end = strchr(info->g.gr_name, '\n');
  105.       if (NULL != end)
  106.         *end = '\0';
  107.       return &info->g;
  108.     }
  109.   *end = '\0';
  110.     
  111.   start = end + 1;
  112.   end = strchr (start, ':');
  113.   info->g.gr_passwd = start;
  114.   if (end == NULL)
  115.     {
  116.       if(!is_nis_entry)
  117.         return NULL;
  118.       end = strchr(info->g.gr_passwd, '\n');
  119.       if (NULL != end)
  120.         *end = '\0';
  121.       return &info->g;
  122.     }
  123.   *end = '\0';
  124.   
  125.   info->g.gr_gid = (gid_t) strtol (end + 1, &end, 10);
  126.   if (*end != ':')
  127.     return ( is_nis_entry ? &info->g : NULL );
  128.   
  129.   i = 0;
  130.   do
  131.     {
  132.       start = end + 1;
  133.       end = strchr (start, ',');
  134.       if (end == NULL)
  135.         {
  136.           end = strchr (start, '\n');
  137.           if (end == start)
  138.             break;
  139.           if (end == NULL)
  140.             return NULL;
  141.           *end = '\0';
  142.           end = NULL;
  143.         }
  144.       else
  145.         *end = '\0';
  146.       
  147.       if (i == info->max_members - 2)
  148.         {
  149.           info->max_members += 5;
  150.           info->members = (char **)
  151.             realloc ((PTR) info->members, info->max_members * sizeof (char *));
  152.           if (info->members == NULL)
  153.             return NULL;
  154.         }
  155.       
  156.       info->members[i++] = start;
  157.     } while (end != NULL);
  158.   info->members[i] = NULL;
  159.   info->g.gr_mem = info->members;
  160.   
  161.   return &info->g;
  162. }
  163.  
  164. #ifdef YP
  165. struct group *
  166. __nis_parsegroupdata(char *line, void *g)
  167. {
  168.   register grpread_info *const info = (grpread_info *)g;
  169.   char *start, *end;
  170.   char **memtmp;
  171.   struct group *grptr = &info->g ;
  172.   register int i;
  173.  
  174.   i = strlen(line) + 1;
  175.   if (NULL == info->buf || info->buflen < i)
  176.     {
  177.       start = (NULL == info->buf) ? malloc(i) : realloc(info->buf, i);
  178.       if (NULL == start)
  179.         return NULL;
  180.       info->buf = start;
  181.       info->buflen = i;
  182.     }
  183.   strcpy(info->buf, line);
  184.   
  185.   start = info->buf;
  186.   end = strchr (start, ':');
  187.   if (end == NULL)
  188.     return NULL;
  189.   *end = '\0';
  190.   
  191.   grptr->gr_name = start;
  192.   
  193.   start = end + 1;
  194.   end = strchr (start, ':');
  195.   if (end == NULL)
  196.       return NULL;
  197.   *end = '\0';
  198.   grptr->gr_passwd = start;
  199.   
  200.   grptr->gr_gid = (gid_t) strtol (end + 1, &end, 10);
  201.   if (*end != ':')
  202.       return NULL;
  203.  
  204.   i = 0;
  205.   do
  206.     {
  207.       start = end + 1;
  208.       end = strchr (start, ',');
  209.       if (NULL == end)
  210.         {
  211.           end = strchr (start, '\n');
  212.           if (end == start)
  213.             break;
  214.           if (NULL == end)
  215.               return NULL;
  216.           *end = '\0';
  217.           end = NULL;
  218.         }
  219.       else
  220.         *end = '\0';
  221.       if (i == info->max_members - 2)
  222.         {
  223.           info->max_members += 5;
  224.           memtmp = (char **)
  225.             realloc ((PTR) info->members, info->max_members * sizeof (char *));
  226.           if (NULL == memtmp)
  227.               return NULL;
  228.           info->members = memtmp;
  229.         }
  230.       info->members[i++] = start;
  231.     } while (end != NULL);
  232.   info->members[i] = NULL;
  233.   grptr->gr_mem = info->members;
  234.   
  235.   return grptr;
  236. }
  237. #endif
  238.