]> granicus.if.org Git - php/commitdiff
Add optind param to getopt
authorAdam Saponara <saponara@gmail.com>
Sun, 29 May 2016 17:44:16 +0000 (13:44 -0400)
committerJulien Pauli <jpauli@php.net>
Fri, 8 Jul 2016 14:53:24 +0000 (16:53 +0200)
ext/standard/basic_functions.c
ext/standard/tests/general_functions/getopt_006.phpt [new file with mode: 0644]
ext/standard/tests/general_functions/getopt_007.phpt [new file with mode: 0644]
ext/standard/tests/general_functions/getopt_008.phpt [new file with mode: 0644]
ext/standard/tests/general_functions/getopt_009.phpt [new file with mode: 0644]

index 20f03bb67f8dfed7fd10ada0e9965c476c4d5f0d..7fa1ad4bfc1b9d585b57284ec309f536c088bc71 100644 (file)
@@ -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 (file)
index 0000000..875c404
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+getopt#006 (optind #1)
+--ARGS--
+-a 1 -b 2 test
+--INI--
+register_argc_argv=On
+variables_order=GPS
+--FILE--
+<?php
+    $optind = null;
+    getopt('a:b:', [], $optind);
+    var_dump($optind);
+?>
+--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 (file)
index 0000000..157a86e
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+getopt#007 (optind #2)
+--ARGS--
+-a 1 -b 2 -- test
+--INI--
+register_argc_argv=On
+variables_order=GPS
+--FILE--
+<?php
+    $optind = null;
+    getopt('a:b:', [], $optind);
+    var_dump($optind);
+?>
+--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 (file)
index 0000000..c6bacf4
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+getopt#008 (optind #3)
+--ARGS--
+-a 1 -b 2
+--INI--
+register_argc_argv=On
+variables_order=GPS
+--FILE--
+<?php
+    $optind = null;
+    getopt('a:b:', [], $optind);
+    var_dump($optind);
+?>
+--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 (file)
index 0000000..26da1cc
--- /dev/null
@@ -0,0 +1,15 @@
+--TEST--
+getopt#009 (optind #4)
+--ARGS--
+messup -a 1 -b 2
+--INI--
+register_argc_argv=On
+variables_order=GPS
+--FILE--
+<?php
+    $optind = null;
+    getopt('a:b:', [], $optind);
+    var_dump($optind);
+?>
+--EXPECT--
+int(1)