spent.c 2.2 KB

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