Why is it possible to write() to STDIN?

前端 未结 3 990
后悔当初
后悔当初 2020-12-30 14:13

I have the following code:

int main()
{
    char str[] = \"Hello\\n\";
    write(0, str, 6);   // write() to STDIN
    return 0;
}

When I c

相关标签:
3条回答
  • 2020-12-30 14:47

    Because by default your terminal will echo stdin back out to the console. Try redirecting it to a file; it didn't actually write to stdout.

    0 讨论(0)
  • 2020-12-30 14:55

    Well, old Unix systems were originaly used with serial terminals, and a special program getty was in charge to manage the serial devices, open and configure them, display a message on an incoming connexion (break signal), and pass the opened file descriptors to login and then the shell.

    It used to open the tty device as input/output to configure it, and that was then duplicated in file descriptors 0, 1 and 2. And by default the (still good old) stty command operates by default on standard input. To ensure compatibility, on modern Linuxes, when you are connected to a terminal, file descriptor 0 is still opened for input/output.

    It can be used as a quick and dirty hack to only display prompts when standard input is connected to a terminal, because if standard input is redirected to a read only file or pipe, all writes will fail (without any harm for the process) and nothing will be printed. But it is anyway a dirty hack: just imagine what happens if a caller passes a file opened for input/output as standard input... That's why good practices recommend to use stderr for prompts or messages to avoid having them lost in redirected stream while keeping output and input in separate streams, which is neither harder nor longer.

    TL/DR: if you are connected to a terminal, standard input is opened for input/output even if the name and standard usage could suggest it is read only.

    0 讨论(0)
  • 2020-12-30 15:05

    Are you confusing write with fwrite? The first parameter in write is a "file descripter", but it's not stdin. Try doing an fwrite to stdin -- it doesn't happen.

    0 讨论(0)
提交回复
热议问题