Author: jilles
Date: Sun Nov  1 22:07:40 2015
New Revision: 290244
URL: https://svnweb.freebsd.org/changeset/base/290244

Log:
  sh: Avoid copying argv for simple commands.
  
  Add dummy entries before and after so arglist's array is directly usable as
  argv.

Modified:
  head/bin/sh/eval.c
  head/bin/sh/expand.c
  head/bin/sh/expand.h

Modified: head/bin/sh/eval.c
==============================================================================
--- head/bin/sh/eval.c  Sun Nov  1 21:59:56 2015        (r290243)
+++ head/bin/sh/eval.c  Sun Nov  1 22:07:40 2015        (r290244)
@@ -750,7 +750,7 @@ isdeclarationcmd(struct narg *arg)
 }
 
 static void
-xtracecommand(struct arglist *varlist, struct arglist *arglist)
+xtracecommand(struct arglist *varlist, int argc, char **argv)
 {
        char sep = 0;
        const char *text, *p, *ps4;
@@ -771,8 +771,8 @@ xtracecommand(struct arglist *varlist, s
                        out2qstr(text);
                sep = ' ';
        }
-       for (i = 0; i < arglist->count; i++) {
-               text = arglist->args[i];
+       for (i = 0; i < argc; i++) {
+               text = argv[i];
                if (sep != 0)
                        out2c(' ');
                out2qstr(text);
@@ -849,6 +849,8 @@ evalcommand(union node *cmd, int flags, 
        do_clearcmdentry = 0;
        oexitstatus = exitstatus;
        exitstatus = 0;
+       /* Add one slot at the beginning for tryexec(). */
+       appendarglist(&arglist, nullstr);
        for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
                if (varflag && isassignment(argp->narg.text)) {
                        expandarg(argp, varflag == 1 ? &varlist : &arglist,
@@ -858,13 +860,11 @@ evalcommand(union node *cmd, int flags, 
                        varflag = isdeclarationcmd(&argp->narg) ? 2 : 0;
                expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
        }
+       appendarglist(&arglist, nullstr);
        expredir(cmd->ncmd.redirect);
-       argc = arglist.count;
-       /* Add one slot at the beginning for tryexec(). */
-       argv = stalloc(sizeof (char *) * (argc + 2));
-       argv++;
+       argc = arglist.count - 2;
+       argv = &arglist.args[1];
 
-       memcpy(argv, arglist.args, sizeof(*argv) * argc);
        argv[argc] = NULL;
        lastarg = NULL;
        if (iflag && funcnest == 0 && argc > 0)
@@ -872,7 +872,7 @@ evalcommand(union node *cmd, int flags, 
 
        /* Print the command if xflag is set. */
        if (xflag)
-               xtracecommand(&varlist, &arglist);
+               xtracecommand(&varlist, argc, argv);
 
        /* Now locate the command. */
        if (argc == 0) {

Modified: head/bin/sh/expand.c
==============================================================================
--- head/bin/sh/expand.c        Sun Nov  1 21:59:56 2015        (r290243)
+++ head/bin/sh/expand.c        Sun Nov  1 22:07:40 2015        (r290244)
@@ -114,7 +114,6 @@ static void expmeta(char *, char *, stru
 static int expsortcmp(const void *, const void *);
 static int patmatch(const char *, const char *, int);
 static char *cvtnum(int, char *);
-static void appendarglist(struct arglist *, char *);
 static int collate_range_cmp(wchar_t, wchar_t);
 
 void
@@ -126,7 +125,7 @@ emptyarglist(struct arglist *list)
        list->capacity = sizeof(list->smallarg) / sizeof(list->smallarg[0]);
 }
 
-static void
+void
 appendarglist(struct arglist *list, char *str)
 {
        char **newargs;

Modified: head/bin/sh/expand.h
==============================================================================
--- head/bin/sh/expand.h        Sun Nov  1 21:59:56 2015        (r290243)
+++ head/bin/sh/expand.h        Sun Nov  1 22:07:40 2015        (r290244)
@@ -52,6 +52,7 @@ struct arglist {
 
 
 void emptyarglist(struct arglist *);
+void appendarglist(struct arglist *, char *);
 union node;
 void expandarg(union node *, struct arglist *, int);
 void rmescapes(char *);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to