Hello,
I am trying to use putenv() on UNIX by concatenating str1 and str2 before that. I want to add a variable, or modify a variable, in the environment, so I am calling putenv() (or I could call setenv() identically).
Basically, I receive str1 and str2, I create str1=str2 and pass it in putenv() as a parameter.
The code I am showing works but when I uncomment the free() call, then it does not: the variable do not get added/modified for the environment.
size_t size = strlen(str1) + strlen(str2) + 2; // 2 is for the '\0' and the '='
char *tmp = (char *) malloc(sizeof(char) * size);
char *p;
int pos = 0;
// Copy first word
p = str1;
while (*p != NULL) {
tmp[pos++] = *p++;
}
// Add the '='
tmp[pos++] = '=';
// Copy second word
p = str2;
while (*p != NULL) {
tmp[pos++] = *p++;
}
// Add null character
tmp[pos] = '\0';
int ret = putenv(tmp);
if (ret != 0) {
perror("putenv failed");
}
//free(tmp); // This line is the problem when not commented
I apologize for the code redundancy, I know that the two while loops are identical. The issue that I have is that if I uncomment the free statement, then calling "env" to print the environment, the putenv will not have added the value.
I am not sure why is that. Right now to have it work, I am having a memory leak which I do not like. When I used an array, rather than a pointer, it would produce the same issue as uncommenting free.
Any idea please?
Thank you very much,
Jary