From: Matthew Dillon Date: Tue, 17 Jan 2012 02:41:24 +0000 (-0800) Subject: varsym - Add -x option X-Git-Tag: v3.0.0~46 X-Git-Url: https://gitweb.dragonflybsd.org/~tuxillo/dragonfly.git/commitdiff_plain/2f5c5b87d512dff27a8ab2b5bddb25a6a1c488a1 varsym - Add -x option * Add the -x option which causes varsym to set per-process variables and then exec the command line from the first non-varsym-assign. For example: varsym -x UNAME_n=fubar uname varsym -x UNAME_n=fubar /bin/csh etc --- diff --git a/bin/varsym/varsym.1 b/bin/varsym/varsym.1 index ed5fbb3b31..ee61ca6dd7 100644 --- a/bin/varsym/varsym.1 +++ b/bin/varsym/varsym.1 @@ -32,11 +32,19 @@ .Nd get and set user and system-wide variables for variant symlinks .Sh SYNOPSIS .Nm -.Op Fl adpqsu +.Op Fl adpqsux .Oo .Ar var Ns Op Ns Cm = Ns Ar data .Ar ... .Oc +.Nm +.Fl x +.Oo +.Ar var Ns Op Ns Cm = Ns Ar data +.Ar ... +.Oc +.Ar command +.Ar args .Sh DESCRIPTION The .Nm @@ -66,6 +74,9 @@ Note that since .Nm is run as its own process, using this option to set a variable will not affect your shell's namespace. +Instead you might want to use the +.Fl x +option to set local varsyms and exec a command. .It Fl q Quiet mode. When retrieving a variable only its data is printed. @@ -75,7 +86,14 @@ to system-specific variables. .It Fl u This option causes variables to be set on a per-user-id basis and restricts retrievals to user-specific variables. -This is the default. +This is the default unless +.Fl x +is used. +.It Fl x +This option causes variables to be set only within the +.Nm +process and its children, and also allows you to specify a command and +arguments to exec after the var=data specifications. .El .Sh DIAGNOSTICS The diff --git a/bin/varsym/varsym.c b/bin/varsym/varsym.c index 5ae3a0cdef..802e67cf38 100644 --- a/bin/varsym/varsym.c +++ b/bin/varsym/varsym.c @@ -33,8 +33,9 @@ #include #include -static void usage(void); static void dumpvars(char *buf, int bytes); +static int doexec(char **av); +static void usage(void); int main(int ac, char **av) @@ -45,8 +46,9 @@ main(int ac, char **av) int deleteOpt = 0; int verboseOpt = 1; int allOpt = 0; + int execok = 0; - while ((i = getopt(ac, av, "adhpqsu")) != -1) { + while ((i = getopt(ac, av, "adhpqsux")) != -1) { switch (i) { case 'a': allOpt = 1; @@ -69,6 +71,11 @@ main(int ac, char **av) mask = VARSYM_USER_MASK; level = VARSYM_USER; break; + case 'x': + mask = VARSYM_PROC_MASK; + level = VARSYM_PROC; + execok = 1; + break; case 'h': default: usage(); @@ -102,13 +109,21 @@ main(int ac, char **av) if (data) *data++ = 0; - if (deleteOpt) { + if (execok) { + if (deleteOpt) { + usage(); + exit(1); + } + if (data) { + error = varsym_set(level, name, data); + } else { + error = doexec(av + optind); + } + } else if (deleteOpt) { error = varsym_set(level, name, NULL); - } - else if (data) { + } else if (data) { error = varsym_set(level, name, data); - } - else { + } else { error = varsym_get(mask, name, buf, sizeof(buf)); if (error >= 0 && error <= (int)sizeof(buf)) { if (verboseOpt) @@ -126,23 +141,32 @@ main(int ac, char **av) static void dumpvars(char *buf, int bytes) { - int b; - int i; - char *vname = NULL; - char *vdata = NULL; - - for (b = i = 0; i < bytes; ++i) { - if (buf[i] == 0) { - if (vname == NULL) { - vname = buf + b; - } else { - vdata = buf + b; - printf("%s=%s\n", vname, vdata); - vname = vdata = NULL; - } - b = i + 1; + int b; + int i; + char *vname = NULL; + char *vdata = NULL; + + for (b = i = 0; i < bytes; ++i) { + if (buf[i] == 0) { + if (vname == NULL) { + vname = buf + b; + } else { + vdata = buf + b; + printf("%s=%s\n", vname, vdata); + vname = vdata = NULL; + } + b = i + 1; + } } - } +} + +static int +doexec(char **av) +{ + int error; + + error = execvp(av[0], av); + return (error); } static void