M
Michael B Allen
Is the string returned by getenv guaranteed to be the same string supplied
to putenv plus the offset of the variable name and equals sign?
Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.
The following code demponstrates that with at least glibc the same string
is in fact returned.
unsigned char *s1, *s2;
s2 = "SOMEVAR=whatever";
s1 = malloc(strlen(s2) + 1);
strcpy(s1, s2);
if (putenv(s1) == -1) {
PMNO(errno);
return -1;
}
s2 = getenv("SOMEVAR");
printf("s1=%p,s2 - 8=%p\n", s1, s2 - 8);
free(s2 - 8);
output: s1=0x9925828,s2 - 8=0x9925828
Mike
to putenv plus the offset of the variable name and equals sign?
Because of API constraints I do not want to save a pointer to the string
passed to putenv but I need to be able to free it later or I will have
a memory leak.
The following code demponstrates that with at least glibc the same string
is in fact returned.
unsigned char *s1, *s2;
s2 = "SOMEVAR=whatever";
s1 = malloc(strlen(s2) + 1);
strcpy(s1, s2);
if (putenv(s1) == -1) {
PMNO(errno);
return -1;
}
s2 = getenv("SOMEVAR");
printf("s1=%p,s2 - 8=%p\n", s1, s2 - 8);
free(s2 - 8);
output: s1=0x9925828,s2 - 8=0x9925828
Mike