pwent.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * pwent.c - This file is part of the libc-8086/pwd package for ELKS,
  4. * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
  5. * Copyright (C) 2001-2003 Erik Andersen <andersee@debian.org>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free
  19. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. /*
  23. * setpwent(), endpwent(), and getpwent() are included in the same object
  24. * file, since one cannot be used without the other two, so it makes sense to
  25. * link them all in together.
  26. */
  27. #define _GNU_SOURCE
  28. #include <features.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <paths.h>
  34. #include "config.h"
  35. #ifdef __UCLIBC_HAS_THREADS__
  36. #include <pthread.h>
  37. static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  38. # define LOCK pthread_mutex_lock(&mylock)
  39. # define UNLOCK pthread_mutex_unlock(&mylock);
  40. #else
  41. # define LOCK
  42. # define UNLOCK
  43. #endif
  44. /* file descriptor for the password file currently open */
  45. static int pw_fd = -9;
  46. void setpwent(void)
  47. {
  48. LOCK;
  49. if (pw_fd > -1)
  50. close(pw_fd);
  51. pw_fd = open(_PATH_PASSWD, O_RDONLY);
  52. UNLOCK;
  53. }
  54. void endpwent(void)
  55. {
  56. LOCK;
  57. if (pw_fd > -1)
  58. close(pw_fd);
  59. pw_fd = -9;
  60. UNLOCK;
  61. }
  62. int getpwent_r (struct passwd *password, char *buff,
  63. size_t buflen, struct passwd **result)
  64. {
  65. int ret=EINVAL;
  66. *result = NULL;
  67. LOCK;
  68. /* Open /etc/passwd if not yet opened */
  69. if (pw_fd == -9) {
  70. setpwent();
  71. }
  72. if (pw_fd == -1) {
  73. UNLOCK;
  74. return -1;
  75. }
  76. ret=__getpwent_r(password, buff, buflen, pw_fd);
  77. if (ret == 0) {
  78. UNLOCK;
  79. *result = password;
  80. return 0;
  81. }
  82. UNLOCK;
  83. __set_errno(ret);
  84. return ret;
  85. }
  86. struct passwd *getpwent(void)
  87. {
  88. int ret;
  89. struct passwd *result;
  90. static struct passwd pwd;
  91. static char line_buff[PWD_BUFFER_SIZE];
  92. ret = getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
  93. if (ret == 0) {
  94. return &pwd;
  95. }
  96. __set_errno(ret);
  97. return NULL;
  98. }