]> git.ipfire.org Git - thirdparty/glibc.git/blame - grp/putgrent.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / grp / putgrent.c
CommitLineData
568035b7 1/* Copyright (C) 1991-2013 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
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
7ce241a0
UD
17
18#include <errno.h>
19#include <stdio.h>
20#include <grp.h>
21
3ce1f295
UD
22#define flockfile(s) _IO_flockfile (s)
23#define funlockfile(s) _IO_funlockfile (s)
7ce241a0
UD
24
25#define _S(x) x ? x : ""
26
27/* Write an entry to the given stream.
28 This must know the format of the group file. */
29int
30putgrent (gr, stream)
31 const struct group *gr;
32 FILE *stream;
33{
34 int retval;
35
a711dd4b 36 if (__builtin_expect (gr == NULL, 0) || __builtin_expect (stream == NULL, 0))
7ce241a0
UD
37 {
38 __set_errno (EINVAL);
39 return -1;
40 }
41
42 flockfile (stream);
43
537e7234
UD
44 if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-')
45 retval = fprintf (stream, "%s:%s::",
46 gr->gr_name, _S (gr->gr_passwd));
47 else
48 retval = fprintf (stream, "%s:%s:%lu:",
49 gr->gr_name, _S (gr->gr_passwd),
50 (unsigned long int) gr->gr_gid);
a711dd4b
UD
51 if (__builtin_expect (retval, 0) < 0)
52 {
53 funlockfile (stream);
54 return -1;
55 }
7ce241a0
UD
56
57 if (gr->gr_mem != NULL)
58 {
59 int i;
60
61 for (i = 0 ; gr->gr_mem[i] != NULL; i++)
8ba3c7d9 62 if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0)
7ce241a0
UD
63 {
64 /* What else can we do? */
65 funlockfile (stream);
66 return -1;
67 }
68 }
69
70 retval = fputc_unlocked ('\n', stream);
71
72 funlockfile (stream);
73
74 return retval < 0 ? -1 : 0;
75}