commit c331811c91d9141a1b6c5273115517185d39df60
Author:     Michael Forney <[email protected]>
AuthorDate: Fri Sep 10 22:43:54 2021 -0700
Commit:     Michael Forney <[email protected]>
CommitDate: Fri Sep 10 22:43:54 2021 -0700

    find: Add spawn helper function

diff --git a/find.c b/find.c
index 82b4f87..a8404fe 100644
--- 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);
@@ -223,6 +226,29 @@ static struct {
        char print; /* whether we will need -print when parsing           */
 } 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
  */
@@ -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++)

Reply via email to