sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit c331811c91d9141a1b6c5273115517185d39df60
parent 7d60e2cabbe07495ee74f3d5e705f1e3470148eb
Author: Michael Forney <mforney@mforney.org>
Date:   Fri, 10 Sep 2021 22:43:54 -0700

find: Add spawn helper function

Diffstat:
Mfind.c | 70++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 30 insertions(+), 40 deletions(-)

diff --git a/find.c b/find.c @@ -110,6 +110,9 @@ struct findhist { ino_t ino; }; +/* Utility */ +static int spawn(char *argv[]); + /* Primaries */ static int pri_name (struct arg *arg); static int pri_path (struct arg *arg); @@ -224,6 +227,29 @@ static struct { } gflags; /* + * Utility + */ +static int +spawn(char *argv[]) +{ + pid_t pid; + int status; + + switch((pid = fork())) { + case -1: + eprintf("fork:"); + case 0: + execvp(*argv, argv); + weprintf("exec %s failed:", *argv); + _exit(1); + } + + /* FIXME: proper course of action for waitpid() on EINTR? */ + waitpid(pid, &status, 0); + return status; +} + +/* * Primaries */ static int @@ -352,7 +378,6 @@ pri_exec(struct arg *arg) { int status; size_t len; - pid_t pid; char **sp, ***brace; struct execarg *e = arg->extra.p; @@ -363,15 +388,7 @@ pri_exec(struct arg *arg) if (len + e->u.p.arglen + e->u.p.filelen + envlen > argmax) { e->argv[e->u.p.next] = NULL; - switch((pid = fork())) { - case -1: - eprintf("fork:"); - case 0: - execvp(*e->argv, e->argv); - weprintf("exec %s failed:", *e->argv); - _exit(1); - } - waitpid(pid, &status, 0); + status = spawn(e->argv); gflags.ret = gflags.ret || status; for (sp = e->argv + e->u.p.first; *sp; sp++) @@ -394,16 +411,7 @@ pri_exec(struct arg *arg) for (brace = e->u.s.braces; *brace; brace++) **brace = arg->path; - switch((pid = fork())) { - case -1: - eprintf("fork:"); - case 0: - execvp(*e->argv, e->argv); - weprintf("exec %s failed:", *e->argv); - _exit(1); - } - /* FIXME: proper course of action for all waitpid() on EINTR? */ - waitpid(pid, &status, 0); + status = spawn(e->argv); return !!status; } } @@ -412,7 +420,6 @@ static int pri_ok(struct arg *arg) { int status, reply; - pid_t pid; char ***brace, c; struct okarg *o = arg->extra.p; @@ -435,15 +442,7 @@ pri_ok(struct arg *arg) for (brace = o->braces; *brace; brace++) **brace = arg->path; - switch((pid = fork())) { - case -1: - eprintf("fork:"); - case 0: - execvp(*o->argv, o->argv); - weprintf("exec %s failed:", *o->argv); - _exit(1); - } - waitpid(pid, &status, 0); + status = spawn(o->argv); return !!status; } @@ -687,7 +686,6 @@ static void free_exec_arg(union extra extra) { int status; - pid_t pid; char **arg; struct execarg *e = extra.p; @@ -698,15 +696,7 @@ free_exec_arg(union extra extra) /* if we have files, do the last exec */ if (e->u.p.first != e->u.p.next) { - switch((pid = fork())) { - case -1: - eprintf("fork:"); - case 0: - execvp(*e->argv, e->argv); - weprintf("exec %s failed:", *e->argv); - _exit(1); - } - waitpid(pid, &status, 0); + status = spawn(e->argv); gflags.ret = gflags.ret || status; } for (arg = e->argv + e->u.p.first; *arg; arg++)