pwd_grp_internal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (C) 2003 Manuel Novoa III <mjn3@uclibc.org>
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /* Nov 6, 2003 Initial version.
  8. *
  9. * NOTE: This implementation is quite strict about requiring all
  10. * field seperators. It also does not allow leading whitespace
  11. * except when processing the numeric fields. glibc is more
  12. * lenient. See the various glibc difference comments below.
  13. *
  14. * TODO:
  15. * Move to dynamic allocation of (currently statically allocated)
  16. * buffers; especially for the group-related functions since
  17. * large group member lists will cause error returns.
  18. *
  19. */
  20. #include <features.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <paths.h>
  32. #ifdef __UCLIBC_HAS_SHADOW__
  33. #include <shadow.h>
  34. #endif
  35. /**********************************************************************/
  36. /* Prototypes for internal functions. */
  37. #if !defined(GETXXKEY_R_FUNC) && !defined(GETXXKEY_FUNC)
  38. #error GETXXKEY_R_FUNC/GETXXKEY_FUNC are not defined!
  39. #endif
  40. /**********************************************************************/
  41. #ifdef GETXXKEY_R_FUNC
  42. libc_hidden_proto(GETXXKEY_R_FUNC)
  43. int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
  44. GETXXKEY_R_ENTTYPE *__restrict resultbuf,
  45. char *__restrict buffer, size_t buflen,
  46. GETXXKEY_R_ENTTYPE **__restrict result)
  47. {
  48. FILE *stream;
  49. int rv;
  50. *result = NULL;
  51. if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
  52. rv = errno;
  53. } else {
  54. __STDIO_SET_USER_LOCKING(stream);
  55. do {
  56. if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
  57. buffer, buflen, stream))
  58. ) {
  59. if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
  60. *result = resultbuf;
  61. break;
  62. }
  63. } else {
  64. if (rv == ENOENT) { /* end-of-file encountered. */
  65. rv = 0;
  66. }
  67. break;
  68. }
  69. } while (1);
  70. fclose(stream);
  71. }
  72. return rv;
  73. }
  74. libc_hidden_def(GETXXKEY_R_FUNC)
  75. #endif /* GETXXKEY_R_FUNC */
  76. /**********************************************************************/
  77. #ifdef GETXXKEY_FUNC
  78. #define REENTRANT_NAME APPEND_R(GETXXKEY_FUNC)
  79. #define APPEND_R(name) APPEND_R1(name)
  80. #define APPEND_R1(name) name##_r
  81. GETXXKEY_ENTTYPE *GETXXKEY_FUNC(GETXXKEY_ADD_PARAMS)
  82. {
  83. static char buffer[GETXXKEY_BUFLEN];
  84. static GETXXKEY_ENTTYPE resultbuf;
  85. GETXXKEY_ENTTYPE *result;
  86. # ifdef GETXXKEY_ADD_VARIABLES
  87. REENTRANT_NAME(GETXXKEY_ADD_VARIABLES, &resultbuf, buffer, sizeof(buffer), &result);
  88. # else
  89. REENTRANT_NAME(&resultbuf, buffer, sizeof(buffer), &result);
  90. # endif
  91. return result;
  92. }
  93. #ifdef GETXXKEY_FUNC_HIDDEN
  94. libc_hidden_def(GETXXKEY_FUNC)
  95. #endif
  96. #undef REENTRANT_NAME
  97. #undef APPEND_R
  98. #undef APPEND_R1
  99. #endif /* GETXXKEY_FUNC */
  100. /**********************************************************************/
  101. #undef GETXXKEY_FUNC
  102. #undef GETXXKEY_ENTTYPE
  103. #undef GETXXKEY_BUFLEN
  104. #undef GETXXKEY_FUNC_HIDDEN
  105. #undef GETXXKEY_ADD_PARAMS
  106. #undef GETXXKEY_ADD_VARIABLES
  107. #undef GETXXKEY_R_FUNC
  108. #undef GETXXKEY_R_PARSER
  109. #undef GETXXKEY_R_ENTTYPE
  110. #undef GETXXKEY_R_TEST
  111. #undef DO_GETXXKEY_R_KEYTYPE
  112. #undef DO_GETXXKEY_R_PATHNAME