3 ms·
Linefeed (\n) is a single byte in DOS as well. I think you are talking about carriage return linefeed pair (CRLF or \r\n), These control codes go back to line
by criddell 6mo ago
Linefeed (\n) is a single byte in DOS as well.
I think you are talking about carriage return linefeed pair (CRLF or \r\n),
These control codes go back to line printers. Linefeed advances the paper one line and carriage return moves the print head to the left.
- Isamu 6mo ago>Linefeed (\n) is a single byte in DOS as well. In binary mode. In text mode if you printf(“Hello World\n”) you get CRLF because that’s how text works on DOS. Unix had the convention of only requiring the LF for text. And Unix didn’t have text/binary modes. That’s the compatibility hack on DOS. >These control codes go back to line printers. Back to teletypes even. Believe me, I go back to line printers.
- foresto 6mo agoI'm pretty sure that conversion was done by the C library, just as stated in the article. Not by DOS. ASCII 0x0A '\n' is always one byte*, and C library implementations for DOS would insert an ASCII 0x0D '\r' byte before it at output time if the C FILE stream had been opened in text mode. Note that printf(), which you use in your example, is a C library function that writes writes to a predefined text mode stream. So it follows the same rules. I wasn't able to dig up the source code of a vintage DOS compiler's C library in a few minutes of looking, so I can't prove it right now, but this section of the C standard (7.21.2 - Streams) hints that my recollection is correct: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf#page=316 https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf#p... *(On systems where the char type is one byte, of course, which is the case for DOS C compilers.)
- criddell 6mo agoTeletypes is what I meant to say, thank you. Line printers have no carriage return mechanism.
- satiated_grue 6mo agoYes, and the order is important. Sending a carriage return and linefeed to a TTY 33 and then printing works fine. Doing them in the opposite order, if the carriage is to the right of the page, will result in a linefeed (platen rotation) happening quickly, then the carriage starting to move left to the beginning of the next line, and then the next printed character will print wherever the carriage happens to be at the time - not yet to the left. So you will be missing a character at the beginning of the line because it's in between the two lines in an unexpected column-ish. I have run into (in my mind, "hipster") code where the programmer for some reason reversed the order of CR and LF.
- anitil 6mo agoAnnoyingly I actually think '\r\n' is the correct line ending here - advance the paper and return the carriage, but I suppose unix took the simpler implementation which makes looping over characters, words (split by ' ') and lines (split by '\n') simpler as each loop only has a single comparison
- Joker_vD 6mo agoYep, on Unixen the translation of CRLF to LF when printing to the terminal (and from CR to CRLF when reading input from the terminal) is done in the kernel, it's called "line discipline".
- poizan42 6mo agoAnd if you switch the tty from "cooked" to "raw" mode then it doesn't do the conversion, and a CR just moves the cursor back to the start of the line and a LF just moves the cursor one line down.
- fragmede 6mo agoWhich is how you do the fun spinny icons on the command line without having to invoke ncurses!
- freedomben 6mo agoYou can also just use a \r directly without a \n. For example: spin='/-\|' while true; do i=$(( (i+1) % 4 )) printf "\r${spin:$i:1} Working..." sleep 0.1 done
- freedomben 6mo agoBy the way yes, that is a subtle reference to the TOS computer, so it's best read in that voice :-)
- spauldo 6mo ago