getXXent.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 1996-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 <libc-lock.h>
  16. #include <stdlib.h>
  17. #include <set-freeres.h>
  18. #include "nsswitch.h"
  19. /*******************************************************************\
  20. |* Here we assume several symbols to be defined: *|
  21. |* *|
  22. |* LOOKUP_TYPE - the return type of the function *|
  23. |* *|
  24. |* GETFUNC_NAME - name of the non-reentrant getXXXent function *|
  25. |* *|
  26. |* BUFLEN - size of static buffer *|
  27. |* *|
  28. |* Optionally the following vars can be defined: *|
  29. |* *|
  30. |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
  31. |* the global `h_errno' variable. *|
  32. |* *|
  33. \*******************************************************************/
  34. /* To make the real sources a bit prettier. */
  35. #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME)
  36. #define APPEND_R(name) APPEND_R1 (name)
  37. #define APPEND_R1(name) name##_r
  38. #define INTERNAL(name) INTERNAL1 (name)
  39. #define INTERNAL1(name) __##name
  40. #define APPEND_FREEMEM_NAME1(name) __libc_##name##_freemem_ptr
  41. #define APPEND_FREEMEM_NAME(name) APPEND_FREEMEM_NAME1(name)
  42. #define FREEMEM_NAME APPEND_FREEMEM_NAME (GETFUNC_NAME)
  43. /* Sometimes we need to store error codes in the `h_errno' variable. */
  44. #ifdef NEED_H_ERRNO
  45. # define H_ERRNO_PARM , int *h_errnop
  46. # define H_ERRNO_VAR &h_errno
  47. #else
  48. # define H_ERRNO_PARM
  49. # define H_ERRNO_VAR NULL
  50. #endif
  51. /* Prototype of the reentrant version. */
  52. extern int INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer,
  53. size_t buflen, LOOKUP_TYPE **result
  54. H_ERRNO_PARM) attribute_hidden;
  55. /* We need to protect the dynamic buffer handling. */
  56. __libc_lock_define_initialized (static, lock);
  57. /* This points to the static buffer used. */
  58. static char *buffer;
  59. weak_alias (buffer, FREEMEM_NAME)
  60. LOOKUP_TYPE *
  61. GETFUNC_NAME (void)
  62. {
  63. static size_t buffer_size;
  64. static union
  65. {
  66. LOOKUP_TYPE l;
  67. void *ptr;
  68. } resbuf;
  69. LOOKUP_TYPE *result;
  70. int save;
  71. /* Get lock. */
  72. __libc_lock_lock (lock);
  73. result = (LOOKUP_TYPE *)
  74. __nss_getent ((getent_r_function) INTERNAL (REENTRANT_GETNAME),
  75. &resbuf.ptr, &buffer, BUFLEN, &buffer_size,
  76. H_ERRNO_VAR);
  77. save = errno;
  78. __libc_lock_unlock (lock);
  79. __set_errno (save);
  80. return result;
  81. }
  82. nss_interface_function (GETFUNC_NAME)