writeall.c 501 B

123456789101112131415161718192021222324252627
  1. #include <poll.h>
  2. #include <unistd.h>
  3. #include "e.h"
  4. #include "writeall.h"
  5. int writeall(int fd,const void *x,long long xlen)
  6. {
  7. long long w;
  8. while (xlen > 0) {
  9. w = xlen;
  10. if (w > 1048576) w = 1048576;
  11. w = write(fd,x,w);
  12. if (w < 0) {
  13. if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
  14. struct pollfd p;
  15. p.fd = fd;
  16. p.events = POLLOUT | POLLERR;
  17. poll(&p,1,-1);
  18. continue;
  19. }
  20. return -1;
  21. }
  22. x += w;
  23. xlen -= w;
  24. }
  25. return 0;
  26. }