[comp.bugs.4bsd] sort

ok@cs.mu.oz.au (Richard O'Keefe) (10/06/89)

Try this:

#!/bin/sh
cat >mktrouble.c <<end_of_file.
char trouble[] =
"This\000will\000not\000do\n\
This\000file\000contains\000NUL\000characters.\n\
This\000(if\000you\000please)\000is\000out\000of\000order.\n";
main() {write(1, trouble, -1+sizeof trouble); exit(0);}
end_of_file.
cc -o mktrouble mktrouble.c
./mktrouble >ZABBO
sort ZABBO
rm ZABBO
exit
#end of script

The output on a Sun running SunOS 4.0 is
  sort: warning: missing NEWLINE added at end of input file ZABBO
  sort: warning: missing NEWLINE added at end of input file ZABBO
  sort: warning: missing NEWLINE added at end of input file ZABBO
  This
  This
  This
The output on an ELXSI is the same except that the message is
  sort: missing newline before EOF in ZABBO
If the input is a pipe, as in ./mktrouble | sort, the message is
  sort: warning: missing NEWLINE added at EOF

I expect that the error is also present in System V, but I have not
tried it.  A plausible reason for the error would be using fgets()
to read the input, then checking to see whether the character before
the NUL was a \n, but without sources I can't tell whether that is so.

Yes, I know that NUL is a rather implausible character in UNIX.
That's exactly why it looked like being a good field separator.

Making sort(1) *work* with NULs in records might be more costly than
is warranted.  It should not be unduly expensive, however, for sort
to check when it is about to print this message, and print a correct
warning: "input line truncated at NUL byte".  The manual page should
include
BUGS
     sort(1) truncates lines containing NUL bytes.