| 572 | | |
|---|
| 573 | | /** |
|---|
| 574 | | * camel_write: |
|---|
| 575 | | * @fd: file descriptor |
|---|
| 576 | | * @buf: buffer to write |
|---|
| 577 | | * @n: number of bytes of @buf to write |
|---|
| 578 | | * |
|---|
| 579 | | * Cancellable libc write() replacement. |
|---|
| 580 | | * |
|---|
| 581 | | * Code that intends to be portable to Win32 should call this function |
|---|
| 582 | | * only on file descriptors returned from open(), not on sockets. |
|---|
| 583 | | * |
|---|
| 584 | | * Returns number of bytes written or -1 on fail. On failure, errno will |
|---|
| 585 | | * be set appropriately. |
|---|
| 586 | | **/ |
|---|
| 587 | | ssize_t |
|---|
| 588 | | camel_write (int fd, const char *buf, size_t n) |
|---|
| | 572 | static ssize_t |
|---|
| | 573 | camel_write_shared (int fd, const char *buf, size_t n, gboolean write_in_chunks) |
|---|
| 604 | | /* Write in chunks of max WRITE_CHUNK_SIZE bytes */ |
|---|
| 605 | | ssize_t actual = MIN (n - written, WRITE_CHUNK_SIZE); |
|---|
| 606 | | |
|---|
| 607 | | do { |
|---|
| 608 | | w = write (fd, buf + written, actual /* n - written */); |
|---|
| 609 | | } while (w == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)); |
|---|
| | 589 | if (write_in_chunks) { |
|---|
| | 590 | /* Write in chunks of max WRITE_CHUNK_SIZE bytes */ |
|---|
| | 591 | ssize_t actual = MIN (n - written, WRITE_CHUNK_SIZE); |
|---|
| | 592 | do { |
|---|
| | 593 | w = write (fd, buf + written, actual); |
|---|
| | 594 | } while (w == -1 && (errno == EINTR || errno == EAGAIN || |
|---|
| | 595 | errno == EWOULDBLOCK)); |
|---|
| | 596 | } else { |
|---|
| | 597 | do { |
|---|
| | 598 | w = write (fd, buf + written, n - written); |
|---|
| | 599 | } while (w == -1 && (errno == EINTR || errno == EAGAIN || |
|---|
| | 600 | errno == EWOULDBLOCK)); |
|---|
| | 601 | } |
|---|
| 648 | | do { |
|---|
| 649 | | w = write (fd, buf + written, actual /*n - written*/); |
|---|
| 650 | | } while (w == -1 && errno == EINTR); |
|---|
| 651 | | |
|---|
| | 637 | if (write_in_chunks) { |
|---|
| | 638 | /* Write in chunks of max WRITE_CHUNK_SIZE bytes */ |
|---|
| | 639 | ssize_t actual = MIN (n - written, WRITE_CHUNK_SIZE); |
|---|
| | 640 | do { |
|---|
| | 641 | w = write (fd, buf + written, actual); |
|---|
| | 642 | } while (w == -1 && (errno == EINTR || errno == EAGAIN || |
|---|
| | 643 | errno == EWOULDBLOCK)); |
|---|
| | 644 | } else { |
|---|
| | 645 | do { |
|---|
| | 646 | w = write (fd, buf + written, n - written); |
|---|
| | 647 | } while (w == -1 && (errno == EINTR || errno == EAGAIN || |
|---|
| | 648 | errno == EWOULDBLOCK)); |
|---|
| | 649 | } |
|---|
| | 668 | } |
|---|
| | 669 | |
|---|
| | 670 | |
|---|
| | 671 | /** |
|---|
| | 672 | * camel_write: |
|---|
| | 673 | * @fd: file descriptor |
|---|
| | 674 | * @buf: buffer to write |
|---|
| | 675 | * @n: number of bytes of @buf to write |
|---|
| | 676 | * |
|---|
| | 677 | * Cancellable libc write() replacement. |
|---|
| | 678 | * |
|---|
| | 679 | * Code that intends to be portable to Win32 should call this function |
|---|
| | 680 | * only on file descriptors returned from open(), not on sockets. |
|---|
| | 681 | * |
|---|
| | 682 | * Returns number of bytes written or -1 on fail. On failure, errno will |
|---|
| | 683 | * be set appropriately. |
|---|
| | 684 | **/ |
|---|
| | 685 | ssize_t |
|---|
| | 686 | camel_write (int fd, const char *buf, size_t n) |
|---|
| | 687 | { |
|---|
| | 688 | camel_write_shared (fd, buf, n, FALSE); |
|---|