svn commit: r265844 - in head/bin/sh: . tests/builtins

Jilles Tjoelker jilles at FreeBSD.org
Sat May 10 17:42:22 UTC 2014


Author: jilles
Date: Sat May 10 17:42:21 2014
New Revision: 265844
URL: http://svnweb.freebsd.org/changeset/base/265844

Log:
  sh: Don't discard getopts state on unknown option or missing argument.
  
  When getopts finds an invalid option or a missing option-argument, it should
  not reset its state and should set OPTIND as normal. This is an old ash bug
  that was fixed long ago in dash. Our behaviour now matches most other
  shells.

Added:
  head/bin/sh/tests/builtins/getopts6.0   (contents, props changed)
  head/bin/sh/tests/builtins/getopts7.0   (contents, props changed)
Modified:
  head/bin/sh/options.c

Modified: head/bin/sh/options.c
==============================================================================
--- head/bin/sh/options.c	Sat May 10 17:03:33 2014	(r265843)
+++ head/bin/sh/options.c	Sat May 10 17:42:21 2014	(r265844)
@@ -480,7 +480,7 @@ atend:
 				INTON;
 			}
 			c = '?';
-			goto bad;
+			goto out;
 		}
 		if (*++q == ':')
 			q++;
@@ -501,7 +501,7 @@ atend:
 				INTON;
 				c = '?';
 			}
-			goto bad;
+			goto out;
 		}
 
 		if (p == **optnext)
@@ -511,14 +511,10 @@ atend:
 	}
 	else
 		setvarsafe("OPTARG", "", 0);
-	ind = *optnext - optfirst + 1;
-	goto out;
 
-bad:
-	ind = 1;
-	*optnext = NULL;
-	p = NULL;
 out:
+	if (*optnext != NULL)
+		ind = *optnext - optfirst + 1;
 	*optptr = p;
 	fmtstr(s, sizeof(s), "%d", ind);
 	err |= setvarsafe("OPTIND", s, VNOFUNC);

Added: head/bin/sh/tests/builtins/getopts6.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/bin/sh/tests/builtins/getopts6.0	Sat May 10 17:42:21 2014	(r265844)
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+set -- -x -y
+getopts :x var || echo "First getopts bad: $?"
+getopts :x var
+r=$?
+[ r != 0 ] && [ "$OPTIND" = 3 ]

Added: head/bin/sh/tests/builtins/getopts7.0
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/bin/sh/tests/builtins/getopts7.0	Sat May 10 17:42:21 2014	(r265844)
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+set -- -x
+getopts :x: var
+r=$?
+[ r != 0 ] && [ "$OPTIND" = 2 ]


More information about the svn-src-all mailing list