spent.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include "config.h"
  24. /*
  25. * setspent(), endspent(), and getspent() are included in the same object
  26. * file, since one cannot be used without the other two, so it makes sense to
  27. * link them all in together.
  28. */
  29. /* file descriptor for the password file currently open */
  30. static int spwd_fd = -1;
  31. void setspent(void)
  32. {
  33. if (spwd_fd != -1)
  34. close(spwd_fd);
  35. spwd_fd = open(_PATH_SHADOW, O_RDONLY);
  36. }
  37. void endspent(void)
  38. {
  39. if (spwd_fd != -1)
  40. close(spwd_fd);
  41. spwd_fd = -1;
  42. }
  43. int getspent_r (struct spwd *spwd, char *buff,
  44. size_t buflen, struct spwd **crap)
  45. {
  46. if (spwd_fd != -1 && __getspent_r(spwd, buff, buflen, spwd_fd) != -1) {
  47. return 0;
  48. }
  49. return -1;
  50. }
  51. struct spwd *getspent(void)
  52. {
  53. static char line_buff[PWD_BUFFER_SIZE];
  54. static struct spwd spwd;
  55. if (getspent_r(&spwd, line_buff, sizeof(line_buff), NULL) != -1) {
  56. return &spwd;
  57. }
  58. return NULL;
  59. }