Thanks for taking the time to provide all your feedback, and for the many very speedy replies.
Using a straight through pipe unfortunately doesn't tell me very much because it's both reading as text, then writing as text. The mangling translation through stdin is negated when translated again through stdout. If I perform any process with the data instead of just piping it back out, I find the data is different from the original file. In addition, if you would examine my code closely, you would notice that I was not using (print) to write any binary data (it was merely for feedback).
For better examples, I created a file called "filefullofcrlf.bin" that looks like this in a hex editor
Code: Select all
0d 0a 0d 0a 0d 0a 0d 0a 0d 0a 0d 0a
It contains 6 pairs of "\r\n", for a file length of 12 bytes.
Then I used a variation of your code that instead writes directly to a file
Code: Select all
#!/usr/bin/newlisp
; test1.lsp
(set 'fh (open "whatwasread.bin" "write"))
(while (> (read-buffer 0 'buff 1024) 0)
(write-buffer fh buff))
(close fh)
(exit)
When I pipe in "filefullofcrlf.bin" by either
C:\> type filefullofcrlf.bin | newlisp.exe test1.lsp
or
C:\> newlisp.exe test1.lsp < filefullofcrlf.bin
I get the output file "whatwasread.bin" containing exactly 6 bytes, all of which are 0x0a ("\n").
Here's another test
Code: Select all
#!/usr/bin/newlisp
; test2.lsp
(while (set 'ch (read-char 0))
(if (= ch 13) ; (char 13) == "\r"
(write-char 1 42))) ; Write a "*" for every "\r" in the file
; (char 42) == "*"
(exit)
If I feed in "filefullofcrlf.bin", then this code produces no output at all.
Just to make sure I'm not crazy, I created another file "filefullofcr.bin" that contains 12 "\r" bytes and piped it into test2.lsp. The output was 12 stars ("*").
One final test
Code: Select all
#!/usr/bin/newlisp
; test3.lsp
(while (read-char 0)
(write-char 1 42)) ; Write a "*" for every byte in the file
; (char 42) == "*"
(exit)
After feeding in "filefullofcrlf.bin", stdout shows me only 6 stars ("*") when it should be showing me 12.
All this strongly suggests that reading from stdin via (read-buffer) or (read-char) translates "\r\n" into "\n".
-----------------------------------------------------------
I also tested if reading binary 0's caused problems.
I created a 7 byte file "abc_nil_def.bin" which when viewed in a hex editor looks like
I piped this file into test1.lsp and test3.lsp with the following results:
test1.lsp: "whatwasread.bin" contained 7 bytes that matched "abc_nil_def.bin" exactly.
test3.lsp: The output was 7 stars ("*")
This seems to suggest that (read-buffer) and (read-char) are unaffected by binary 0's.
Edit:
All tests run on Windows XP Home and Pro with newLISP v.9.1.1 on Win32