pwd_grp_internal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <malloc.h>
  28. #include <assert.h>
  29. #include <ctype.h>
  30. #include <pwd.h>
  31. #include <grp.h>
  32. #include <paths.h>
  33. #ifdef __UCLIBC_HAS_SHADOW__
  34. #include <shadow.h>
  35. #endif
  36. /**********************************************************************/
  37. /* Prototypes for internal functions. */
  38. #if !defined(GETXXKEY_R_FUNC) && !defined(GETXXKEY_FUNC)
  39. #error GETXXKEY_R_FUNC/GETXXKEY_FUNC are not defined!
  40. #endif
  41. /**********************************************************************/
  42. #ifdef GETXXKEY_R_FUNC
  43. libc_hidden_proto(GETXXKEY_R_FUNC)
  44. int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
  45. GETXXKEY_R_ENTTYPE *__restrict resultbuf,
  46. char *__restrict buffer, size_t buflen,
  47. GETXXKEY_R_ENTTYPE **__restrict result)
  48. {
  49. FILE *stream;
  50. int rv;
  51. *result = NULL;
  52. if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
  53. rv = errno;
  54. } else {
  55. __STDIO_SET_USER_LOCKING(stream);
  56. do {
  57. if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
  58. buffer, buflen, stream))
  59. ) {
  60. if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
  61. *result = resultbuf;
  62. break;
  63. }
  64. } else {
  65. if (rv == ENOENT) { /* end-of-file encountered. */
  66. rv = 0;
  67. }
  68. break;
  69. }
  70. } while (1);
  71. fclose(stream);
  72. }
  73. return rv;
  74. }
  75. libc_hidden_def(GETXXKEY_R_FUNC)
  76. #endif /* GETXXKEY_R_FUNC */
  77. /**********************************************************************/
  78. #ifdef GETXXKEY_FUNC
  79. #define REENTRANT_NAME APPEND_R(GETXXKEY_FUNC)
  80. #define APPEND_R(name) APPEND_R1(name)
  81. #define APPEND_R1(name) name##_r
  82. GETXXKEY_ENTTYPE *GETXXKEY_FUNC(GETXXKEY_ADD_PARAMS)
  83. {
  84. static char *buffer = NULL;
  85. static GETXXKEY_ENTTYPE resultbuf;
  86. GETXXKEY_ENTTYPE *result;
  87. if (buffer == NULL)
  88. buffer = (char *)__uc_malloc(GETXXKEY_BUFLEN);
  89. # ifdef GETXXKEY_ADD_VARIABLES
  90. REENTRANT_NAME(GETXXKEY_ADD_VARIABLES, &resultbuf, buffer, GETXXKEY_BUFLEN, &result);
  91. # else
  92. REENTRANT_NAME(&resultbuf, buffer, GETXXKEY_BUFLEN, &result);
  93. # endif
  94. return result;
  95. }
  96. #ifdef GETXXKEY_FUNC_HIDDEN
  97. libc_hidden_def(GETXXKEY_FUNC)
  98. #endif
  99. #undef REENTRANT_NAME
  100. #undef APPEND_R
  101. #undef APPEND_R1
  102. #endif /* GETXXKEY_FUNC */
  103. /**********************************************************************/
  104. #undef GETXXKEY_FUNC
  105. #undef GETXXKEY_ENTTYPE
  106. #undef GETXXKEY_BUFLEN
  107. #undef GETXXKEY_FUNC_HIDDEN
  108. #undef GETXXKEY_ADD_PARAMS
  109. #undef GETXXKEY_ADD_VARIABLES
  110. #undef GETXXKEY_R_FUNC
  111. #undef GETXXKEY_R_PARSER
  112. #undef GETXXKEY_R_ENTTYPE
  113. #undef GETXXKEY_R_TEST
  114. #undef DO_GETXXKEY_R_KEYTYPE
  115. #undef DO_GETXXKEY_R_PATHNAME