! * Function: read_atmost_n * Reads at the most `read_upto` bytes from the * file object `fp` before returning. * Parameters: * [in] fp : The file object from which it needs to read. * [in] buf : The buffer into which it needs to write the data. * [in] read_upto: Max number of bytes which must be read from `fd`. * [out] int : Number of bytes written to `buf` or read from `f
| 668 | * reaches 50 after which it will return with whatever data it read. |
| 669 | */ |
| 670 | static inline |
| 671 | int read_atmost_n(FILE* fp, char* buf, size_t read_upto) |
| 672 | { |
| 673 | #ifdef __USING_WINDOWS__ |
| 674 | return (int)fread(buf, 1, read_upto, fp); |
| 675 | #else |
| 676 | int fd = subprocess_fileno(fp); |
| 677 | int rbytes = 0; |
| 678 | int eintr_cnter = 0; |
| 679 | |
| 680 | while (1) { |
| 681 | int read_bytes = read(fd, buf + rbytes, read_upto - rbytes); |
| 682 | if (read_bytes == -1) { |
| 683 | if (errno == EINTR) { |
| 684 | if (eintr_cnter >= 50) return -1; |
| 685 | eintr_cnter++; |
| 686 | continue; |
| 687 | } |
| 688 | return -1; |
| 689 | } |
| 690 | if (read_bytes == 0) return rbytes; |
| 691 | |
| 692 | rbytes += read_bytes; |
| 693 | } |
| 694 | return rbytes; |
| 695 | #endif |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /*! |
no outgoing calls
no test coverage detected