freebsd-dev/bin/sh/tests/builtins/getopts9.0
Jilles Tjoelker 1bc2fdfabf sh: Make getopts memory-safe if with changing arguments.
POSIX does not permit to continuing a getopts loop with different
arguments. For parsing the positional parameters, we handle this case by
resetting the getopts state when the positional parameters are changed in
any way (and the getopts state is local to a function). However, in the
syntax getopts <optstring> <var> <arg...>, changes could lead to invalid
memory access.

In the syntax getopts <optstring> <var> <arg...>, store a copy of the
arguments and continue to use them until getopts is reset.
2014-10-26 17:50:33 +00:00

10 lines
146 B
Plaintext

# $FreeBSD$
args='-ab'
getopts ab opt $args
echo $?:$opt:$OPTARG
for dummy in dummy1 dummy2; do
getopts ab opt $args
echo $?:$opt:$OPTARG
done