Blame shadow/putspent.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <nss.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <shadow.h>
Packit 6c4009
Packit 6c4009
#define flockfile(s) _IO_flockfile (s)
Packit 6c4009
#define funlockfile(s) _IO_funlockfile (s)
Packit 6c4009
Packit 6c4009
#define _S(x)	x ? x : ""
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Write an entry to the given stream.
Packit 6c4009
   This must know the format of the password file.  */
Packit 6c4009
int
Packit 6c4009
putspent (const struct spwd *p, FILE *stream)
Packit 6c4009
{
Packit 6c4009
  int errors = 0;
Packit 6c4009
Packit 6c4009
  if (p->sp_namp == NULL || !__nss_valid_field (p->sp_namp)
Packit 6c4009
      || !__nss_valid_field (p->sp_pwdp))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  flockfile (stream);
Packit 6c4009
Packit 6c4009
  if (fprintf (stream, "%s:%s:", p->sp_namp, _S (p->sp_pwdp)) < 0)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_lstchg != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_lstchg) < 0)
Packit 6c4009
      || (p->sp_lstchg == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_min != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_min) < 0)
Packit 6c4009
      || (p->sp_min == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_max != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_max) < 0)
Packit 6c4009
      || (p->sp_max == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_warn != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_warn) < 0)
Packit 6c4009
      || (p->sp_warn == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_inact != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_inact) < 0)
Packit 6c4009
      || (p->sp_inact == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if ((p->sp_expire != (long int) -1
Packit 6c4009
       && fprintf (stream, "%ld:", p->sp_expire) < 0)
Packit 6c4009
      || (p->sp_expire == (long int) -1
Packit 6c4009
	  && putc_unlocked (':', stream) == EOF))
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if (p->sp_flag != ~0ul
Packit 6c4009
      && fprintf (stream, "%ld", p->sp_flag) < 0)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  if (putc_unlocked ('\n', stream) == EOF)
Packit 6c4009
    ++errors;
Packit 6c4009
Packit 6c4009
  funlockfile (stream);
Packit 6c4009
Packit 6c4009
  return errors ? -1 : 0;
Packit 6c4009
}