On Sun, 11 Jan 2015, Ted Unangst wrote:
> Even more awesome.

How about enforcing the full rule?

Index: env.c
===================================================================
RCS file: /cvs/src/usr.bin/env/env.c,v
retrieving revision 1.15
diff -u -p -r1.15 env.c
--- env.c       8 Mar 2014 00:09:20 -0000       1.15
+++ env.c       11 Jan 2015 21:52:32 -0000
@@ -29,6 +29,7 @@
  * SUCH DAMAGE.
  */
 
+#include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <locale.h>
@@ -39,12 +40,22 @@
 
 __dead void usage(void);
 
+/*
+ * POSIX XBD definition of 'name':
+ *     In the shell command language, a word consisting solely of
+ *     underscores, digits, and alphabetics from the portable character
+ *     set. The first character of a name is not a digit.
+ */
+static const char varchar[] =
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
+
 int
 main(int argc, char *argv[])
 {
        extern char **environ;
        extern int optind;
        char **ep, *p;
+       size_t n;
        int ch;
 
        setlocale(LC_ALL, "");
@@ -62,9 +73,14 @@ main(int argc, char *argv[])
        argc -= optind;
        argv += optind;
 
-       for (; *argv && (p = strchr(*argv, '=')); ++argv) {
-               *p++ = '\0';
-               if (setenv(*argv, p, 1) == -1) {
+       for (; p = *argv; ++argv) {
+               /* is the argument an assignment to a legal name? */
+               if (isdigit((unsigned char)*p) ||
+                   (n = strspn(p, varchar)) == 0 ||
+                   p[n] != '=')
+                       break;
+               p[n] = '\0';
+               if (setenv(p, p + n + 1, 1) == -1) {
                        /* reuse 126, it matches the problem most */
                        err(126, "setenv");
                }
Index: env.1
===================================================================
RCS file: /cvs/src/usr.bin/env/env.1,v
retrieving revision 1.19
diff -u -p -r1.19 env.1
--- env.1       8 Mar 2014 01:42:17 -0000       1.19
+++ env.1       11 Jan 2015 21:52:32 -0000
@@ -68,6 +68,16 @@ Causes
 to completely ignore the environment it inherits.
 .El
 .Pp
+To be treated as an assignment, the
+.Ar name
+before the equals-sign must consist only of underscores, digits, and letters,
+and must not start with a digit.
+The first argument to
+.Nm
+that doesn't meet that criteria will be considered the
+.Ar utility
+to execute.
+.Pp
 If no
 .Ar utility
 is specified,
@@ -119,9 +129,3 @@ specification.
 The historic
 .Fl
 option has been deprecated but is still supported in this implementation.
-.Sh BUGS
-.Nm
-doesn't handle commands with equal
-.Pq Sq =
-signs in their
-names, for obvious reasons.

Reply via email to