@@ -727,98 +727,98 @@ PHP_FUNCTION(long2ip)
727
727
* System Functions *
728
728
********************/
729
729
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 ) {
760
731
#ifdef PHP_WIN32
761
732
{
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 );
767
734
if (!keyw ) {
768
- RETURN_FALSE ;
735
+ return NULL ;
769
736
}
770
737
771
738
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 );
776
744
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 ;
780
748
}
781
749
782
750
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 ();
786
754
}
787
755
788
- valw = emalloc ((size + 1 ) * sizeof (wchar_t ));
756
+ wchar_t * valw = emalloc ((size + 1 ) * sizeof (wchar_t ));
789
757
size = GetEnvironmentVariableW (keyw , valw , size );
790
758
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 ();
795
763
} 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 );
798
766
free (ptr );
799
767
free (keyw );
800
768
efree (valw );
801
- return ;
769
+ return result ;
802
770
}
803
771
}
804
772
#else
805
-
806
773
tsrm_env_lock ();
807
774
808
775
/* system method returns a const */
809
- ptr = getenv (str );
810
-
776
+ char * ptr = getenv (str );
777
+ zend_string * result = NULL ;
811
778
if (ptr ) {
812
- RETVAL_STRING (ptr );
779
+ result = zend_string_init (ptr , strlen ( ptr ), 0 );
813
780
}
814
781
815
782
tsrm_env_unlock ();
783
+ return result ;
784
+ #endif
785
+ }
816
786
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 ;
820
794
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
+ }
822
822
RETURN_FALSE ;
823
823
}
824
824
/* }}} */
0 commit comments