Back to index

glibc  2.9
putspent.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1996, 1997, 1998 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdio.h>
00020 #include <shadow.h>
00021 
00022 #ifdef USE_IN_LIBIO
00023 # define flockfile(s) _IO_flockfile (s)
00024 # define funlockfile(s) _IO_funlockfile (s)
00025 #endif
00026 
00027 #define _S(x) x ? x : ""
00028 
00029 
00030 /* Write an entry to the given stream.
00031    This must know the format of the password file.  */
00032 int
00033 putspent (const struct spwd *p, FILE *stream)
00034 {
00035   int errors = 0;
00036 
00037   flockfile (stream);
00038 
00039   if (fprintf (stream, "%s:%s:", p->sp_namp, _S (p->sp_pwdp)) < 0)
00040     ++errors;
00041 
00042   if ((p->sp_lstchg != (long int) -1
00043        && fprintf (stream, "%ld:", p->sp_lstchg) < 0)
00044       || (p->sp_lstchg == (long int) -1
00045          && putc_unlocked (':', stream) == EOF))
00046     ++errors;
00047 
00048   if ((p->sp_min != (long int) -1
00049        && fprintf (stream, "%ld:", p->sp_min) < 0)
00050       || (p->sp_min == (long int) -1
00051          && putc_unlocked (':', stream) == EOF))
00052     ++errors;
00053 
00054   if ((p->sp_max != (long int) -1
00055        && fprintf (stream, "%ld:", p->sp_max) < 0)
00056       || (p->sp_max == (long int) -1
00057          && putc_unlocked (':', stream) == EOF))
00058     ++errors;
00059 
00060   if ((p->sp_warn != (long int) -1
00061        && fprintf (stream, "%ld:", p->sp_warn) < 0)
00062       || (p->sp_warn == (long int) -1
00063          && putc_unlocked (':', stream) == EOF))
00064     ++errors;
00065 
00066   if ((p->sp_inact != (long int) -1
00067        && fprintf (stream, "%ld:", p->sp_inact) < 0)
00068       || (p->sp_inact == (long int) -1
00069          && putc_unlocked (':', stream) == EOF))
00070     ++errors;
00071 
00072   if ((p->sp_expire != (long int) -1
00073        && fprintf (stream, "%ld:", p->sp_expire) < 0)
00074       || (p->sp_expire == (long int) -1
00075          && putc_unlocked (':', stream) == EOF))
00076     ++errors;
00077 
00078   if (p->sp_flag != ~0ul
00079       && fprintf (stream, "%ld", p->sp_flag) < 0)
00080     ++errors;
00081 
00082   if (putc_unlocked ('\n', stream) == EOF)
00083     ++errors;
00084 
00085   funlockfile (stream);
00086 
00087   return errors ? -1 : 0;
00088 }