getnssent.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 2000-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 <stdlib.h>
  16. #include <netdb.h>
  17. #include "nsswitch.h"
  18. void *
  19. __nss_getent (getent_r_function func, void **resbuf, char **buffer,
  20. size_t buflen, size_t *buffer_size, int *h_errnop)
  21. {
  22. void *result;
  23. if (*buffer == NULL)
  24. {
  25. *buffer_size = buflen;
  26. *buffer = malloc (*buffer_size);
  27. }
  28. while (*buffer != NULL
  29. && func (resbuf, *buffer, *buffer_size, &result, h_errnop) == ERANGE
  30. && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL))
  31. {
  32. char *new_buf;
  33. *buffer_size *= 2;
  34. new_buf = realloc (*buffer, *buffer_size);
  35. if (new_buf == NULL)
  36. {
  37. /* We are out of memory. Free the current buffer so that the
  38. process gets a chance for a normal termination. */
  39. int save = errno;
  40. free (*buffer);
  41. __set_errno (save);
  42. }
  43. *buffer = new_buf;
  44. }
  45. if (*buffer == NULL)
  46. result = NULL;
  47. return result;
  48. }