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. */
25 #include <sys/types.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. */
42 /* Return a chunk of memory containing a pre-initialized `grpread_info'. */
44 DEFUN_VOID(__grpalloc
)
46 grpread_info
*info
= (PTR
) malloc (sizeof(grpread_info
));
53 info
->max_members
= 5;
54 info
->members
= (char **) malloc (5 * sizeof(char *));
55 if (info
->members
== NULL
)
64 /* Read a group entry from STREAM, filling in G. */
66 DEFUN(__grpread
, (stream
, g
), FILE *stream AND PTR CONST g
)
68 register grpread_info
*CONST info
= (grpread_info
*) g
;
80 if (__getline (&info
->buf
, &info
->buflen
, stream
) == -1)
82 while (info
->buf
[0] == '#');
85 end
= strchr (start
, ':');
89 info
->g
.gr_name
= start
;
92 end
= strchr (start
, ':');
96 info
->g
.gr_passwd
= start
;
98 info
->g
.gr_gid
= (gid_t
) strtol (end
+ 1, &end
, 10);
106 end
= strchr (start
, ',');
109 end
= strchr (start
, '\n');
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
)
129 info
->members
[i
++] = start
;
130 } while (end
!= NULL
);
131 info
->members
[i
] = NULL
;
132 info
->g
.gr_mem
= info
->members
;
139 __grpscan (void **info
, int (*selector
) (struct group
*))
146 *info
= __grpalloc ();
151 stream
= __grpopen ();
156 while (! feof (stream
))
158 p
= __grpread (stream
, *info
);
159 if (p
&& (*selector
) (p
))
164 (void) fclose (stream
);