David X
David X

Reputation: 4176

How to send end-of-file through a pipe without closing the pipe?

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?

Upvotes: 2

Views: 3036

Answers (2)

sarnold
sarnold

Reputation: 104100

xterm does not perform its input with the shell and utilities via pipe(7)s. Instead, it uses the Unix PTY framework (see pty(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 pty program that allows driving "interactive" programs in a manner similar to expect(1), but in C rather than tcl.

Upvotes: 6

mah
mah

Reputation: 39847

Control-D is special to the terminal, not to general input. Simply close the pipe.

Upvotes: 3

Related Questions