__getspent_r.c 1.8 KB

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