summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2024-07-14 07:21:14 +0200
committerLouis Burda <quent.burda@gmail.com>2024-07-14 07:21:36 +0200
commit45f674aa80b6cec9c95e12ea086e06811ccbdeb2 (patch)
treebb588e81018d52afa77af85299bac216862d7be8
parent25aef9010259cf14d0b9a138cbadc13740feaff5 (diff)
downloadpipeln-45f674aa80b6cec9c95e12ea086e06811ccbdeb2.tar.gz
pipeln-45f674aa80b6cec9c95e12ea086e06811ccbdeb2.zip
Prevent re-closing input pipe file descriptorHEADmaster
-rw-r--r--Makefile8
-rw-r--r--pipeln.c55
2 files changed, 34 insertions, 29 deletions
diff --git a/Makefile b/Makefile
index 62e95b5..9c072c7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,13 @@
PREFIX ?= /usr/local
BINDIR ?= /bin
-CFLAGS = -std=c99 -Wunused-variable -Wunused-function -Wconversion -O2
+CFLAGS = -std=c99 -Wunused-variable -Wunused-function -Wconversion
+
+ifeq ($(DEBUG), 1)
+CFLAGS += -Og -g
+else
+CFLAGS += -O2
+endif
all: pipeln
diff --git a/pipeln.c b/pipeln.c
index 71f3ff2..a0995d7 100644
--- a/pipeln.c
+++ b/pipeln.c
@@ -1,12 +1,8 @@
-#include <asm-generic/errno-base.h>
-#include <linux/limits.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <wait.h>
-
#include <errno.h>
-#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
@@ -21,6 +17,7 @@ die(const char *fmt, ...)
{
va_list ap;
+ setvbuf(stderr, NULL, _IOLBF, 0);
fputs("pipeln: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
@@ -35,6 +32,13 @@ die(const char *fmt, ...)
exit(1);
}
+static void
+usage(int rc, FILE *file)
+{
+ fprintf(file, "Usage: pipeln [-h] [-R] [--] [CMD |].. CMD\n");
+ exit(rc);
+}
+
static pid_t
run(int *in, int *out, const char **argv)
{
@@ -48,15 +52,15 @@ run(int *in, int *out, const char **argv)
if (!child) {
if (in) {
if (dup2(in[0], 0) < 0)
- die("dup2:");
- close(in[0]);
- close(in[1]);
+ die("dup2 %i:", errno);
+ if (close(in[0]) < 0)
+ die("close:");
}
if (out) {
if (dup2(out[1], 1) < 0)
die("dup2:");
- close(out[0]);
- close(out[1]);
+ if (close(out[0]) < 0 || close(out[1]) < 0)
+ die("close:");
}
if (resolve_bin) {
execvp(argv[0], (char *const *) argv);
@@ -99,48 +103,43 @@ int
main(int argc, const char **argv)
{
const char **arg, **cmdargs;
- const char *invalid;
int pipe1[2], pipe2[2];
int *in, *out;
pid_t pid;
- /* first cmd from stdin */
- in = NULL;
- out = pipe2;
-
if (argc < 1) return 1;
- invalid = NULL;
- cmdargs = NULL;
for (arg = argv + 1; *arg; arg++) {
if (!strcmp(*arg, "--")) {
- cmdargs = arg;
+ arg++;
break;
- } else if (!strcmp(*arg, "-x")) {
+ } else if (!strcmp(*arg, "-h")) {
+ usage(1, stderr);
+ } else if (!strcmp(*arg, "-R")) {
resolve_bin = false;
} else {
- invalid = *arg;
+ break;
}
}
- if (!cmdargs) die("missing -- separator");
- if (invalid) die("invalid argument '%s'", invalid);
- cmdargs += 1;
- for (arg = argv + 1; ; arg++) {
+ // First command from stdin.
+ in = NULL;
+ out = pipe1;
+
+ cmdargs = arg;
+ for (; ; arg++) {
if (!*arg || !strcmp(*arg, "|")) {
- /* last cmd goes to stdout */
+ // Last command to stdout.
if (!*arg) out = NULL;
- else if (pipe(out)) die("pipe:");
+ else if (pipe(out) < 0) die("pipe:");
- /* run piped cmd */
*arg = NULL;
pid = run(in, out, cmdargs);
if (!out) waitall(pid);
cmdargs = arg + 1;
- /* out => in */
in = out;
- out = out == pipe1 ? pipe2 : pipe1;
+ out = (out == pipe1) ? pipe2 : pipe1;
} else if (!strcmp(*arg, "\\|")) {
*arg = "|";
}