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?

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

#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");
}

Here's two outputs:

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$

I think you'd rather want to use execvp() , see exec() - scroll down to Using execvp(), there's a minimum usage example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.