add epty.* to extend _pty.*
stdout/stderr independence for child process
This commit is contained in:
parent
ac590806bf
commit
b2f204a2cb
2 changed files with 53 additions and 0 deletions
45
cli/epty.c
Normal file
45
cli/epty.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "mkpty.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;
|
||||
}
|
||||
8
cli/epty.h
Normal file
8
cli/epty.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef DORNE_EPTY_H
|
||||
#define DORNE_EPTY_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
pid_t forkepty(int *fdmx, int *fderr);
|
||||
|
||||
#endif /* DORNE_EPTY_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue