fe
terminal file explorer and picker
git clone https://9o.is/git/fe.git
commit ca3fadfcaeb9dde00f4303174da033fe21927621 parent 7d7ce9facbf8e50672ea54217bcdd47be6b94c9c Author: Jul <jul@9o.is> Date: Mon, 19 Jan 2026 01:24:23 -0500 merge spawn with actions Diffstat:
| M | Makefile | | | 1 | - |
| M | actions.c | | | 51 | ++++++++++++++++++++++++++++++++++++++++++++++++++- |
| D | spawn.c | | | 50 | -------------------------------------------------- |
| D | spawn.h | | | 5 | ----- |
4 files changed, 50 insertions(+), 57 deletions(-)
diff --git a/Makefile b/Makefile @@ -20,7 +20,6 @@ OBJS = fe.o \ tty_interface.o \ tty.o \ actions.o \ - spawn.o \ compats.o all: fe diff --git a/actions.c b/actions.c @@ -1,6 +1,55 @@ +#include <sys/types.h> +#include <sys/wait.h> #include <stdlib.h> +#include <errno.h> +#include <stdarg.h> +#include <unistd.h> +#include <stdio.h> #include <string.h> -#include "spawn.h" + +#define EXEC_REPLACE 1 +#define EXEC_SHELL 2 +#define EXEC_CAPTURE 4 + +static int x_spawn(const char *cmd, char *const argv[], int flags, char *out_buf, size_t out_sz) { + int pipefd[2]; + + if (flags & EXEC_CAPTURE) { + if (pipe(pipefd) == -1) return -1; + } + + pid_t pid = (flags & EXEC_REPLACE) ? 0 : fork(); + + if (pid == -1) return -1; + + if (pid == 0) { + if (flags & EXEC_CAPTURE) { + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[0]); + close(pipefd[1]); + } + + if (flags & EXEC_SHELL) { + execlp("sh", "sh", "-c", cmd, (char *)NULL); + } else { + execvp(cmd, argv); + } + + perror("exec failed"); + if (flags & EXEC_REPLACE) return -1; + _exit(EXIT_FAILURE); + } + + if (flags & EXEC_CAPTURE) { + close(pipefd[1]); + read(pipefd[0], out_buf, out_sz - 1); + close(pipefd[0]); + } + + int status; + waitpid(pid, &status, 0); + return WEXITSTATUS(status); +} static void clear(tty_interface_t *state) { tty_t *tty = state->tty; diff --git a/spawn.c b/spawn.c @@ -1,50 +0,0 @@ -#include <sys/types.h> -#include <sys/wait.h> - -#include <errno.h> -#include <stdarg.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> -#include <string.h> -#include "spawn.h" - -int x_spawn(const char *cmd, char *const argv[], int flags, char *out_buf, size_t out_sz) { - int pipefd[2]; - - if (flags & EXEC_CAPTURE) { - if (pipe(pipefd) == -1) return -1; - } - - pid_t pid = (flags & EXEC_REPLACE) ? 0 : fork(); - - if (pid == -1) return -1; - - if (pid == 0) { - if (flags & EXEC_CAPTURE) { - dup2(pipefd[1], STDOUT_FILENO); - close(pipefd[0]); - close(pipefd[1]); - } - - if (flags & EXEC_SHELL) { - execlp("sh", "sh", "-c", cmd, (char *)NULL); - } else { - execvp(cmd, argv); - } - - perror("exec failed"); - if (flags & EXEC_REPLACE) return -1; - _exit(EXIT_FAILURE); - } - - if (flags & EXEC_CAPTURE) { - close(pipefd[1]); - read(pipefd[0], out_buf, out_sz - 1); - close(pipefd[0]); - } - - int status; - waitpid(pid, &status, 0); - return WEXITSTATUS(status); -} diff --git a/spawn.h b/spawn.h @@ -1,5 +0,0 @@ -#define EXEC_REPLACE 1 -#define EXEC_SHELL 2 -#define EXEC_CAPTURE 4 - -int x_spawn(const char *cmd, char *const argv[], int flags, char *out_buf, size_t out_sz);