full-write.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* full-write.c -- an interface to write that retries after interrupts
  2. Copyright 1993, 1994, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. Copied largely from GNU C's cccp.c.
  15. */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include "safe-read.h"
  21. #if HAVE_UNISTD_H
  22. # include <unistd.h>
  23. #endif
  24. #include <errno.h>
  25. #ifndef errno
  26. extern int errno;
  27. #endif
  28. /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
  29. Return LEN upon success, -1 (setting errno) otherwise. */
  30. ssize_t
  31. full_write (int desc, const char *ptr, size_t len)
  32. {
  33. ssize_t total_written = 0;
  34. while (len > 0)
  35. {
  36. ssize_t written = write (desc, ptr, len);
  37. if (written <= 0)
  38. {
  39. /* Some buggy drivers return 0 when you fall off a device's end. */
  40. if (written == 0)
  41. errno = ENOSPC;
  42. #ifdef EINTR
  43. if (errno == EINTR)
  44. continue;
  45. #endif
  46. return -1;
  47. }
  48. total_written += written;
  49. ptr += written;
  50. len -= written;
  51. }
  52. return total_written;
  53. }