简体   繁体   中英

(unix/C) “stty: stdin isn't a terminal” when using system() function

We're reading a file from stdin into file_buffer , and then stepping into a method more .

As soon as we use system("stty cbreak -echo"); , the output prints "stty: stdin isn't a terminal" and doesn't set our terminal to the settings we asked for.

This problem only exists when we use standard in. If we use a file argument, the program works fine -- the terminal settings get set, and there is no error message.

So, this is okay: myprogram file1.txt

But this is not: myprogram < file1.txt

Either way the contents are being read into file_buffer before being used at all. What the heck is wrong with using stty if we're taking input from stdin??

If you use input redirection or pipes, then stdin is not a TTY.

You can use isatty to check for that.

When the standard input is a file, it isn't a terminal, so setting terminal attributes on stty 's standard input won't work.

It sounds daft at first, but you will probably find that you can use either stdout or stderr as the input for stty and it will adjust the terminal. Therefore:

system("stty cbreak -echo <&2");

is likely to set the terminal characteristics. If you have a GNU version of stty , you could also use:

system("stty -F /dev/stderr cbreak -echo");

or substitute /dev/stdout or /dev/tty for /dev/stderr . You could also use any of the named devices instead of the &2 in the redirection in the first variant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM