I was trying to set the environment of child process similar to the parent process and I need to fill the array called envp with string as follows:
char *envp = malloc(sizeof(args) / sizeof(char *));
strcpy(envp[0], strcat("HOME=", getenv("HOME")));
envp[1] = strcat("PATH=", getenv("PATH"));
envp[2] = strcat("TZ=", getenv("TZ"));
envp[3] = strcat("USER=", getenv("USER"));
envp[4] = strcat("LOGNAME=", getenv("LOGNAME"));
envp[5] = 0;
Inside if(fork() ==0)
setenv("parent", cwd, 1);
if((execve(args[0], &args[0], envp)) < 0 ){
perror(*args);
exit(EXIT_FAILURE);
}
I'm doing this because I don't know the values of these environment variables so I want to copy the parent variables in order to use them in execve() that will replace the child process! I'm using execve() instead of the execvp() because I want to handle searching the cwd before executing the command and search the directories of the shell path name of the cwd is not found.
So my question is: How to set the values of the array in a correct way? Plus, am I misunderstanding any concept?
I looked at many posts here, but it's obvious that I'm lost! Thanks in advance,