Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / grp / grpread.c
blob1fed32f2e31095f302939623f3de45dd874bfc1e
1 /* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
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>
28 /* This is the function that all the others are based on.
29 The format of the group file is known only here. */
31 /* Structure containing info kept by each __grpread caller. */
32 typedef struct
34 char *buf;
35 size_t buflen;
36 size_t max_members;
37 char **members;
38 struct group g;
39 } grpread_info;
42 /* Return a chunk of memory containing a pre-initialized `grpread_info'. */
43 PTR
44 DEFUN_VOID(__grpalloc)
46 grpread_info *info = (PTR) malloc (sizeof(grpread_info));
47 if (info == NULL)
48 return NULL;
50 info->buf = NULL;
51 info->buflen = 0;
53 info->max_members = 5;
54 info->members = (char **) malloc (5 * sizeof(char *));
55 if (info->members == NULL)
57 free ((PTR) info);
58 return NULL;
61 return info;
64 /* Read a group entry from STREAM, filling in G. */
65 struct group *
66 DEFUN(__grpread, (stream, g), FILE *stream AND PTR CONST g)
68 register grpread_info *CONST info = (grpread_info *) g;
69 char *start, *end;
70 register size_t i;
72 /* Idiocy checks. */
73 if (stream == NULL)
75 errno = EINVAL;
76 return NULL;
80 if (__getline (&info->buf, &info->buflen, stream) == -1)
81 return NULL;
82 while (info->buf[0] == '#');
84 start = info->buf;
85 end = strchr (start, ':');
86 if (end == NULL)
87 return NULL;
88 *end = '\0';
89 info->g.gr_name = start;
91 start = end + 1;
92 end = strchr (start, ':');
93 if (end == NULL)
94 return NULL;
95 *end = '\0';
96 info->g.gr_passwd = start;
98 info->g.gr_gid = (gid_t) strtol (end + 1, &end, 10);
99 if (*end != ':')
100 return NULL;
102 i = 0;
105 start = end + 1;
106 end = strchr (start, ',');
107 if (end == NULL)
109 end = strchr (start, '\n');
110 if (end == start)
111 break;
112 if (end == NULL)
113 return NULL;
114 *end = '\0';
115 end = NULL;
117 else
118 *end = '\0';
120 if (i == info->max_members - 2)
122 info->max_members += 5;
123 info->members = (char **)
124 realloc ((PTR) info->members, info->max_members * sizeof (char *));
125 if (info->members == NULL)
126 return NULL;
129 info->members[i++] = start;
130 } while (end != NULL);
131 info->members[i] = NULL;
132 info->g.gr_mem = info->members;
134 return &info->g;
138 struct group *
139 __grpscan (void **info, int (*selector) (struct group *))
141 FILE *stream;
142 struct group *p;
144 if (*info == NULL)
146 *info = __grpalloc ();
147 if (info == NULL)
148 return NULL;
151 stream = __grpopen ();
152 if (stream == NULL)
153 return NULL;
155 p = NULL;
156 while (! feof (stream))
158 p = __grpread (stream, *info);
159 if (p && (*selector) (p))
160 break;
161 p = NULL;
164 (void) fclose (stream);
165 return p;