pwent.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <features.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <paths.h>
  28. #include "config.h"
  29. #ifdef __UCLIBC_HAS_THREADS__
  30. #include <pthread.h>
  31. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  32. # define LOCK pthread_mutex_lock(&mylock)
  33. # define UNLOCK pthread_mutex_unlock(&mylock);
  34. #else
  35. # define LOCK
  36. # define UNLOCK
  37. #endif
  38. /*
  39. * setpwent(), endpwent(), and getpwent() are included in the same object
  40. * file, since one cannot be used without the other two, so it makes sense to
  41. * link them all in together.
  42. */
  43. /* file descriptor for the password file currently open */
  44. static int pw_fd = -1;
  45. void setpwent(void)
  46. {
  47. LOCK;
  48. if (pw_fd != -1)
  49. close(pw_fd);
  50. pw_fd = open(_PATH_PASSWD, O_RDONLY);
  51. UNLOCK;
  52. }
  53. void endpwent(void)
  54. {
  55. LOCK;
  56. if (pw_fd != -1)
  57. close(pw_fd);
  58. pw_fd = -1;
  59. UNLOCK;
  60. }
  61. int getpwent_r (struct passwd *password, char *buff,
  62. size_t buflen, struct passwd **result)
  63. {
  64. int ret=EINVAL;
  65. LOCK;
  66. *result = NULL;
  67. if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
  68. UNLOCK;
  69. *result = password;
  70. return 0;
  71. }
  72. UNLOCK;
  73. __set_errno(ret);
  74. return ret;
  75. }
  76. struct passwd *getpwent(void)
  77. {
  78. int ret;
  79. static char line_buff[PWD_BUFFER_SIZE];
  80. static struct passwd pwd;
  81. struct passwd *result;
  82. if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), &result)) == 0) {
  83. return &pwd;
  84. }
  85. __set_errno(ret);
  86. return NULL;
  87. }