C99 says {{ Wiki Markup getenv()
}} has the following behavior \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\]:
The getenv function returns a pointer to a string associated with the matched list member. The string pointed to shall not be modified by the program but may be overwritten by a subsequent call to the getenv function.
...
Compliant Solution (Windows)
Windows provides the [{{ Wiki Markup getenv_s()
}} and {{\_wgetenv_s()
}}|http://msdn.microsoft.com/en-us/library/tb2sfw2z(VS.80).aspx] functions for getting a value from the current environment \[ [MSDN|AA. Bibliography#MSDN]\].
Code Block | ||||
---|---|---|---|---|
| ||||
char *tmpvar; char *tempvar; size_t requiredSize; getenv_s(&requiredSize, NULL, 0, "TMP"); tmpvar = (char *)malloc(requiredSize * sizeof(char)); if (!tmpvar) { /* Handle error */ } getenv_s(&requiredSize, tmpvar, requiredSize, "TMP" ); getenv_s(&requiredSize, NULL, 0, "TEMP"); tempvar = (char *)malloc(requiredSize * sizeof(char)); if (!tempvar) { free(tmpvar); tmpvar = NULL; /* Handle error */ } getenv_s(&requiredSize, tempvar, requiredSize, "TEMP" ); if (strcmp(tmpvar, tempvar) == 0) { if (puts("TMP and TEMP are the same.\n") == EOF) { /* Handle error */ } } else { if (puts("TMP and TEMP are NOT the same.\n") == EOF) { /* Handle Error */ } } free(tmpvar); tmpvar = NULL; free(tempvar); tempvar = NULL; |
Compliant Solution (Windows)
...
Windows also provides the [{{\_dupenv_s()
}} and {{\_wdupenv_s()
}}|http://msdn.microsoft.com/en-us/library/ms175774.aspx] functions for getting a value from the current environment \[ [MSDN|AA. Bibliography#MSDN]\].
The _dupenv_s()
function searches the list of environment variables for a specified name. If the name is found, a buffer is allocated; the variable's value is copied into the buffer, and the buffer's address and number of elements are returned. By allocating the buffer itself, _dupenv_s()
and _wdupenv_s()
provide a more convenient alternative to getenv_s()
and _wgetenv_s()
.
...
Compliant Solution (POSIX)
POSIX provides the [{{ Wiki Markup strdup()
}}|http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html] function, which can make a copy of the environment variable string \ [[Open Group 2004|AA. Bibliography#Open Group 04]\]. The {{strdup()
}} function is also included in ISO/IEC PDTR 24731-2 \ [[ISO/IEC PDTR 24731-2|AA. Bibliography#ISO/IEC ISO/IEC PDTR 24731-2]\].
Code Block | ||||
---|---|---|---|---|
| ||||
char *tmpvar; char *tempvar; const char *temp = getenv("TMP"); if (temp != NULL) { tmpvar = strdup(temp); if (tmpvar == NULL) { /* Handle error */ } } else { return -1; } temp = getenv("TEMP"); if (temp != NULL) { tempvar = strdup(temp); if (tempvar == NULL) { free(tmpvar); tmpvar = NULL; /* Handle error */ } } else { free(tmpvar); tmpvar = NULL; return -1; } if (strcmp(tmpvar, tempvar) == 0) { if (puts("TMP and TEMP are the same.\n") == EOF) { /* Handle error */ } } else { if (puts("TMP and TEMP are NOT the same.\n") == EOF) { /* Handle error */ } } free(tmpvar); tmpvar = NULL; free(tempvar); tempvar = NULL; |
...
Bibliography
\[[MSDN|AA. Bibliography#MSDN]\] [{{\ Wiki Markup _dupenv_s()
}} and {{\_wdupenv_s()
}}|http://msdn.microsoft.com/en-us/library/ms175774.aspx], [{{getenv_s()
}}, {{\_wgetenv_s()
}}|http://msdn.microsoft.com/en-us/library/tb2sfw2z(VS.80).aspx]
\[[Open Group 2004|AA. Bibliography#Open Group 04]\] Chapter 8, and "Environment Variables", [{{strdup}}|http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html]
\[[Viega 2003|AA. Bibliography#Viega 03]\] Section 3.6, "Using Environment Variables
[Open Group 2004] Chapter 8, and "Environment Variables", strdup
[Viega 2003] Section 3.6, "Using Environment Variables Securely"
...