full-write.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #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, write's (negative) error code 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. /* FIXME: write on my slackware Linux 1.2.13 returns zero when
  38. I try to write more data than there is room on a floppy disk.
  39. This puts dd into an infinite loop. Reproduce with
  40. dd if=/dev/zero of=/dev/fd0. */
  41. if (written < 0)
  42. {
  43. #ifdef EINTR
  44. if (errno == EINTR)
  45. continue;
  46. #endif
  47. return written;
  48. }
  49. total_written += written;
  50. ptr += written;
  51. len -= written;
  52. }
  53. return total_written;
  54. }