find: Add spawn helper function

This commit is contained in:
Michael Forney 2021-09-10 22:43:54 -07:00
parent 7d60e2cabb
commit c331811c91

70
find.c
View File

@ -110,6 +110,9 @@ struct findhist {
ino_t ino; ino_t ino;
}; };
/* Utility */
static int spawn(char *argv[]);
/* Primaries */ /* Primaries */
static int pri_name (struct arg *arg); static int pri_name (struct arg *arg);
static int pri_path (struct arg *arg); static int pri_path (struct arg *arg);
@ -223,6 +226,29 @@ static struct {
char print; /* whether we will need -print when parsing */ char print; /* whether we will need -print when parsing */
} gflags; } 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 * Primaries
*/ */
@ -352,7 +378,6 @@ pri_exec(struct arg *arg)
{ {
int status; int status;
size_t len; size_t len;
pid_t pid;
char **sp, ***brace; char **sp, ***brace;
struct execarg *e = arg->extra.p; 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) { if (len + e->u.p.arglen + e->u.p.filelen + envlen > argmax) {
e->argv[e->u.p.next] = NULL; e->argv[e->u.p.next] = NULL;
switch((pid = fork())) { status = spawn(e->argv);
case -1:
eprintf("fork:");
case 0:
execvp(*e->argv, e->argv);
weprintf("exec %s failed:", *e->argv);
_exit(1);
}
waitpid(pid, &status, 0);
gflags.ret = gflags.ret || status; gflags.ret = gflags.ret || status;
for (sp = e->argv + e->u.p.first; *sp; sp++) 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++) for (brace = e->u.s.braces; *brace; brace++)
**brace = arg->path; **brace = arg->path;
switch((pid = fork())) { status = spawn(e->argv);
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);
return !!status; return !!status;
} }
} }
@ -412,7 +420,6 @@ static int
pri_ok(struct arg *arg) pri_ok(struct arg *arg)
{ {
int status, reply; int status, reply;
pid_t pid;
char ***brace, c; char ***brace, c;
struct okarg *o = arg->extra.p; struct okarg *o = arg->extra.p;
@ -435,15 +442,7 @@ pri_ok(struct arg *arg)
for (brace = o->braces; *brace; brace++) for (brace = o->braces; *brace; brace++)
**brace = arg->path; **brace = arg->path;
switch((pid = fork())) { status = spawn(o->argv);
case -1:
eprintf("fork:");
case 0:
execvp(*o->argv, o->argv);
weprintf("exec %s failed:", *o->argv);
_exit(1);
}
waitpid(pid, &status, 0);
return !!status; return !!status;
} }
@ -687,7 +686,6 @@ static void
free_exec_arg(union extra extra) free_exec_arg(union extra extra)
{ {
int status; int status;
pid_t pid;
char **arg; char **arg;
struct execarg *e = extra.p; struct execarg *e = extra.p;
@ -698,15 +696,7 @@ free_exec_arg(union extra extra)
/* if we have files, do the last exec */ /* if we have files, do the last exec */
if (e->u.p.first != e->u.p.next) { if (e->u.p.first != e->u.p.next) {
switch((pid = fork())) { status = spawn(e->argv);
case -1:
eprintf("fork:");
case 0:
execvp(*e->argv, e->argv);
weprintf("exec %s failed:", *e->argv);
_exit(1);
}
waitpid(pid, &status, 0);
gflags.ret = gflags.ret || status; gflags.ret = gflags.ret || status;
} }
for (arg = e->argv + e->u.p.first; *arg; arg++) for (arg = e->argv + e->u.p.first; *arg; arg++)