]> diplodocus.org Git - nmh/blobdiff - sbr/arglist.c
Commit ddf3a8574f65 is a fix to commit af586ebe59b7.
[nmh] / sbr / arglist.c
index e6f079e46c50fc90f84999c19c2661cf1cee897b..f771155a440dbd444f35089e1e410a687fa3962a 100644 (file)
@@ -1,14 +1,16 @@
-
-/*
- * arglist.c -- Routines for handling argument lists for execvp() and friends
+/* arglist.c -- Routines for handling argument lists for execvp() and friends
  *
  * This code is Copyright (c) 2013, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/utils.h>
+#include "h/mh.h"
+#include "r1bindex.h"
+#include "brkstring.h"
+#include "error.h"
+#include "arglist.h"
+#include "h/utils.h"
 
 /*
  * Split up a command into an appropriate array to pass to execvp()
@@ -41,25 +43,27 @@ char **
 argsplit(char *command, char **file, int *argp)
 {
     char **argvarray, *p;
-    int space = 0, metachar = 0, i;
+    int i;
 
+    bool space = false;
+    bool metachar = false;
     for (p = command; *p; p++) {
-       if (*p == ' ' || *p == '\t') {
-               space = 1;
+       if (*p == ' ' || *p == '\t') {
+               space = true;
        } else if (strchr(METACHARS, *p)) {
-               metachar = 1;
+               metachar = true;
                break;
        }
     }
 
-    argvarray = (char **) mh_xmalloc((sizeof(char **) * (MAXARGS + 5)));
+    argvarray = mh_xmalloc(sizeof *argvarray * (MAXARGS + 5));
 
     /*
      * The simple case - no spaces or shell metacharacters
      */
 
     if (!space && !metachar) {
-       argvarray[0] = getcpy(r1bindex(command, '/'));
+       argvarray[0] = mh_xstrdup(r1bindex(command, '/'));
        argvarray[1] = NULL;
        *file = mh_xstrdup(command);
        if (argp)
@@ -73,16 +77,16 @@ argsplit(char *command, char **file, int *argp)
      */
 
     if (space && !metachar) {
-       char **split;
+       char **split;
        p = mh_xstrdup(command);
        split = brkstring(p, " \t", NULL);
        if (split[0] == NULL) {
-           adios(NULL, "Invalid blank command found");
+           die("Invalid blank command found");
        }
        argvarray[0] = mh_xstrdup(r1bindex(split[0], '/'));
        for (i = 1; split[i] != NULL; i++) {
            if (i > MAXARGS) {
-               adios(NULL, "Command exceeded argument limit");
+               die("Command exceeded argument limit");
            }
            argvarray[i] = mh_xstrdup(split[i]);
        }
@@ -119,7 +123,7 @@ argsplit(char *command, char **file, int *argp)
     argvarray[4] = NULL;
 
     if (argp)
-       *argp = 4;
+       *argp = 4;
 
     return argvarray;
 }
@@ -133,10 +137,10 @@ arglist_free(char *command, char **argvarray)
 {
     int i;
 
-    mh_xfree(command);
+    free(command);
 
     if (argvarray != NULL) {
-       for (i = 0; argvarray[i] != NULL; i++)
+       for (i = 0; argvarray[i] != NULL; i++)
            free(argvarray[i]);
        free(argvarray);
     }
@@ -168,7 +172,7 @@ argsplit_msgarg(struct msgs_array *msgs, char *command, char **program)
      */
 
     for (i = 0; i < argp; i++) {
-       app_msgarg(msgs, vec[i]);
+       app_msgarg(msgs, vec[i]);
     }
 
     free(vec);
@@ -201,12 +205,12 @@ argsplit_insert(struct msgs_array *msgs, char *command, char **program)
      */
 
     if (msgs->size + argp >= msgs->max) {
-       msgs->max += MAXMSGS > argp ? MAXMSGS : argp;
-       msgs->msgs = mh_xrealloc(msgs->msgs, msgs->max * sizeof(*msgs->msgs));
+        msgs->max += max(MAXMSGS, argp);
+        msgs->msgs = mh_xrealloc(msgs->msgs, msgs->max * sizeof(*msgs->msgs));
     }
 
     for (i = msgs->size - 1; i >= 0; i--)
-       msgs->msgs[i + argp] = msgs->msgs[i];
+       msgs->msgs[i + argp] = msgs->msgs[i];
 
     msgs->size += argp;
 
@@ -215,7 +219,7 @@ argsplit_insert(struct msgs_array *msgs, char *command, char **program)
      */
 
     for (i = 0; i < argp; i++)
-       msgs->msgs[i] = vec[i];
+       msgs->msgs[i] = vec[i];
 
     free(vec);
 }