@@ -268,6 +268,16 @@ fputnbytes(FILE *f, const char *str, size_t n)
268268}
269269
270270
271+ static void
272+ print_separator (struct separator sep , FILE * fout )
273+ {
274+ if (sep .separator_zero )
275+ fputc ('\000' , fout );
276+ else if (sep .separator )
277+ fputs (sep .separator , fout );
278+ }
279+
280+
271281/*************************/
272282/* Unaligned text */
273283/*************************/
@@ -276,8 +286,6 @@ fputnbytes(FILE *f, const char *str, size_t n)
276286static void
277287print_unaligned_text (const printTableContent * cont , FILE * fout )
278288{
279- const char * opt_fieldsep = cont -> opt -> fieldSep ;
280- const char * opt_recordsep = cont -> opt -> recordSep ;
281289 bool opt_tuples_only = cont -> opt -> tuples_only ;
282290 unsigned int i ;
283291 const char * const * ptr ;
@@ -286,24 +294,22 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
286294 if (cancel_pressed )
287295 return ;
288296
289- if (!opt_fieldsep )
290- opt_fieldsep = "" ;
291- if (!opt_recordsep )
292- opt_recordsep = "" ;
293-
294297 if (cont -> opt -> start_table )
295298 {
296299 /* print title */
297300 if (!opt_tuples_only && cont -> title )
298- fprintf (fout , "%s%s" , cont -> title , opt_recordsep );
301+ {
302+ fputs (cont -> title , fout );
303+ print_separator (cont -> opt -> recordSep , fout );
304+ }
299305
300306 /* print headers */
301307 if (!opt_tuples_only )
302308 {
303309 for (ptr = cont -> headers ; * ptr ; ptr ++ )
304310 {
305311 if (ptr != cont -> headers )
306- fputs ( opt_fieldsep , fout );
312+ print_separator ( cont -> opt -> fieldSep , fout );
307313 fputs (* ptr , fout );
308314 }
309315 need_recordsep = true;
@@ -318,15 +324,15 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
318324 {
319325 if (need_recordsep )
320326 {
321- fputs ( opt_recordsep , fout );
327+ print_separator ( cont -> opt -> recordSep , fout );
322328 need_recordsep = false;
323329 if (cancel_pressed )
324330 break ;
325331 }
326332 fputs (* ptr , fout );
327333
328334 if ((i + 1 ) % cont -> ncolumns )
329- fputs ( opt_fieldsep , fout );
335+ print_separator ( cont -> opt -> fieldSep , fout );
330336 else
331337 need_recordsep = true;
332338 }
@@ -342,25 +348,32 @@ print_unaligned_text(const printTableContent *cont, FILE *fout)
342348 {
343349 if (need_recordsep )
344350 {
345- fputs ( opt_recordsep , fout );
351+ print_separator ( cont -> opt -> recordSep , fout );
346352 need_recordsep = false;
347353 }
348354 fputs (f -> data , fout );
349355 need_recordsep = true;
350356 }
351357 }
352- /* the last record needs to be concluded with a newline */
358+ /*
359+ * The last record is terminated by a newline, independent of the set
360+ * record separator. But when the record separator is a zero byte, we
361+ * use that (compatible with find -print0 and xargs).
362+ */
353363 if (need_recordsep )
354- fputc ('\n' , fout );
364+ {
365+ if (cont -> opt -> recordSep .separator_zero )
366+ print_separator (cont -> opt -> recordSep , fout );
367+ else
368+ fputc ('\n' , fout );
369+ }
355370 }
356371}
357372
358373
359374static void
360375print_unaligned_vertical (const printTableContent * cont , FILE * fout )
361376{
362- const char * opt_fieldsep = cont -> opt -> fieldSep ;
363- const char * opt_recordsep = cont -> opt -> recordSep ;
364377 bool opt_tuples_only = cont -> opt -> tuples_only ;
365378 unsigned int i ;
366379 const char * const * ptr ;
@@ -369,11 +382,6 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
369382 if (cancel_pressed )
370383 return ;
371384
372- if (!opt_fieldsep )
373- opt_fieldsep = "" ;
374- if (!opt_recordsep )
375- opt_recordsep = "" ;
376-
377385 if (cont -> opt -> start_table )
378386 {
379387 /* print title */
@@ -393,19 +401,19 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
393401 if (need_recordsep )
394402 {
395403 /* record separator is 2 occurrences of recordsep in this mode */
396- fputs ( opt_recordsep , fout );
397- fputs ( opt_recordsep , fout );
404+ print_separator ( cont -> opt -> recordSep , fout );
405+ print_separator ( cont -> opt -> recordSep , fout );
398406 need_recordsep = false;
399407 if (cancel_pressed )
400408 break ;
401409 }
402410
403411 fputs (cont -> headers [i % cont -> ncolumns ], fout );
404- fputs ( opt_fieldsep , fout );
412+ print_separator ( cont -> opt -> fieldSep , fout );
405413 fputs (* ptr , fout );
406414
407415 if ((i + 1 ) % cont -> ncolumns )
408- fputs ( opt_recordsep , fout );
416+ print_separator ( cont -> opt -> recordSep , fout );
409417 else
410418 need_recordsep = true;
411419 }
@@ -417,15 +425,19 @@ print_unaligned_vertical(const printTableContent *cont, FILE *fout)
417425 {
418426 printTableFooter * f ;
419427
420- fputs ( opt_recordsep , fout );
428+ print_separator ( cont -> opt -> recordSep , fout );
421429 for (f = cont -> footers ; f ; f = f -> next )
422430 {
423- fputs ( opt_recordsep , fout );
431+ print_separator ( cont -> opt -> recordSep , fout );
424432 fputs (f -> data , fout );
425433 }
426434 }
427435
428- fputc ('\n' , fout );
436+ /* see above in print_unaligned_text() */
437+ if (cont -> opt -> recordSep .separator_zero )
438+ print_separator (cont -> opt -> recordSep , fout );
439+ else
440+ fputc ('\n' , fout );
429441 }
430442}
431443
0 commit comments