]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1991-2021 Free Software Foundation, Inc. |
7ce241a0 UD |
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 | |
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. | |
7ce241a0 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
7ce241a0 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
7ce241a0 UD |
17 | |
18 | #include <errno.h> | |
676599b3 | 19 | #include <nss.h> |
7ce241a0 | 20 | #include <stdio.h> |
676599b3 | 21 | #include <string.h> |
7ce241a0 UD |
22 | #include <grp.h> |
23 | ||
3ce1f295 UD |
24 | #define flockfile(s) _IO_flockfile (s) |
25 | #define funlockfile(s) _IO_funlockfile (s) | |
7ce241a0 UD |
26 | |
27 | #define _S(x) x ? x : "" | |
28 | ||
29 | /* Write an entry to the given stream. | |
30 | This must know the format of the group file. */ | |
31 | int | |
676599b3 | 32 | putgrent (const struct group *gr, FILE *stream) |
7ce241a0 UD |
33 | { |
34 | int retval; | |
35 | ||
676599b3 FW |
36 | if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL) |
37 | || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name) | |
38 | || !__nss_valid_field (gr->gr_passwd) | |
39 | || !__nss_valid_list_field (gr->gr_mem)) | |
7ce241a0 UD |
40 | { |
41 | __set_errno (EINVAL); | |
42 | return -1; | |
43 | } | |
44 | ||
45 | flockfile (stream); | |
46 | ||
537e7234 UD |
47 | if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-') |
48 | retval = fprintf (stream, "%s:%s::", | |
49 | gr->gr_name, _S (gr->gr_passwd)); | |
50 | else | |
51 | retval = fprintf (stream, "%s:%s:%lu:", | |
52 | gr->gr_name, _S (gr->gr_passwd), | |
53 | (unsigned long int) gr->gr_gid); | |
a711dd4b UD |
54 | if (__builtin_expect (retval, 0) < 0) |
55 | { | |
56 | funlockfile (stream); | |
57 | return -1; | |
58 | } | |
7ce241a0 UD |
59 | |
60 | if (gr->gr_mem != NULL) | |
61 | { | |
676599b3 | 62 | for (size_t i = 0; gr->gr_mem[i] != NULL; i++) |
8ba3c7d9 | 63 | if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0) |
7ce241a0 UD |
64 | { |
65 | /* What else can we do? */ | |
66 | funlockfile (stream); | |
67 | return -1; | |
68 | } | |
69 | } | |
70 | ||
71 | retval = fputc_unlocked ('\n', stream); | |
72 | ||
73 | funlockfile (stream); | |
74 | ||
75 | return retval < 0 ? -1 : 0; | |
76 | } |