pajoye Mon Aug 18 07:09:20 2008 UTC Modified files: /php-src/ext/standard basic_functions.c Log: - [DOC] make putenv behaves like unix putenv on Windows: . FOO=1234 => set FOO to 1234 . FOO= => set FOO to an empty string . FOO= => unset FOO . Use Set/GetEnvironmnent variable only http://cvs.php.net/viewvc.cgi/php-src/ext/standard/basic_functions.c?r1=1.918&r2=1.919&diff_format=u Index: php-src/ext/standard/basic_functions.c diff -u php-src/ext/standard/basic_functions.c:1.918 php-src/ext/standard/basic_functions.c:1.919 --- php-src/ext/standard/basic_functions.c:1.918 Sat Aug 16 10:57:26 2008 +++ php-src/ext/standard/basic_functions.c Mon Aug 18 07:09:20 2008 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.918 2008/08/16 10:57:26 bjori Exp $ */ +/* $Id: basic_functions.c,v 1.919 2008/08/18 07:09:20 pajoye Exp $ */ #include "php.h" #include "php_streams.h" @@ -61,7 +61,11 @@ #include <netinet/in.h> #endif -#include<netdb.h> +#ifndef PHP_WIN32 +# include<netdb.h> +#else +# include "win32/inet.h" +#endif #if HAVE_ARPA_INET_H # include <arpa/inet.h> @@ -4391,15 +4395,39 @@ /* SAPI method returns an emalloc()'d string */ ptr = sapi_getenv(str, str_len TSRMLS_CC); if (ptr) { - RETURN_RT_STRING(ptr, ZSTR_AUTOFREE); + RETURN_STRING(ptr, ZSTR_AUTOFREE); } +#ifdef PHP_WIN32 + { + char dummybuf; + int size; + SetLastError(0); + /*If the given bugger 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 = GetEnvironmentVariableA(str, &dummybuf, 0); + if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) { + /* The environment variable doesn't exist. */ + RETURN_FALSE; + } + + if (size == 0) { + /* env exists, but it is empty */ + RETURN_EMPTY_STRING(); + } + + ptr = emalloc(size); + size = GetEnvironmentVariableA(str, ptr, size); + RETURN_STRING(ptr, 0); + } +#else /* system method returns a const */ ptr = getenv(str); if (ptr) { RETURN_RT_STRING(ptr, ZSTR_DUPLICATE); } - +#endif RETURN_FALSE; } /* }}} */ @@ -4419,14 +4447,33 @@ if (setting_len) { char *p, **env; putenv_entry pe; +#ifdef PHP_WIN32 + char *value = NULL; + int equals = 0; +#endif pe.putenv_string = estrndup(setting, setting_len); pe.key = estrndup(setting, setting_len); if ((p = strchr(pe.key, '='))) { /* nullify the '=' if there is one */ *p = '\0'; +#ifdef PHP_WIN32 + equals = 1; +#endif } + pe.key_len = strlen(pe.key); +#ifdef PHP_WIN32 + if (equals) { + if (pe.key_len < setting_len - 2) { + value = p + 1; + } else { + /* empty string*/ + value = p; + } + } +#endif + zend_hash_del(&BG(putenv_ht), pe.key, pe.key_len+1); /* find previous value */ @@ -4443,23 +4490,19 @@ } } -#if _MSC_VER >= 1300 - /* VS.Net has a bug in putenv() when setting a variable that - * is already set; if the SetEnvironmentVariable() API call - * fails, the Crt will double free() a string. - * We try to avoid this by setting our own value first */ - SetEnvironmentVariable(pe.key, "bugbug"); -#endif - #if HAVE_UNSETENV if (!p) { /* no '=' means we want to unset it */ unsetenv(pe.putenv_string); } if (!p || putenv(pe.putenv_string) == 0) { /* success */ #else +# ifndef PHP_WIN32 if (putenv(pe.putenv_string) == 0) { /* success */ +# else + if (SetEnvironmentVariableA(pe.key, value) != 0) { /* success */ +# endif #endif - zend_hash_add(&BG(putenv_ht), pe.key, pe.key_len+1, (void **) &pe, sizeof(putenv_entry), NULL); + zend_hash_add(&BG(putenv_ht), pe.key, pe.key_len + 1, (void **) &pe, sizeof(putenv_entry), NULL); #ifdef HAVE_TZSET if (!strncmp(pe.key, "TZ", pe.key_len)) { tzset();
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php