| 1213 | to opening and closing each file for each line. */ |
| 1214 | |
| 1215 | static void |
| 1216 | lines_rr (intmax_t k, intmax_t n, char *buf, idx_t bufsize, of_t **filesp) |
| 1217 | { |
| 1218 | bool wrapped = false; |
| 1219 | bool wrote = false; |
| 1220 | bool file_limit; |
| 1221 | idx_t i_file; |
| 1222 | of_t *files IF_LINT (= NULL); |
| 1223 | intmax_t line_no; |
| 1224 | |
| 1225 | if (k) |
| 1226 | line_no = 1; |
| 1227 | else |
| 1228 | { |
| 1229 | if (IDX_MAX < n) |
| 1230 | xalloc_die (); |
| 1231 | files = *filesp = xinmalloc (n, sizeof *files); |
| 1232 | |
| 1233 | /* Generate output file names. */ |
| 1234 | for (i_file = 0; i_file < n; i_file++) |
| 1235 | { |
| 1236 | next_file_name (); |
| 1237 | files[i_file].of_name = xstrdup (outfile); |
| 1238 | files[i_file].ofd = OFD_NEW; |
| 1239 | files[i_file].ofile = NULL; |
| 1240 | files[i_file].opid = 0; |
| 1241 | } |
| 1242 | i_file = 0; |
| 1243 | file_limit = false; |
| 1244 | } |
| 1245 | |
| 1246 | while (true) |
| 1247 | { |
| 1248 | char *bp = buf, *eob; |
| 1249 | ssize_t n_read = read (STDIN_FILENO, buf, bufsize); |
| 1250 | if (n_read < 0) |
| 1251 | error (EXIT_FAILURE, errno, "%s", quotef (infile)); |
| 1252 | else if (n_read == 0) |
| 1253 | break; /* eof. */ |
| 1254 | eob = buf + n_read; |
| 1255 | |
| 1256 | while (bp != eob) |
| 1257 | { |
| 1258 | idx_t to_write; |
| 1259 | bool next = false; |
| 1260 | |
| 1261 | /* Find end of line. */ |
| 1262 | char *bp_out = memchr (bp, eolchar, eob - bp); |
| 1263 | if (bp_out) |
| 1264 | { |
| 1265 | bp_out++; |
| 1266 | next = true; |
| 1267 | } |
| 1268 | else |
| 1269 | bp_out = eob; |
| 1270 | to_write = bp_out - bp; |
| 1271 | |
| 1272 | if (k) |
no test coverage detected