]> git.ipfire.org Git - thirdparty/glibc.git/blame - shadow/putspent.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / shadow / putspent.c
CommitLineData
04277e02 1/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
19361cb7
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.
19361cb7
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.
19361cb7 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/>. */
267ca16a 17
676599b3
FW
18#include <errno.h>
19#include <nss.h>
267ca16a
UD
20#include <stdio.h>
21#include <shadow.h>
22
3ce1f295
UD
23#define flockfile(s) _IO_flockfile (s)
24#define funlockfile(s) _IO_funlockfile (s)
7ce241a0
UD
25
26#define _S(x) x ? x : ""
27
267ca16a
UD
28
29/* Write an entry to the given stream.
30 This must know the format of the password file. */
31int
32putspent (const struct spwd *p, FILE *stream)
33{
34 int errors = 0;
35
676599b3
FW
36 if (p->sp_namp == NULL || !__nss_valid_field (p->sp_namp)
37 || !__nss_valid_field (p->sp_pwdp))
38 {
39 __set_errno (EINVAL);
40 return -1;
41 }
42
7ce241a0
UD
43 flockfile (stream);
44
45 if (fprintf (stream, "%s:%s:", p->sp_namp, _S (p->sp_pwdp)) < 0)
267ca16a
UD
46 ++errors;
47
c4029823 48 if ((p->sp_lstchg != (long int) -1
569c558c 49 && fprintf (stream, "%ld:", p->sp_lstchg) < 0)
c4029823 50 || (p->sp_lstchg == (long int) -1
7ce241a0 51 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
52 ++errors;
53
c4029823 54 if ((p->sp_min != (long int) -1
569c558c 55 && fprintf (stream, "%ld:", p->sp_min) < 0)
c4029823 56 || (p->sp_min == (long int) -1
7ce241a0 57 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
58 ++errors;
59
c4029823 60 if ((p->sp_max != (long int) -1
569c558c 61 && fprintf (stream, "%ld:", p->sp_max) < 0)
c4029823 62 || (p->sp_max == (long int) -1
7ce241a0 63 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
64 ++errors;
65
c4029823 66 if ((p->sp_warn != (long int) -1
569c558c 67 && fprintf (stream, "%ld:", p->sp_warn) < 0)
c4029823 68 || (p->sp_warn == (long int) -1
7ce241a0 69 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
70 ++errors;
71
c4029823 72 if ((p->sp_inact != (long int) -1
569c558c 73 && fprintf (stream, "%ld:", p->sp_inact) < 0)
c4029823 74 || (p->sp_inact == (long int) -1
7ce241a0 75 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
76 ++errors;
77
c4029823 78 if ((p->sp_expire != (long int) -1
569c558c 79 && fprintf (stream, "%ld:", p->sp_expire) < 0)
c4029823 80 || (p->sp_expire == (long int) -1
7ce241a0 81 && putc_unlocked (':', stream) == EOF))
267ca16a
UD
82 ++errors;
83
11336c16
UD
84 if (p->sp_flag != ~0ul
85 && fprintf (stream, "%ld", p->sp_flag) < 0)
267ca16a
UD
86 ++errors;
87
7ce241a0 88 if (putc_unlocked ('\n', stream) == EOF)
267ca16a
UD
89 ++errors;
90
7ce241a0
UD
91 funlockfile (stream);
92
267ca16a
UD
93 return errors ? -1 : 0;
94}