I’m wanting to write my own psuedo-shell and would like to get pretty colors etc. How do I go about tricking a subprocess into thinking that it is in a TTY? I’ve read about virtual TTY’s but haven’t found much practical information about how to either create one or how that makes a subprocess think that isatty(stdout) == 1.
I’m wanting to write my own psuedo-shell and would like to get pretty colors
Share
What you’re looking for are called pseudoterminals, pseudo-ttys or ptys. These exist in master/slave pairs, that behave similarly to socket pairs (the bidirectional version of pipes; what is written to one end can be read on the other). In the controlling process, use
posix_openptto open a master, thenptsnameto get the slave’s name (probably/dev/pts/X):As usual, each function can fail, so add error checking. The
slavefd now refers to the slave device. Usedup2(slave, STDOUT_FILENO)in the child process to set standard output to the slave pseudoterminal; similarly for stdin and stderr.(Note that some Linux manpages incorrectly state that
posix_openptreturnschar *. Also, don’t get confused by theopenptyfamily of functions; these represent an older interface to pseudo-ttys that is deprecated.)