Ram
Ram

Reputation: 822

redirection to multiple ttys in c

I see that I can do freopen to redirect stdout going to a console to one another tty. I am trying to redirect the same to multiple terminals including the console. Console is where the program is running. What is the best way to do it?

TIA

Upvotes: 2

Views: 635

Answers (2)

gerrit zijlstra
gerrit zijlstra

Reputation: 754

There is no really easy way to do this like with freopen. You need some wrapper that takes the input and writes it to each output tty individually.

For example there is the tee program that multiplexes its input to stdout and a number of files. You could for example create a pipe in C that is connected to tee /dev/ttyX /dev/ttyY .... Then you can replace stdout with the pipe file descriptor and you will get the desired behaviour.

Upvotes: 0

millinon
millinon

Reputation: 1576

You didn't specify what platform you're using, but assuming you can find the file path to the TTY you'd like to redirect to, you can call freopen on the stdout file descriptor. However, that would close the initial file descriptor, which doesn't sound like your desired behavior. A file descriptor can only point to one file.

The easiest C solution is probably going to be a wrapper around printf that calls it on all of your specified files. You might be able to do something with threading, but that's likely to complicate things.

If you're on a *nix system, I suggest using tee which is made for outputting to stdout and secondary files.

Upvotes: 2

Related Questions