#include #include "_pty.h" #define PIPE_READ 0 #define PIPE_WRITE 1 /* Allocate a PTY and fork, giving fdmx (master) to the parent * and binding the child's stdin/stdout to fds (slave). * Return value is indentical to fork(2). * NOTE: This function is my alternative to GLibC's * NOTE: forkpty() function. It exists as a learning resource. * REF: https://sourceware.org/git/glibc.git -> ./login/forkpty.c */ pid_t forkepty(int *fdmx, int *fderr) { // master/slave, and stderr pipe fds int epipe[2]; pid_t pid; if (pipe(epipe) == -1) return EXIT_FAILURE; switch (pid = forkmkpty(fdmx)) { case -1: /* forkmkpty() will close fdmx/fds for us */ close(epipe[PIPE_READ]); close(epipe[PIPE_WRITE]); return -1; case 0: /* Child Process */ /* forkmkpty() will close fdmx for us */ close(epipe[PIPE_READ]); BIND(epipe[PIPE_WRITE], STDERR_FILENO); break; default: /* Parent Process */ /* forkmkpty() will close fds for us */ close(epipe[PIPE_WRITE]); *fderr = epipe[PIPE_READ]; break; } /* Both Processes */ return pid; }