Because of the frequent questions "How do I read one char at a time without waiting for a whole line?" and "How do I get a string from the user without echo of the input?" in IRC channel #c I've decided to write something to point them to.
You often see answers like, "use ncurses", but that is the wrong answer most of the time. The library ncurses is for writing text GUIs, probably not what our aspiring programmer wants to do. Not just yet in her career anyway.
<termios.h>.
The function tcgetattr() retrieves the current
settings and tcsetattr() changes them, now isn't that
intuitive? Now you just need to find out what to be changed in the
termios struct. The following members are of interest.
Even though both echo and line buffering is about input and output
c_lflag is the entry to manipulate. You're not supposed to remember all
this and therefore you have the manpage available. The mask to allow
echo is called ECHO and the mask for line-buffering is part of ICANON.
So what we do is that we clear out these bits from c_lflag.
1998 Oct. First published
2000 Apr. Rewritten in XML