full-write.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* full-write.c -- an interface to write that retries after interrupts
  2. Copyright (C) 1993, 1994, 1997, 1998, 1999 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. #if HAVE_UNISTD_H
  21. # include <unistd.h>
  22. #endif
  23. #include <errno.h>
  24. #ifndef errno
  25. extern int errno;
  26. #endif
  27. /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
  28. Return LEN upon success, write's (negative) error code otherwise. */
  29. ssize_t
  30. full_write (int desc, const char *ptr, size_t len)
  31. {
  32. ssize_t total_written = 0;
  33. while (len > 0)
  34. {
  35. ssize_t written = write (desc, ptr, len);
  36. /* FIXME: write on my slackware Linux 1.2.13 returns zero when
  37. I try to write more data than there is room on a floppy disk.
  38. This puts dd into an infinite loop. Reproduce with
  39. dd if=/dev/zero of=/dev/fd0. */
  40. if (written < 0)
  41. {
  42. #ifdef EINTR
  43. if (errno == EINTR)
  44. continue;
  45. #endif
  46. return written;
  47. }
  48. total_written += written;
  49. ptr += written;
  50. len -= written;
  51. }
  52. return total_written;
  53. }