spent.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 **crap)
  59. {
  60. LOCK;
  61. if (spwd_fd != -1 && __getspent_r(spwd, buff, buflen, spwd_fd) != -1) {
  62. UNLOCK;
  63. return 0;
  64. }
  65. UNLOCK;
  66. return -1;
  67. }
  68. struct spwd *getspent(void)
  69. {
  70. static char line_buff[PWD_BUFFER_SIZE];
  71. static struct spwd spwd;
  72. LOCK;
  73. if (getspent_r(&spwd, line_buff, sizeof(line_buff), NULL) != -1) {
  74. UNLOCK;
  75. return &spwd;
  76. }
  77. UNLOCK;
  78. return NULL;
  79. }