Pseudo Terminal pair vs popen()

Writing a telnetd for a 1980s Uniflex workstation. No threads, just processes.

Can some wise person explain why I would use a pty pair rather than just call popen() ?

Feels like I am missing something on what pty does.

I’m not the wise person you seek, but… someone on the internet says

popen is a SIMPLE wrapper for uni-directional communication only (either reading or writing). Some enhanced versions of popen() allow bi-directional flow (which is what you would want for an interactive program like telnet).

So you might need to check what kind of popen is available to you - simple or enhanced. But I’m very much guessing. Perhaps see also
Can popen() make bidirectional pipes like pipe() + fork()? (stackoverflow)
where we see

The man page for popen() on my system says a bidirectional pipe is possible, but my code needs to run on a system with an older version supporting only unidirectional pipes.

It’s been a long, long time since I thought about this stuff, but … I think you need a pty because otherwise there won’t be a terminal driver anywhere in the link between the remote client and server where the telnetd process is running.

Suppose the remote client sends an ASCII character (byte) which has the value 3 up the telnet connection to the server. This is an ETX control character, commonly known as “Control C”. If it goes up a pipe, it will just be delivered. If it goes through a terminal driver, the driver will send SIGINT to whatever process is listening to the terminal, the usual behavior of ^C that we expect on a terminal.

The pty is basically a bidirectional pipe with a terminal driver in it.

2 Likes

Thanks all. Useful info. I found this last night which is the best step by step explanation / example code I found so far that really made it clear to me (essentially what pdxjjb said).

http://www.rkoucha.fr/tech_corner/pty_pdip.html

1 Like