full-write.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* full-write.c -- an interface to write that retries after interrupts
  2. Copyright 1993, 1994, 1997, 1998, 1999, 2000, 2001 Free Software
  3. Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. Written by Paul Eggert. */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <sys/types.h>
  20. #include "full-write.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. or if partial writes occur. Return the number of bytes successfully
  30. written, setting errno if that is less than LEN. */
  31. size_t
  32. full_write (int desc, const char *ptr, size_t len)
  33. {
  34. size_t total_written = 0;
  35. while (len > 0)
  36. {
  37. ssize_t written = write (desc, ptr, len);
  38. if (written <= 0)
  39. {
  40. /* Some buggy drivers return 0 when you fall off a device's end. */
  41. if (written == 0)
  42. errno = ENOSPC;
  43. #ifdef EINTR
  44. if (errno == EINTR)
  45. continue;
  46. #endif
  47. break;
  48. }
  49. total_written += written;
  50. ptr += written;
  51. len -= written;
  52. }
  53. return total_written;
  54. }