[...] there are actually a few features that you get for free just from your terminal, without the program needing to do anything special at all.
The things you get for free are: [...]
- backspace
- Ctrl+W, to delete the previous word
- Ctrl+U, to delete the whole line
Linux noob here, so this may be a stupid question, but how does that work under the hood?
The documentation for fgets() says [1]:
> Reads at most count - 1 characters from the given file stream and stores them in the character array pointed to by str. Parsing stops if a newline character is found (in which case str will contain that newline character) or if end-of-file occurs.
Does that mean that, by default, fgets() blocks until the user enters a newline and, before they do, it lets them edit the line buffer using the backspace, CTRL+W and CTRL+U shortcuts?
But there seems to be no guarantee that a program is using fgets() to read an entire line - it could also set count to 2 and read individual characters. Those could not be "unread" anymore when a backspace occurs. Is there some magic that lets fgets() buffer characters internally in such a situation, or would the backspace/line editing functionality just be broken then?