]> git.ipfire.org Git - thirdparty/glibc.git/blame - grp/fgetgrent_r.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / grp / fgetgrent_r.c
CommitLineData
f7a9f785 1/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
2303f5fd 2 This file is part of the GNU C Library.
267ca16a 3
2303f5fd 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
267ca16a 8
2303f5fd
UD
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
41bdb6e2 12 Lesser General Public License for more details.
267ca16a 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
267ca16a
UD
17
18#include <ctype.h>
d71b808a 19#include <errno.h>
267ca16a
UD
20#include <grp.h>
21#include <stdio.h>
22
3ce1f295
UD
23#include <libio/iolibio.h>
24#define flockfile(s) _IO_flockfile (s)
25#define funlockfile(s) _IO_funlockfile (s)
50304ef0 26
267ca16a
UD
27/* Define a line parsing function using the common code
28 used in the nss_files module. */
29
30#define STRUCTURE group
31#define ENTNAME grent
32struct grent_data {};
33
34#define TRAILING_LIST_MEMBER gr_mem
35#define TRAILING_LIST_SEPARATOR_P(c) ((c) == ',')
d71b808a 36#include <nss/nss_files/files-parse.c>
267ca16a
UD
37LINE_PARSER
38(,
39 STRING_FIELD (result->gr_name, ISCOLON, 0);
2303f5fd
UD
40 if (line[0] == '\0'
41 && (result->gr_name[0] == '+' || result->gr_name[0] == '-'))
42 {
43 result->gr_passwd = NULL;
44 result->gr_gid = 0;
45 }
6ed0492f 46 else
2303f5fd
UD
47 {
48 STRING_FIELD (result->gr_passwd, ISCOLON, 0);
49 if (result->gr_name[0] == '+' || result->gr_name[0] == '-')
50 INT_FIELD_MAYBE_NULL (result->gr_gid, ISCOLON, 0, 10, , 0)
51 else
52 INT_FIELD (result->gr_gid, ISCOLON, 0, 10,)
53 }
267ca16a
UD
54 )
55
56
57/* Read one entry from the given stream. */
ba1ffaa1
UD
58int
59__fgetgrent_r (FILE *stream, struct group *resbuf, char *buffer, size_t buflen,
60 struct group **result)
267ca16a
UD
61{
62 char *p;
22d57dd3 63 int parse_result;
267ca16a 64
50304ef0 65 flockfile (stream);
267ca16a
UD
66 do
67 {
31f7410f 68 buffer[buflen - 1] = '\xff';
50304ef0 69 p = fgets_unlocked (buffer, buflen, stream);
a711dd4b 70 if (__builtin_expect (p == NULL, 0) && feof_unlocked (stream))
ba1ffaa1 71 {
50304ef0 72 funlockfile (stream);
ba1ffaa1 73 *result = NULL;
db873f32 74 __set_errno (ENOENT);
ba1ffaa1
UD
75 return errno;
76 }
a711dd4b 77 if (__builtin_expect (p == NULL, 0) || buffer[buflen - 1] != '\xff')
6591c335 78 {
50304ef0 79 funlockfile (stream);
6591c335 80 *result = NULL;
db873f32
UD
81 __set_errno (ERANGE);
82 return errno;
6591c335 83 }
267ca16a
UD
84
85 /* Skip leading blanks. */
86 while (isspace (*p))
87 ++p;
22d57dd3 88 } while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
267ca16a
UD
89 /* Parse the line. If it is invalid, loop to
90 get the next line of the file to parse. */
22d57dd3 91 || ! (parse_result = parse_line (p, resbuf,
d71b808a 92 (void *) buffer, buflen,
7ba4fcfc 93 &errno)));
22d57dd3 94
50304ef0
UD
95 funlockfile (stream);
96
a711dd4b 97 if (__builtin_expect (parse_result, 0) == -1)
22d57dd3
UD
98 {
99 /* The parser ran out of space. */
100 *result = NULL;
101 return errno;
102 }
267ca16a 103
ba1ffaa1
UD
104 *result = resbuf;
105 return 0;
267ca16a
UD
106}
107weak_alias (__fgetgrent_r, fgetgrent_r)