{"ignore-case", no_argument, NULL, 'i'},
{"check-order", no_argument, NULL, CHECK_ORDER_OPTION},
{"nocheck-order", no_argument, NULL, NOCHECK_ORDER_OPTION},
+ {"zero-terminated", no_argument, NULL, 'z'},
{"header", no_argument, NULL, HEADER_LINE_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
join them without checking for ordering */
static bool join_header_lines;
+/* The character marking end of line. Default to \n. */
+static char eolchar = '\n';
+
void
usage (int status)
{
--nocheck-order do not check that the input is correctly sorted\n\
--header treat the first line in each file as field headers,\n\
print them without trying to pair them\n\
+"), stdout);
+ fputs (_("\
+ -z, --zero-terminated end lines with 0 byte, not newline\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
else
line = init_linep (linep);
- if (! readlinebuffer (&line->buf, fp))
+ if (! readlinebuffer_delim (&line->buf, fp, eolchar))
{
if (ferror (fp))
error (EXIT_FAILURE, errno, _("read error"));
break;
putchar (output_separator);
}
- putchar ('\n');
+ putchar (eolchar);
}
else
{
prfields (line1, join_field_1, autocount_1);
prfields (line2, join_field_2, autocount_2);
- putchar ('\n');
+ putchar (eolchar);
}
}
issued_disorder_warning[0] = issued_disorder_warning[1] = false;
check_input_order = CHECK_ORDER_DEFAULT;
- while ((optc = getopt_long (argc, argv, "-a:e:i1:2:j:o:t:v:",
+ while ((optc = getopt_long (argc, argv, "-a:e:i1:2:j:o:t:v:z",
longopts, NULL))
!= -1)
{
}
break;
+ case 'z':
+ eolchar = 0;
+ break;
+
case NOCHECK_ORDER_OPTION:
check_input_order = CHECK_ORDER_DISABLED;
break;
[ "ID1 Name\n1 A\n", ""],
"ID1 Name\n1 A\n", 0],
+# Zero-terminated lines
+['z1', '-z',
+ ["a\0c\0e\0", "a\0b\0c\0"], "a\0c\0", 0],
+
+# not zero-terminated, but related to the code change:
+# the old readlinebuffer() auto-added '\n' to the last line.
+# the new readlinebuffer_delim() does not.
+# Ensure it doesn't matter.
+['z2', '',
+ ["a\nc\ne\n", "a\nb\nc"], "a\nc\n", 0],
+['z3', '',
+ ["a\nc\ne", "a\nb\nc"], "a\nc\n", 0],
+# missing last NUL at the end of the last line (=end of file)
+['z4', '-z',
+ ["a\0c\0e", "a\0b\0c"], "a\0c\0", 0],
+# edge-case: the embedded newlines should treated as
+# part of the nul-terminated line
+['z5', '-z -a1 -a2',
+ ["a\n1\0c 3\0","b\n8\0c 9\0"], "a\n1\0b\n8\0c 3 9\0"],
+
);
# Convert the above old-style test vectors to the newer