From e4f7f00a7748b1312f6486639c158be887f23e18 Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sun, 29 May 2016 13:44:16 -0400 Subject: [PATCH] Add optind param to getopt --- ext/standard/basic_functions.c | 17 +++++++++++++++-- .../tests/general_functions/getopt_006.phpt | 15 +++++++++++++++ .../tests/general_functions/getopt_007.phpt | 15 +++++++++++++++ .../tests/general_functions/getopt_008.phpt | 15 +++++++++++++++ .../tests/general_functions/getopt_009.phpt | 15 +++++++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 ext/standard/tests/general_functions/getopt_006.phpt create mode 100644 ext/standard/tests/general_functions/getopt_007.phpt create mode 100644 ext/standard/tests/general_functions/getopt_008.phpt create mode 100644 ext/standard/tests/general_functions/getopt_009.phpt diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 20f03bb67f..7fa1ad4bfc 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -646,6 +646,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_getopt, 0, 0, 1) ZEND_ARG_INFO(0, options) ZEND_ARG_INFO(0, opts) /* ARRAY_INFO(0, opts, 1) */ + ZEND_ARG_INFO(1, optind) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_flush, 0) @@ -4269,7 +4270,7 @@ static int parse_opts(char * opts, opt_struct ** result) } /* }}} */ -/* {{{ proto array getopt(string options [, array longopts]) +/* {{{ proto array getopt(string options [, array longopts [, int &optind]]) Get options from the command line argument list */ PHP_FUNCTION(getopt) { @@ -4281,13 +4282,20 @@ PHP_FUNCTION(getopt) char *php_optarg = NULL; int php_optind = 1; zval val, *args = NULL, *p_longopts = NULL; + zval *zoptind = NULL; int optname_len = 0; opt_struct *opts, *orig_opts; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a", &options, &options_len, &p_longopts) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|az/", &options, &options_len, &p_longopts, &zoptind) == FAILURE) { RETURN_FALSE; } + /* Init zoptind to 1 */ + if (zoptind) { + zval_dtor(zoptind); + ZVAL_LONG(zoptind, 1); + } + /* Get argv from the global symbol table. We calculate argc ourselves * in order to be on the safe side, even though it is also available * from the symbol table. */ @@ -4429,6 +4437,11 @@ PHP_FUNCTION(getopt) php_optarg = NULL; } + /* Set zoptind to php_optind */ + if (zoptind) { + ZVAL_LONG(zoptind, php_optind); + } + free_longopts(orig_opts); efree(orig_opts); free_argv(argv, argc); diff --git a/ext/standard/tests/general_functions/getopt_006.phpt b/ext/standard/tests/general_functions/getopt_006.phpt new file mode 100644 index 0000000000..875c404921 --- /dev/null +++ b/ext/standard/tests/general_functions/getopt_006.phpt @@ -0,0 +1,15 @@ +--TEST-- +getopt#006 (optind #1) +--ARGS-- +-a 1 -b 2 test +--INI-- +register_argc_argv=On +variables_order=GPS +--FILE-- + +--EXPECT-- +int(5) diff --git a/ext/standard/tests/general_functions/getopt_007.phpt b/ext/standard/tests/general_functions/getopt_007.phpt new file mode 100644 index 0000000000..157a86e70b --- /dev/null +++ b/ext/standard/tests/general_functions/getopt_007.phpt @@ -0,0 +1,15 @@ +--TEST-- +getopt#007 (optind #2) +--ARGS-- +-a 1 -b 2 -- test +--INI-- +register_argc_argv=On +variables_order=GPS +--FILE-- + +--EXPECT-- +int(6) diff --git a/ext/standard/tests/general_functions/getopt_008.phpt b/ext/standard/tests/general_functions/getopt_008.phpt new file mode 100644 index 0000000000..c6bacf4e56 --- /dev/null +++ b/ext/standard/tests/general_functions/getopt_008.phpt @@ -0,0 +1,15 @@ +--TEST-- +getopt#008 (optind #3) +--ARGS-- +-a 1 -b 2 +--INI-- +register_argc_argv=On +variables_order=GPS +--FILE-- + +--EXPECT-- +int(5) diff --git a/ext/standard/tests/general_functions/getopt_009.phpt b/ext/standard/tests/general_functions/getopt_009.phpt new file mode 100644 index 0000000000..26da1cc3ef --- /dev/null +++ b/ext/standard/tests/general_functions/getopt_009.phpt @@ -0,0 +1,15 @@ +--TEST-- +getopt#009 (optind #4) +--ARGS-- +messup -a 1 -b 2 +--INI-- +register_argc_argv=On +variables_order=GPS +--FILE-- + +--EXPECT-- +int(1) -- 2.50.1