title: How to set the environment variable of child same as parent to use execve in C?
body: <p>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:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Inside if(fork() ==0)</p>
  
  <pre><code>setenv("parent", cwd, 1);
  if((execve(args[0], &amp;args[0], envp)) &lt; 0 ){
              perror(*args);
              exit(EXIT_FAILURE);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>So my question is: How to set the values of the array in a correct way?
  Plus, am I misunderstanding any concept? </p>
  
  <p>I looked at many posts here, but it's obvious that I'm lost!
  Thanks in advance,</p>