__getspent_r.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* __getspent_r.c - Based on __getpwent_r.c
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include "config.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. if (buflen<PWD_BUFFER_SIZE)
  29. return ERANGE;
  30. /* We use the restart label to handle malformatted lines */
  31. restart:
  32. /* Read the shadow line into the buffer using a minimum of syscalls. */
  33. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  34. return EIO;
  35. endptr = strchr(line_buff, '\n');
  36. if (endptr != NULL)
  37. lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
  38. else {
  39. /* The line is too long - skip it. :-\ */
  40. do {
  41. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  42. return EIO;
  43. } while (!(endptr = strchr(line_buff, '\n')));
  44. lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR);
  45. goto restart;
  46. }
  47. if (__sgetspent_r(line_buff, spwd, line_buff, buflen) != 0)
  48. goto restart;
  49. return 0;
  50. }