putgrent.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * putgrent.c
  4. * Copyright (C) 2003 Erik Andersen <andersee@debian.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include "config.h"
  24. /* Write the given entry onto the given stream. */
  25. int putgrent(const struct group *__restrict grp,
  26. FILE *__restrict f)
  27. {
  28. if (grp == NULL || f == NULL) {
  29. __set_errno(EINVAL);
  30. return -1;
  31. }
  32. if (fprintf(f, "%s:%s:%u:", grp->gr_name,
  33. grp->gr_passwd, grp->gr_gid) < 0)
  34. {
  35. return -1;
  36. }
  37. if (grp->gr_mem) {
  38. int i;
  39. char **gr_mem = grp->gr_mem;
  40. while(*gr_mem) {
  41. if (fprintf(f, (i++)? ",%s" : "%s", *gr_mem) < 0) {
  42. return -1;
  43. }
  44. gr_mem++;
  45. }
  46. }
  47. if (fputc('\n', f) < 0) {
  48. return -1;
  49. }
  50. return 0;
  51. }