writeall.c 472 B

1234567891011121314151617181920212223242526272829
  1. #include <sys/file.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include "../common.h"
  5. /* We return EPIPE here to indicate incomplete write.
  6. In all concievable case that should be the only possible
  7. cause (and we'll probably get SIGPIPE anyway) */
  8. long writeall(int fd, void* buf, long len)
  9. {
  10. long wr = 0;
  11. while(len > 0) {
  12. wr = write(fd, buf, len);
  13. if(!wr)
  14. errno = -EPIPE;
  15. if(wr <= 0)
  16. break;
  17. buf += wr;
  18. len -= wr;
  19. }
  20. return wr > 0 ? wr : -1;
  21. }