mv cursetree/pty subdir
This commit is contained in:
parent
878be07a71
commit
69745c81f7
7 changed files with 1 additions and 1 deletions
45
cursetree/pty/epty.c
Normal file
45
cursetree/pty/epty.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue