2017-08-11 20:30:39 +05:30
|
|
|
# This test can fail with libc with buggy getopt() implementation.
|
|
|
|
# If getopt() wants to parse multi-option args (-abc),
|
2017-08-13 06:29:00 +05:30
|
|
|
# it needs to remember a position within current arg.
|
2017-08-11 20:30:39 +05:30
|
|
|
#
|
|
|
|
# If this position is kept as a POINTER, not an offset,
|
|
|
|
# and if argv[] ADDRESSES (not contents!) change, it blows up.
|
|
|
|
|
2017-08-13 06:29:00 +05:30
|
|
|
(
|
|
|
|
|
2017-08-11 20:30:39 +05:30
|
|
|
echo "*** optstring:'ac' args:-a -b -c -d e"
|
|
|
|
getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
|
|
|
|
# Above: args are (usually) in the same locations in memory.
|
|
|
|
# Below: variable allocations change the location.
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "*** optstring:'ac' args:-a -b -c -d e"
|
|
|
|
unset OPTIND
|
|
|
|
OPTARG=QWERTY; getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
NEWVAR=NEWVAL; getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
VAR111=NEWVAL; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
VAR222=NEWVAL; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
VAR333=NEWVAL; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
|
2017-08-13 06:29:00 +05:30
|
|
|
# Slightly different attempts to force reallocations
|
2017-08-11 20:30:39 +05:30
|
|
|
|
|
|
|
echo
|
|
|
|
echo "*** optstring:'ac' args:-a -b -c -d e"
|
|
|
|
unset OPTIND
|
|
|
|
export OPTARG; getopts "ac" var -a -b -c -d e; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
export NEWVAR; getopts "ac" var -a -b -c -d e; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
export VAR111; getopts "ac" var -a -b -c -d e; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
export VAR222; getopts "ac" var -a -b -c -d e; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
export VAR333; getopts "ac" var -a -b -c -d e; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'"
|
|
|
|
|
|
|
|
# All copies of code above should generate identical output
|
2017-08-13 06:29:00 +05:30
|
|
|
|
|
|
|
) 2>&1 \
|
|
|
|
| sed -e 's/ unrecognized option: / invalid option -- /' \
|
|
|
|
-e 's/ illegal option -- / invalid option -- /' \
|