]> git.ipfire.org Git - thirdparty/glibc.git/blob - grp/fgetgrent_r.c
Update.
[thirdparty/glibc.git] / grp / fgetgrent_r.c
1 /* Copyright (C) 1991, 1996, 1997 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <stdio.h>
23
24 /* Define a line parsing function using the common code
25 used in the nss_files module. */
26
27 #define STRUCTURE group
28 #define ENTNAME grent
29 struct grent_data {};
30
31 #define TRAILING_LIST_MEMBER gr_mem
32 #define TRAILING_LIST_SEPARATOR_P(c) ((c) == ',')
33 #include <nss/nss_files/files-parse.c>
34 LINE_PARSER
35 (,
36 STRING_FIELD (result->gr_name, ISCOLON, 0);
37 if (line[0] == '\0'
38 && (result->gr_name[0] == '+' || result->gr_name[0] == '-'))
39 {
40 result->gr_passwd = NULL;
41 result->gr_gid = 0;
42 }
43 else
44 {
45 STRING_FIELD (result->gr_passwd, ISCOLON, 0);
46 if (result->gr_name[0] == '+' || result->gr_name[0] == '-')
47 INT_FIELD_MAYBE_NULL (result->gr_gid, ISCOLON, 0, 10, , 0)
48 else
49 INT_FIELD (result->gr_gid, ISCOLON, 0, 10,)
50 }
51 )
52
53
54 /* Read one entry from the given stream. */
55 int
56 __fgetgrent_r (FILE *stream, struct group *resbuf, char *buffer, size_t buflen,
57 struct group **result)
58 {
59 char *p;
60 int parse_result;
61
62 do
63 {
64 p = fgets (buffer, buflen, stream);
65 if (p == NULL)
66 {
67 *result = NULL;
68 return errno;
69 }
70
71 /* Skip leading blanks. */
72 while (isspace (*p))
73 ++p;
74 } while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
75 /* Parse the line. If it is invalid, loop to
76 get the next line of the file to parse. */
77 || ! (parse_result = parse_line (p, resbuf,
78 (void *) buffer, buflen,
79 __errno_location ())));
80
81 if (parse_result == -1)
82 {
83 /* The parser ran out of space. */
84 *result = NULL;
85 return errno;
86 }
87
88 *result = resbuf;
89 return 0;
90 }
91 weak_alias (__fgetgrent_r, fgetgrent_r)