__getspent_r.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 buffer using a minimum of syscalls. */
  30. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  31. return -1;
  32. endptr = strchr(line_buff, '\n');
  33. if (endptr != NULL)
  34. lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
  35. else {
  36. /* The line is too long - skip it. :-\ */
  37. do {
  38. if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
  39. return -1;
  40. } while (!(endptr = strchr(line_buff, '\n')));
  41. lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR);
  42. goto restart;
  43. }
  44. if (__sgetspent_r(line_buff, spwd, line_buff, buflen) < 0)
  45. goto restart;
  46. return 0;
  47. }