| 786 | Use buffer BUF, whose size is BUFSIZE. */ |
| 787 | |
| 788 | static void |
| 789 | lines_split (intmax_t n_lines, char *buf, idx_t bufsize) |
| 790 | { |
| 791 | ssize_t n_read; |
| 792 | char *bp, *bp_out, *eob; |
| 793 | bool new_file_flag = true; |
| 794 | intmax_t n = 0; |
| 795 | |
| 796 | do |
| 797 | { |
| 798 | n_read = read (STDIN_FILENO, buf, bufsize); |
| 799 | if (n_read < 0) |
| 800 | error (EXIT_FAILURE, errno, "%s", quotef (infile)); |
| 801 | bp = bp_out = buf; |
| 802 | eob = bp + n_read; |
| 803 | *eob = eolchar; |
| 804 | while (true) |
| 805 | { |
| 806 | bp = rawmemchr (bp, eolchar); |
| 807 | if (bp == eob) |
| 808 | { |
| 809 | if (eob != bp_out) /* do not write 0 bytes! */ |
| 810 | { |
| 811 | idx_t len = eob - bp_out; |
| 812 | cwrite (new_file_flag, bp_out, len); |
| 813 | new_file_flag = false; |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | ++bp; |
| 819 | if (++n >= n_lines) |
| 820 | { |
| 821 | cwrite (new_file_flag, bp_out, bp - bp_out); |
| 822 | bp_out = bp; |
| 823 | new_file_flag = true; |
| 824 | n = 0; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | while (n_read); |
| 829 | } |
| 830 | |
| 831 | /* Split into pieces that are as large as possible while still not more |
| 832 | than N_BYTES bytes, and are split on line boundaries except |