pwd_grp_internal.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2003 Manuel Novoa III
  3. *
  4. * Licensed under LGPL v2.1, see the file COPYING.LIB in this tarball for details.
  5. */
  6. /* Nov 6, 2003 Initial version.
  7. *
  8. * NOTE: This implementation is quite strict about requiring all
  9. * field seperators. It also does not allow leading whitespace
  10. * except when processing the numeric fields. glibc is more
  11. * lenient. See the various glibc difference comments below.
  12. *
  13. * TODO:
  14. * Move to dynamic allocation of (currently staticly allocated)
  15. * buffers; especially for the group-related functions since
  16. * large group member lists will cause error returns.
  17. *
  18. */
  19. #define _GNU_SOURCE
  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 __HAS_SHADOW__
  33. #include <shadow.h>
  34. #endif
  35. #ifdef __UCLIBC_HAS_THREADS__
  36. #include <pthread.h>
  37. #endif
  38. /**********************************************************************/
  39. /* Sizes for staticly 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. int GETXXKEY_R_FUNC(DO_GETXXKEY_R_KEYTYPE key,
  52. GETXXKEY_R_ENTTYPE *__restrict resultbuf,
  53. char *__restrict buffer, size_t buflen,
  54. GETXXKEY_R_ENTTYPE **__restrict result)
  55. {
  56. FILE *stream;
  57. int rv;
  58. *result = NULL;
  59. if (!(stream = fopen(DO_GETXXKEY_R_PATHNAME, "r"))) {
  60. rv = errno;
  61. } else {
  62. __STDIO_SET_USER_LOCKING(stream);
  63. do {
  64. if (!(rv = __pgsreader(GETXXKEY_R_PARSER, resultbuf,
  65. buffer, buflen, stream))
  66. ) {
  67. if (GETXXKEY_R_TEST(resultbuf)) { /* Found key? */
  68. *result = resultbuf;
  69. break;
  70. }
  71. } else {
  72. if (rv == ENOENT) { /* end-of-file encountered. */
  73. rv = 0;
  74. }
  75. break;
  76. }
  77. } while (1);
  78. fclose(stream);
  79. }
  80. return rv;
  81. }
  82. #endif
  83. /**********************************************************************/
  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