putspent.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <nss.h>
  16. #include <stdio.h>
  17. #include <shadow.h>
  18. #define flockfile(s) _IO_flockfile (s)
  19. #define funlockfile(s) _IO_funlockfile (s)
  20. #define _S(x) x ? x : ""
  21. /* Write an entry to the given stream.
  22. This must know the format of the password file. */
  23. int
  24. putspent (const struct spwd *p, FILE *stream)
  25. {
  26. int errors = 0;
  27. if (p->sp_namp == NULL || !__nss_valid_field (p->sp_namp)
  28. || !__nss_valid_field (p->sp_pwdp))
  29. {
  30. __set_errno (EINVAL);
  31. return -1;
  32. }
  33. flockfile (stream);
  34. if (fprintf (stream, "%s:%s:", p->sp_namp, _S (p->sp_pwdp)) < 0)
  35. ++errors;
  36. if ((p->sp_lstchg != (long int) -1
  37. && fprintf (stream, "%ld:", p->sp_lstchg) < 0)
  38. || (p->sp_lstchg == (long int) -1
  39. && putc_unlocked (':', stream) == EOF))
  40. ++errors;
  41. if ((p->sp_min != (long int) -1
  42. && fprintf (stream, "%ld:", p->sp_min) < 0)
  43. || (p->sp_min == (long int) -1
  44. && putc_unlocked (':', stream) == EOF))
  45. ++errors;
  46. if ((p->sp_max != (long int) -1
  47. && fprintf (stream, "%ld:", p->sp_max) < 0)
  48. || (p->sp_max == (long int) -1
  49. && putc_unlocked (':', stream) == EOF))
  50. ++errors;
  51. if ((p->sp_warn != (long int) -1
  52. && fprintf (stream, "%ld:", p->sp_warn) < 0)
  53. || (p->sp_warn == (long int) -1
  54. && putc_unlocked (':', stream) == EOF))
  55. ++errors;
  56. if ((p->sp_inact != (long int) -1
  57. && fprintf (stream, "%ld:", p->sp_inact) < 0)
  58. || (p->sp_inact == (long int) -1
  59. && putc_unlocked (':', stream) == EOF))
  60. ++errors;
  61. if ((p->sp_expire != (long int) -1
  62. && fprintf (stream, "%ld:", p->sp_expire) < 0)
  63. || (p->sp_expire == (long int) -1
  64. && putc_unlocked (':', stream) == EOF))
  65. ++errors;
  66. if (p->sp_flag != ~0ul
  67. && fprintf (stream, "%ld", p->sp_flag) < 0)
  68. ++errors;
  69. if (putc_unlocked ('\n', stream) == EOF)
  70. ++errors;
  71. funlockfile (stream);
  72. return errors ? -1 : 0;
  73. }