Spawning a pty in D

Colin Grogan grogan.colin at gmail.com
Thu Oct 17 06:53:37 PDT 2013


Im having an issue
I can link to the C header file pty.h 
(http://man7.org/linux/man-pages/man3/openpty.3.html) and call 
forkpty like here:
http://dpaste.dzfl.pl/c3b07855

You have to compile that by linking with the util library
( add "libs-posix": ["util"] to dubs package.json )
For ease of reference, the main function is:

import std.stdio;
import pty;
void main(){
     int master, slave;
     char[16] name;
     int pid = forkpty(&master, &name[0], null, null);
     writefln("PID: %s", pid);
     writefln("Master : %s", master);
     writefln("Filename: %s", name);
}

If I run that I get:
~/Projects/D/dexpect$ ./dexpect
PID: 2224
Master : 3
Filename: /dev/pts/6�����

(As you can see, I'm attempting to create a D version of the 
expect library, good way to learn some of the intricacies of D 
and will be useful I suspect)

What I get back is a file name and a File Descriptor to the 
master side of the pty.
How do I then open this file and perform IO on it, preferably in 
a D way (std.stdio.File hopefully) rather than through C's mess 
of function calls?

If I open /dev/pts/6 with File("/dev/pts/6", "rw") and attempt to 
write to it, I get a "Bad file descriptor" error.
Reading from that file blocks (which I think is correct...)

Anyone have any experience with this?

As an aside, I'd prefer to do this in a pure D way, and not have 
to compile against any external C libraries, does anyone know if 
it is possible to spawn a pty in D without resorting to calling 
external C libs?

Sorry for the long post!
Thanks


More information about the Digitalmars-d-learn mailing list