How to get execve() to like your environment vars?

Hi everyone,

I'm a moderately experienced C coder trying to figure out the execve() command. Here's the basic problem:

My program takes a command from the user, loaded into the string UsrCmd. The string is tokenized and each token is loaded into ParaArray[]. Then ParaArray[] is used as arguments to call execve(). I think I also pass in the user's environment variables with envp[].

But here's the problem: If the user inputs a command without the full path to that command, execve() cannot find the associated system call and fails. It is obviously too cumbersome to ask the user to enter the full path for each inputted command. So how do I get execve() to accept the PATH variables? Or, put more simply, how can I make sure any variation of exec() will accept, understand, and use the user's environment variables?

Below I've included both my code and then two outputs:
-- In the first output, I simply type "ls -l"
-- In the second output, I type "/bin/ls -l"

Many thanks!
-P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <unistd.h>
#include <sstream>
using namespace std;

#define BUFF_SIZE 1000


int main(int argc, char **argv, char* envp[])
{
  // Define buffer for user's command
  char *UsrCmd = (char *)malloc(sizeof(char) * BUFF_SIZE);
  char * token;
  char * ParaArray[100];
  int ParaIndex  = 0;  int i = 0;

  printf("Here are your Environment Variables:\n");
  while(envp[i]!=NULL)
    { printf("%s\n", envp[i]); i++; }

  // Get user's command
  printf("ENTER A COMMAND:  ");
  fgets(UsrCmd, BUFF_SIZE+1, stdin);

  // We tokenize the user's command and load the tokens into ParaArray
  token = strtok(UsrCmd, " \t\n");
  ParaArray[ParaIndex] = token;  ParaIndex++;
  while(token != NULL)
    {
      token = strtok(NULL, " \t\n");
      ParaArray[ParaIndex] = token;  ParaIndex++;
    }
  ParaArray[ParaIndex]=NULL;

  // Now we run execve() with envp[] passed in as the environment variables
  execve(ParaArray[0], ParaArray, envp);
  printf("Your command FAILED!\n");
}


...and here's some sample output...

bash-2.05$ 
bash-2.05$ 
bash-2.05$ ./Run_Program
Here are your Environment Variables:
PWD=/dir1/dir2/phummon/
TZ=US/Eastern
VENDOR=sun
...
PATH=/usr/openwin/bin:/usr/local/bin:/usr/local/sbin:/opt/SUNWspro/bin:/usr/local/gnu/bin:/usr/ccs/bin:/usr/bin:/usr/ucb:/usr/sbin:/bin:/sbin
...
ENTER A COMMAND:  ls -l
Your command FAILED!
bash-2.05$ 
bash-2.05$ 
bash-2.05$ ./Run_Program
Here are your Environment Variables:
PWD=/dir1/dir2/phummon/
TZ=US/Eastern
VENDOR=sun
...
PATH=/usr/openwin/bin:/usr/local/bin:/usr/local/sbin:/opt/SUNWspro/bin:/usr/local/gnu/bin:/usr/ccs/bin:/usr/bin:/usr/ucb:/usr/sbin:/bin:/sbin
...
ENTER A COMMAND:  /bin/ls -l
total 300
-rw-------   1 phummon  users        993 Sep 25 12:47 Main.cpp
-rwx------   1 phummon  users     102920 Sep 25 12:51 Run_Program
-rw-------   1 phummon  users        100 Sep 24 20:44 file1
-rw-------   1 phummon  users        100 Sep 24 20:41 file2
-rw-------   1 phummon  users        100 Sep 24 20:41 file3
-rw-------   1 phummon  users        100 Sep 24 20:41 file4
bash-2.05$
bash-2.05$ 



You need to use the execvp() function to have it search the PATH for you.
http://linux.die.net/man/3/exec

To manipulate the environment that the subprocess uses, directly modify the environ variable.
http://linux.die.net/man/7/environ

Always read the man pages carefully when using system calls.

Hope this helps.
Topic archived. No new replies allowed.