mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-04-02 07:15:31 -04:00
spawn: add optional stdin/stdout/stderr redirection FDs
If not -1, spawn() will redirect the child's stdin/stdout/stderr to these FDs.
This commit is contained in:
parent
57f5cc1bf2
commit
69d9ff3f25
3 changed files with 11 additions and 5 deletions
10
spawn.c
10
spawn.c
|
|
@ -13,7 +13,8 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
bool
|
bool
|
||||||
spawn(struct reaper *reaper, const char *cwd, char *const argv[])
|
spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
||||||
|
int stdin_fd, int stdout_fd, int stderr_fd)
|
||||||
{
|
{
|
||||||
int pipe_fds[2] = {-1, -1};
|
int pipe_fds[2] = {-1, -1};
|
||||||
if (pipe2(pipe_fds, O_CLOEXEC) < 0) {
|
if (pipe2(pipe_fds, O_CLOEXEC) < 0) {
|
||||||
|
|
@ -30,8 +31,11 @@ spawn(struct reaper *reaper, const char *cwd, char *const argv[])
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* Child */
|
/* Child */
|
||||||
close(pipe_fds[0]);
|
close(pipe_fds[0]);
|
||||||
if ((cwd != NULL && chdir(cwd) < 0) ||
|
|
||||||
//execlp(term->foot_exe, term->foot_exe, NULL) < 0)
|
if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0 || close(stdin_fd) < 0)) ||
|
||||||
|
(stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0 || close(stdout_fd) < 0)) ||
|
||||||
|
(stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0 || close(stderr_fd) < 0)) ||
|
||||||
|
(cwd != NULL && chdir(cwd) < 0) ||
|
||||||
execvp(argv[0], argv) < 0)
|
execvp(argv[0], argv) < 0)
|
||||||
{
|
{
|
||||||
(void)!write(pipe_fds[1], &errno, sizeof(errno));
|
(void)!write(pipe_fds[1], &errno, sizeof(errno));
|
||||||
|
|
|
||||||
3
spawn.h
3
spawn.h
|
|
@ -3,4 +3,5 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "reaper.h"
|
#include "reaper.h"
|
||||||
|
|
||||||
bool spawn(struct reaper *reaper, const char *cwd, char *const argv[]);
|
bool spawn(struct reaper *reaper, const char *cwd, char *const argv[],
|
||||||
|
int stdin_fd, int stdout_fd, int stderr_fd);
|
||||||
|
|
|
||||||
|
|
@ -2248,7 +2248,8 @@ bool
|
||||||
term_spawn_new(const struct terminal *term)
|
term_spawn_new(const struct terminal *term)
|
||||||
{
|
{
|
||||||
return spawn(
|
return spawn(
|
||||||
term->reaper, term->cwd, (char *const []){term->foot_exe, NULL});
|
term->reaper, term->cwd, (char *const []){term->foot_exe, NULL},
|
||||||
|
-1, -1, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue