fgetgrent.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 1991-2019 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. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <grp.h>
  16. #include <libc-lock.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. /* We need to protect the dynamic buffer handling. */
  20. __libc_lock_define_initialized (static, lock);
  21. libc_freeres_ptr (static char *buffer);
  22. /* Read one entry from the given stream. */
  23. struct group *
  24. fgetgrent (FILE *stream)
  25. {
  26. static size_t buffer_size;
  27. static struct group resbuf;
  28. fpos_t pos;
  29. struct group *result;
  30. int save;
  31. if (__builtin_expect (fgetpos (stream, &pos), 0) != 0)
  32. return NULL;
  33. /* Get lock. */
  34. __libc_lock_lock (lock);
  35. /* Allocate buffer if not yet available. */
  36. if (buffer == NULL)
  37. {
  38. buffer_size = NSS_BUFLEN_GROUP;
  39. buffer = malloc (buffer_size);
  40. }
  41. while (buffer != NULL
  42. && (__fgetgrent_r (stream, &resbuf, buffer, buffer_size, &result)
  43. == ERANGE))
  44. {
  45. char *new_buf;
  46. buffer_size += NSS_BUFLEN_GROUP;
  47. new_buf = realloc (buffer, buffer_size);
  48. if (__glibc_unlikely (new_buf == NULL))
  49. {
  50. /* We are out of memory. Free the current buffer so that the
  51. process gets a chance for a normal termination. */
  52. save = errno;
  53. free (buffer);
  54. __set_errno (save);
  55. }
  56. buffer = new_buf;
  57. /* Reset the stream. */
  58. if (fsetpos (stream, &pos) != 0)
  59. buffer = NULL;
  60. }
  61. if (buffer == NULL)
  62. result = NULL;
  63. /* Release lock. Preserve error value. */
  64. save = errno;
  65. __libc_lock_unlock (lock);
  66. __set_errno (save);
  67. return result;
  68. }