jon Sun Sep 29 23:02:52 2002 EDT Modified files: /php4/ext/standard basic_functions.c basic_functions.h Log: @ - Added getopt() for parsing command line options and arguments. (Jon) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.515 php4/ext/standard/basic_functions.c:1.516 --- php4/ext/standard/basic_functions.c:1.515 Sat Sep 28 18:14:20 2002 +++ php4/ext/standard/basic_functions.c Sun Sep 29 23:02:51 2002 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.515 2002/09/28 22:14:20 wez Exp $ */ +/* $Id: basic_functions.c,v 1.516 2002/09/30 03:02:51 jon Exp $ */ #include "php.h" #include "php_streams.h" @@ -498,6 +498,10 @@ PHP_FE(putenv, NULL) #endif +#ifdef HAVE_GETOPT + PHP_FE(getopt, + NULL) +#endif + PHP_FE(microtime, NULL) PHP_FE(gettimeofday, NULL) @@ -1337,6 +1341,105 @@ RETURN_FALSE; } } +} +/* }}} */ +#endif + +#ifdef HAVE_GETOPT +/* {{{ free_argv + Free the memory allocated to an argv array. */ +static void free_argv(char **argv, int argc) +{ + int i; + + if (argv) { + for (i = 0; i < argc; i++) { + if (argv[i]) { + efree(argv[i]); + } + } + efree(argv); + } +} +/* }}} */ + +/* {{{ proto array getopt(string options) + Get options from the command line argument list */ +PHP_FUNCTION(getopt) +{ + char *options = NULL, **argv = NULL; + char opt[1] = { '\0' }; + int argc = 0, options_len = 0; + zval *val, **args = NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", + &options, &options_len) == +FAILURE) { + return; + } + + /* + * 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. + */ + if (zend_hash_find(&EG(symbol_table), "argv", sizeof("argv"), + (void **) &args) != FAILURE) { + int pos = 0; + zval **arg; + + 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) { + RETURN_FALSE; + } + + /* Reset the array indexes. */ + zend_hash_internal_pointer_reset(Z_ARRVAL_PP(args)); + + /* Iterate over the hash to construct the argv array. */ + while (zend_hash_get_current_data(Z_ARRVAL_PP(args), + +(void **)&arg) == SUCCESS) { + argv[pos++] = estrdup(Z_STRVAL_PP(arg)); + zend_hash_move_forward(Z_ARRVAL_PP(args)); + } + } + + /* Initialize the return value as an array. */ + if (array_init(return_value)) { + RETURN_FALSE; + } + + /* Disable getopt()'s error messages. */ + opterr = 0; + + /* Invoke getopt(3) on the argument array. */ + while (getopt(argc, argv, options) != -1) { + + /* Skip unknown arguments. */ + if (optopt == '?') { + continue; + } + + /* Prepare the option character and the argument string. */ + opt[0] = optopt; + + MAKE_STD_ZVAL(val); + if (optarg != NULL) { + ZVAL_STRING(val, optarg, 1); + } else { + ZVAL_NULL(val); + } + + /* Add this option / argument pair to the result hash. */ + if (zend_hash_add(HASH_OF(return_value), opt, 1, (void *)&val, + sizeof(zval *), NULL) == +FAILURE) { + free_argv(argv, argc); + RETURN_FALSE; + } + } + + free_argv(argv, argc); } /* }}} */ #endif Index: php4/ext/standard/basic_functions.h diff -u php4/ext/standard/basic_functions.h:1.107 php4/ext/standard/basic_functions.h:1.108 --- php4/ext/standard/basic_functions.h:1.107 Mon Sep 23 14:12:38 2002 +++ php4/ext/standard/basic_functions.h Sun Sep 29 23:02:52 2002 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.h,v 1.107 2002/09/23 18:12:38 wez Exp $ */ +/* $Id: basic_functions.h,v 1.108 2002/09/30 03:02:52 jon Exp $ */ #ifndef BASIC_FUNCTIONS_H #define BASIC_FUNCTIONS_H @@ -49,6 +49,8 @@ /* system functions */ PHP_FUNCTION(getenv); PHP_FUNCTION(putenv); + +PHP_FUNCTION(getopt); PHP_FUNCTION(get_current_user); PHP_FUNCTION(set_time_limit);
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php