putsgent.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2009-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 <stdbool.h>
  16. #include <stdio.h>
  17. #include <gshadow.h>
  18. #include <nss.h>
  19. #define _S(x) x ? x : ""
  20. /* Write an entry to the given stream.
  21. This must know the format of the group file. */
  22. int
  23. putsgent (const struct sgrp *g, FILE *stream)
  24. {
  25. int errors = 0;
  26. if (g->sg_namp == NULL || !__nss_valid_field (g->sg_namp)
  27. || !__nss_valid_field (g->sg_passwd)
  28. || !__nss_valid_list_field (g->sg_adm)
  29. || !__nss_valid_list_field (g->sg_mem))
  30. {
  31. __set_errno (EINVAL);
  32. return -1;
  33. }
  34. _IO_flockfile (stream);
  35. if (fprintf (stream, "%s:%s:", g->sg_namp, _S (g->sg_passwd)) < 0)
  36. ++errors;
  37. bool first = true;
  38. char **sp = g->sg_adm;
  39. if (sp != NULL)
  40. while (*sp != NULL)
  41. {
  42. if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
  43. {
  44. ++errors;
  45. break;
  46. }
  47. first = false;
  48. }
  49. if (putc_unlocked (':', stream) == EOF)
  50. ++errors;
  51. first = true;
  52. sp = g->sg_mem;
  53. if (sp != NULL)
  54. while (*sp != NULL)
  55. {
  56. if (fprintf (stream, "%s%s", first ? "" : ",", *sp++) < 0)
  57. {
  58. ++errors;
  59. break;
  60. }
  61. first = false;
  62. }
  63. if (putc_unlocked ('\n', stream) == EOF)
  64. ++errors;
  65. _IO_funlockfile (stream);
  66. return errors ? -1 : 0;
  67. }