When I open a terminal running bash and type the following:
cat\nfoo\n^Decho bar\n
(where \n is enter and ^D is control-d) I get:
foo
bar
Ie, ^D causes cat to exit, but I can still type more.
How would I send the same input (specifically the end-of-file) through a (unix) pipe in C?
xtermdoes not perform its input with the shell and utilities viapipe(7)s. Instead, it uses the Unix PTY framework (seepty(7),openpty(3),forkpty(3),posix_openpt(3),pts(4)manpages for some information). The PTY framework allows any process to serve as a terminal “master” (e.g.,telnetd(8),sshd(8),xterm(1), etc.) and any process can connect to the terminal slave to provide an interactive environment just like sitting at the console.The Advanced Programming in the Unix Environment, 2nd edition book by Stevens and Rago has an excellent chapter on using pseudo-terminal devices to control slave programs — including an excellent little
ptyprogram that allows driving “interactive” programs in a manner similar toexpect(1), but in C rather thantcl.