__getspent_r.c 1.7 KB

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