pwd_grp_internal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifdef __UCLIBC_HAS_THREADS__
  36. #include <pthread.h>
  37. #endif
  38. /**********************************************************************/
  39. /* Sizes for statically allocated buffers. */
  40. /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and
  41. * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */
  42. #define PWD_BUFFER_SIZE 256
  43. #define GRP_BUFFER_SIZE 256
  44. /**********************************************************************/
  45. /* Prototypes for internal functions. */
  46. #ifndef GETXXKEY_R_FUNC
  47. #error GETXXKEY_R_FUNC is not defined!
  48. #endif
  49. /**********************************************************************/
  50. #ifdef GETXXKEY_R_FUNC
  51. libc_hidden_proto(GETXXKEY_R_FUNC)
  52. int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
  53. GETXXKEY_R_ENTTYPE *__restrict resultbuf,
  54. char *__restrict buffer, size_t buflen,
  55. GETXXKEY_R_ENTTYPE **__restrict result)
  56. {
  57. FILE *stream;
  58. int rv;
  59. *result = NULL;
  60. if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
  61. rv = errno;
  62. } else {
  63. __STDIO_SET_USER_LOCKING(stream);
  64. do {
  65. if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
  66. buffer, buflen, stream))
  67. ) {
  68. if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
  69. *result = resultbuf;
  70. break;
  71. }
  72. } else {
  73. if (rv == ENOENT) { /* end-of-file encountered. */
  74. rv = 0;
  75. }
  76. break;
  77. }
  78. } while (1);
  79. fclose(stream);
  80. }
  81. return rv;
  82. }
  83. libc_hidden_def(GETXXKEY_R_FUNC)
  84. #endif
  85. /**********************************************************************/
  86. #undef GETXXKEY_R_FUNC_HIDDEN
  87. #undef GETXXKEY_R_FUNC
  88. #undef GETXXKEY_R_PARSER
  89. #undef GETXXKEY_R_ENTTYPE
  90. #undef GETXXKEY_R_TEST
  91. #undef DO_GETXXKEY_R_KEYTYPE
  92. #undef DO_GETXXKEY_R_PATHNAME