pam_modutil_ioloop.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * $Id$
  3. *
  4. * These functions provides common methods for ensure a complete read or
  5. * write occurs. It handles EINTR and partial read/write returns.
  6. */
  7. #include "pam_modutil_private.h"
  8. #include <unistd.h>
  9. #include <errno.h>
  10. int
  11. pam_modutil_read(int fd, char *buffer, int count)
  12. {
  13. int block, offset = 0;
  14. while (count > 0) {
  15. block = read(fd, &buffer[offset], count);
  16. if (block < 0) {
  17. if (errno == EINTR) continue;
  18. return block;
  19. }
  20. if (block == 0) return offset;
  21. offset += block;
  22. count -= block;
  23. }
  24. return offset;
  25. }
  26. int
  27. pam_modutil_write(int fd, const char *buffer, int count)
  28. {
  29. int block, offset = 0;
  30. while (count > 0) {
  31. block = write(fd, &buffer[offset], count);
  32. if (block < 0) {
  33. if (errno == EINTR) continue;
  34. return block;
  35. }
  36. if (block == 0) return offset;
  37. offset += block;
  38. count -= block;
  39. }
  40. return offset;
  41. }