Skip to content

Export php_getenv() API #6571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 62 additions & 62 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/* }}} */
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down