2

I'd like to know how to emulate the ICANON behavior of ^D: namely, trigger an immediate, even zero-byte, read in the program on the other end of a FIFO or PTY or socket or somesuch. In particular, I have a program whose specification is that it reads a script on stdin up until it gets a zero byte read, then reads input to feed the script, and I'd like to automatically test this functionality.

Simply writing to a FIFO doesn't result in the right thing happening, of course, since there's no zero byte read. Help?

Thanks!

  • There's a pretty significant difference between the various reader/writer behaviors of the different types of files you mention. Is it a pty or fifo or socket that you really need to work with? Anyway, if you want to trigger end of file on a pipe, you have to close the pipe. You can save it on another fd temporarily, then reopen it. If the reader tries to read in the meantime it will see EOF - you'd wanna trap its reaction, probably. – mikeserv Apr 3 '15 at 11:04
2

As far as I know, this behavior is unique to terminal devices, so that's what you have to use.

Use a pseudo-tty whose slave side is in ICANON mode, and write Ctl-d ('\4') to the master side.

  • And one program to aid one in doing so is ptybandage, available in Dan Bernstein's 1999 "ptyget" package (based upon his original "pty" package published to comp.sources.unix, volume 25 issues 127 to 135, in 1992), in Paul Jarc's updated version, and in the nosh source package as an execline script that uses nosh terminal management tools. – JdeBP Apr 4 '15 at 11:05
1

What about:

echo -n '' > /path/to/the/fifo

echo -n produces no newlines and '' is an empty string. So that should result in nothing (0 bytes) being written into the fifo.

You can shorten that echo down to literally nothing:

> /path/to/the/fifo

(if you're careful not to misunderstand it as piping some other output)

  • 1
    It's a guess - the question needs more context. – frostschutz Apr 3 '15 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.