__getspent_r.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * __getspent_r.c - Based on __getpwent_r.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 <stdlib.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <shadow.h>
  24. int __getspent_r(struct spwd * spwd, char * line_buff, size_t buflen, int spwd_fd)
  25. {
  26. char *endptr;
  27. int line_len;
  28. /* We use the restart label to handle malformatted lines */
  29. restart:
  30. /* Read the shadow line into the static buffer using a minimal of
  31. syscalls. */
  32. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  33. return -1;
  34. endptr = strchr(line_buff, '\n');
  35. if (endptr != NULL)
  36. lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)),
  37. SEEK_CUR);
  38. else { /* The line is too long - skip it. :-\ */
  39. do {
  40. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  41. return -1;
  42. } while (!(endptr = strchr(line_buff, '\n')));
  43. lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1,
  44. SEEK_CUR);
  45. goto restart;
  46. }
  47. if (__sgetspent_r(line_buff, spwd, line_buff, buflen) < 0)
  48. goto restart;
  49. return 0;
  50. }