pwd_grp_internal.c 2.6 KB

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