sander Sun Oct 6 13:03:28 2002 EDT
Modified files: (Branch: PHP_4_3)
/php4/ext/standard basic_functions.c
Log:
MFH
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.523
php4/ext/standard/basic_functions.c:1.523.2.1
--- php4/ext/standard/basic_functions.c:1.523 Fri Oct 4 13:17:01 2002
+++ php4/ext/standard/basic_functions.c Sun Oct 6 13:03:26 2002
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.523 2002/10/04 17:17:01 helly Exp $ */
+/* $Id: basic_functions.c,v 1.523.2.1 2002/10/06 17:03:26 sander Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -1371,8 +1371,8 @@
PHP_FUNCTION(getopt)
{
char *options = NULL, **argv = NULL;
- char opt[1] = { '\0' };
- int argc = 0, options_len = 0;
+ char opt[2] = { '\0' };
+ int argc = 0, options_len = 0, o;
zval *val, **args = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
@@ -1392,8 +1392,11 @@
argc = zend_hash_num_elements(Z_ARRVAL_PP(args));
- /* Attempt to allocate enough memory to hold all of the arguments. */
- if ((argv = (char **) emalloc(argc * sizeof(char *))) == NULL) {
+ /*
+ * Attempt to allocate enough memory to hold all of the arguments
+ * and a trailing NULL
+ */
+ if ((argv = (char **) emalloc((argc + 1) * sizeof(char *))) == NULL) {
RETURN_FALSE;
}
@@ -1406,6 +1409,15 @@
argv[pos++] = estrdup(Z_STRVAL_PP(arg));
zend_hash_move_forward(Z_ARRVAL_PP(args));
}
+
+ /*
+ * The C Standard requires argv[argc] to be NULL - this might
+ * keep some getopt implementations happy.
+ */
+ argv[argc] = NULL;
+ } else {
+ /* Return false if we can't find argv. */
+ RETURN_FALSE;
}
/* Initialize the return value as an array. */
@@ -1417,25 +1429,25 @@
opterr = 0;
/* Invoke getopt(3) on the argument array. */
- while (getopt(argc, argv, options) != -1) {
+ while ((o = getopt(argc, argv, options)) != -1) {
/* Skip unknown arguments. */
- if (optopt == '?') {
+ if (o == '?') {
continue;
}
/* Prepare the option character and the argument string. */
- opt[0] = optopt;
+ opt[0] = o;
MAKE_STD_ZVAL(val);
if (optarg != NULL) {
ZVAL_STRING(val, optarg, 1);
} else {
- ZVAL_NULL(val);
+ ZVAL_FALSE(val);
}
/* Add this option / argument pair to the result hash. */
- if (zend_hash_add(HASH_OF(return_value), opt, 1, (void *)&val,
+ if (zend_hash_add(HASH_OF(return_value), opt, sizeof(opt), (void
+*)&val,
sizeof(zval *), NULL) ==
FAILURE) {
free_argv(argv, argc);
RETURN_FALSE;
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php