spent.c 2.2 KB

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