Wiki Markup |
---|
Section 7.20.4.5 of C99 says that \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] |
The set of environment names and the method for altering the environment list are implementation-defined.
The getenv()
function searches an environment list for a string that matches a specified name, and returns a pointer to a string associated with the matched list member. Due to the manner in which environment variables are storedDepending on the implementation, multiple environment variables with the same name may be allowed and can cause unexpected results .Depending on the implementation, if a program may not cannot consistently choose the same value if there are multiple environment variables with the same name. The GNU glibc library addresses this issue in getenv()
and setenv()
by always using the first variable it encounters and ignoring the rest. Other implementations are following suit, although it is unwise to rely on this.
Wiki Markup |
---|
One common difference between implementations is whether or not environment variables are case sensitive. While UNIX-like implementations are generally case sensitive, environment variables are "not case sensitive in Windows 98/Me and Windows NT/2000/XP". \[[MSDN|AA. C References#MSDN]\] |
Duplicate Environment Variable Detection (POSIX)
...
This code behaves differently when compiled under Linux and Windows, because in Windows, environment variables are case-insensitive.
Code Block | ||
---|---|---|
| ||
char *temp; if (putenv("TEST_ENV=foo") != 0) { /* Handle Error */ } if (putenv("Test_ENV=bar") != 0) { /* Handle Error */ } temp = getenv("TEST_ENV"); if (temp == NULL) { /* Handle Error */ } printf("%s\n",temp); |
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.20.4, "Communication with the environment"
\[[MSDN|AA. C References#MSDN]\] [{{getenv()}}|http://msdn.microsoft.com/en-us/library/tehxacec(VS.71).aspx] |
...
ENV01-A. Do not make assumptions about the size of an environment variable 10. Environment (ENV) ENV03-A. Sanitize the environment when invoking external programs