Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 10 May 2014 17:42:21 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r265844 - in head/bin/sh: . tests/builtins
Message-ID:  <201405101742.s4AHgL9g046801@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 ]



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201405101742.s4AHgL9g046801>