diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 33247c4cb3941..c30c93a9689f6 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -727,98 +727,98 @@ PHP_FUNCTION(long2ip) * System Functions * ********************/ -/* {{{ Get the value of an environment variable or every available environment variable - if no varname is present */ -PHP_FUNCTION(getenv) -{ - char *ptr, *str = NULL; - size_t str_len; - zend_bool local_only = 0; - - ZEND_PARSE_PARAMETERS_START(0, 2) - Z_PARAM_OPTIONAL - Z_PARAM_STRING_OR_NULL(str, str_len) - Z_PARAM_BOOL(local_only) - ZEND_PARSE_PARAMETERS_END(); - - if (!str) { - array_init(return_value); - php_import_environment_variables(return_value); - return; - } - - if (!local_only) { - /* SAPI method returns an emalloc()'d string */ - ptr = sapi_getenv(str, str_len); - if (ptr) { - // TODO: avoid reallocation ??? - RETVAL_STRING(ptr); - efree(ptr); - return; - } - } +PHPAPI zend_string *php_getenv(const char *str, size_t str_len) { #ifdef PHP_WIN32 { - wchar_t dummybuf; - DWORD size; - wchar_t *keyw, *valw; - - keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P); + wchar_t *keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P); if (!keyw) { - RETURN_FALSE; + return NULL; } SetLastError(0); - /*If the given buffer is not large enough to hold the data, the return value is - the buffer size, in characters, required to hold the string and its terminating - null character. We use this return value to alloc the final buffer. */ - size = GetEnvironmentVariableW(keyw, &dummybuf, 0); + /* If the given buffer is not large enough to hold the data, the return value is + * the buffer size, in characters, required to hold the string and its terminating + * null character. We use this return value to alloc the final buffer. */ + wchar_t dummybuf; + DWORD size = GetEnvironmentVariableW(keyw, &dummybuf, 0); if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { - /* The environment variable doesn't exist. */ - free(keyw); - RETURN_FALSE; + /* The environment variable doesn't exist. */ + free(keyw); + return NULL; } if (size == 0) { - /* env exists, but it is empty */ - free(keyw); - RETURN_EMPTY_STRING(); + /* env exists, but it is empty */ + free(keyw); + return ZSTR_EMPTY_ALLOC(); } - valw = emalloc((size + 1) * sizeof(wchar_t)); + wchar_t *valw = emalloc((size + 1) * sizeof(wchar_t)); size = GetEnvironmentVariableW(keyw, valw, size); if (size == 0) { - /* has been removed between the two calls */ - free(keyw); - efree(valw); - RETURN_EMPTY_STRING(); + /* has been removed between the two calls */ + free(keyw); + efree(valw); + return ZSTR_EMPTY_ALLOC(); } else { - ptr = php_win32_cp_w_to_any(valw); - RETVAL_STRING(ptr); + char *ptr = php_win32_cp_w_to_any(valw); + zend_string *result = zend_string_init(ptr, strlen(ptr), 0); free(ptr); free(keyw); efree(valw); - return; + return result; } } #else - tsrm_env_lock(); /* system method returns a const */ - ptr = getenv(str); - + char *ptr = getenv(str); + zend_string *result = NULL; if (ptr) { - RETVAL_STRING(ptr); + result = zend_string_init(ptr, strlen(ptr), 0); } tsrm_env_unlock(); + return result; +#endif +} - if (ptr) { - return; - } +/* {{{ Get the value of an environment variable or every available environment variable + if no varname is present */ +PHP_FUNCTION(getenv) +{ + char *str = NULL; + size_t str_len; + zend_bool local_only = 0; -#endif + ZEND_PARSE_PARAMETERS_START(0, 2) + Z_PARAM_OPTIONAL + Z_PARAM_STRING_OR_NULL(str, str_len) + Z_PARAM_BOOL(local_only) + ZEND_PARSE_PARAMETERS_END(); + + if (!str) { + array_init(return_value); + php_import_environment_variables(return_value); + return; + } + + if (!local_only) { + /* SAPI method returns an emalloc()'d string */ + char *ptr = sapi_getenv(str, str_len); + if (ptr) { + // TODO: avoid reallocation ??? + RETVAL_STRING(ptr); + efree(ptr); + return; + } + } + + zend_string *res = php_getenv(str, str_len); + if (res) { + RETURN_STR(res); + } RETURN_FALSE; } /* }}} */ diff --git a/ext/standard/basic_functions.h b/ext/standard/basic_functions.h index 34b2570175ab3..6c745dc549e8d 100644 --- a/ext/standard/basic_functions.h +++ b/ext/standard/basic_functions.h @@ -137,6 +137,8 @@ typedef struct { } putenv_entry; #endif +PHPAPI zend_string *php_getenv(const char *str, size_t str_len); + PHPAPI double php_get_nan(void); PHPAPI double php_get_inf(void);