Skip to content

Commit e69a65a

Browse files
committed
Export php_getenv() API
This exports a php_getenv() API which will fetch an environment variable in a thread-safe manner (assuming all other environment manipulations are thread-safe ... ha ha ha). Closes GH-6571.
1 parent 01f7722 commit e69a65a

File tree

2 files changed

+64
-62
lines changed

2 files changed

+64
-62
lines changed

ext/standard/basic_functions.c

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -727,98 +727,98 @@ PHP_FUNCTION(long2ip)
727727
* System Functions *
728728
********************/
729729

730-
/* {{{ Get the value of an environment variable or every available environment variable
731-
if no varname is present */
732-
PHP_FUNCTION(getenv)
733-
{
734-
char *ptr, *str = NULL;
735-
size_t str_len;
736-
zend_bool local_only = 0;
737-
738-
ZEND_PARSE_PARAMETERS_START(0, 2)
739-
Z_PARAM_OPTIONAL
740-
Z_PARAM_STRING_OR_NULL(str, str_len)
741-
Z_PARAM_BOOL(local_only)
742-
ZEND_PARSE_PARAMETERS_END();
743-
744-
if (!str) {
745-
array_init(return_value);
746-
php_import_environment_variables(return_value);
747-
return;
748-
}
749-
750-
if (!local_only) {
751-
/* SAPI method returns an emalloc()'d string */
752-
ptr = sapi_getenv(str, str_len);
753-
if (ptr) {
754-
// TODO: avoid reallocation ???
755-
RETVAL_STRING(ptr);
756-
efree(ptr);
757-
return;
758-
}
759-
}
730+
PHPAPI zend_string *php_getenv(const char *str, size_t str_len) {
760731
#ifdef PHP_WIN32
761732
{
762-
wchar_t dummybuf;
763-
DWORD size;
764-
wchar_t *keyw, *valw;
765-
766-
keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P);
733+
wchar_t *keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P);
767734
if (!keyw) {
768-
RETURN_FALSE;
735+
return NULL;
769736
}
770737

771738
SetLastError(0);
772-
/*If the given buffer is not large enough to hold the data, the return value is
773-
the buffer size, in characters, required to hold the string and its terminating
774-
null character. We use this return value to alloc the final buffer. */
775-
size = GetEnvironmentVariableW(keyw, &dummybuf, 0);
739+
/* If the given buffer is not large enough to hold the data, the return value is
740+
* the buffer size, in characters, required to hold the string and its terminating
741+
* null character. We use this return value to alloc the final buffer. */
742+
wchar_t dummybuf;
743+
DWORD size = GetEnvironmentVariableW(keyw, &dummybuf, 0);
776744
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
777-
/* The environment variable doesn't exist. */
778-
free(keyw);
779-
RETURN_FALSE;
745+
/* The environment variable doesn't exist. */
746+
free(keyw);
747+
return NULL;
780748
}
781749

782750
if (size == 0) {
783-
/* env exists, but it is empty */
784-
free(keyw);
785-
RETURN_EMPTY_STRING();
751+
/* env exists, but it is empty */
752+
free(keyw);
753+
return ZSTR_EMPTY_ALLOC();
786754
}
787755

788-
valw = emalloc((size + 1) * sizeof(wchar_t));
756+
wchar_t *valw = emalloc((size + 1) * sizeof(wchar_t));
789757
size = GetEnvironmentVariableW(keyw, valw, size);
790758
if (size == 0) {
791-
/* has been removed between the two calls */
792-
free(keyw);
793-
efree(valw);
794-
RETURN_EMPTY_STRING();
759+
/* has been removed between the two calls */
760+
free(keyw);
761+
efree(valw);
762+
return ZSTR_EMPTY_ALLOC();
795763
} else {
796-
ptr = php_win32_cp_w_to_any(valw);
797-
RETVAL_STRING(ptr);
764+
char *ptr = php_win32_cp_w_to_any(valw);
765+
zend_string *result = zend_string_init(ptr, strlen(ptr), 0);
798766
free(ptr);
799767
free(keyw);
800768
efree(valw);
801-
return;
769+
return result;
802770
}
803771
}
804772
#else
805-
806773
tsrm_env_lock();
807774

808775
/* system method returns a const */
809-
ptr = getenv(str);
810-
776+
char *ptr = getenv(str);
777+
zend_string *result = NULL;
811778
if (ptr) {
812-
RETVAL_STRING(ptr);
779+
result = zend_string_init(ptr, strlen(ptr), 0);
813780
}
814781

815782
tsrm_env_unlock();
783+
return result;
784+
#endif
785+
}
816786

817-
if (ptr) {
818-
return;
819-
}
787+
/* {{{ Get the value of an environment variable or every available environment variable
788+
if no varname is present */
789+
PHP_FUNCTION(getenv)
790+
{
791+
char *str = NULL;
792+
size_t str_len;
793+
zend_bool local_only = 0;
820794

821-
#endif
795+
ZEND_PARSE_PARAMETERS_START(0, 2)
796+
Z_PARAM_OPTIONAL
797+
Z_PARAM_STRING_OR_NULL(str, str_len)
798+
Z_PARAM_BOOL(local_only)
799+
ZEND_PARSE_PARAMETERS_END();
800+
801+
if (!str) {
802+
array_init(return_value);
803+
php_import_environment_variables(return_value);
804+
return;
805+
}
806+
807+
if (!local_only) {
808+
/* SAPI method returns an emalloc()'d string */
809+
char *ptr = sapi_getenv(str, str_len);
810+
if (ptr) {
811+
// TODO: avoid reallocation ???
812+
RETVAL_STRING(ptr);
813+
efree(ptr);
814+
return;
815+
}
816+
}
817+
818+
zend_string *res = php_getenv(str, str_len);
819+
if (res) {
820+
RETURN_STR(res);
821+
}
822822
RETURN_FALSE;
823823
}
824824
/* }}} */

ext/standard/basic_functions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ typedef struct {
137137
} putenv_entry;
138138
#endif
139139

140+
PHPAPI zend_string *php_getenv(const char *str, size_t str_len);
141+
140142
PHPAPI double php_get_nan(void);
141143
PHPAPI double php_get_inf(void);
142144

0 commit comments

Comments
 (0)