Thread: execve() accepting & using environment variables?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    New Jersey
    Posts
    14

    execve() accepting & using environment variables?

    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

    Code:
    #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...

    Code:
    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$

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Use execvp() instead, unless there is a compelling reason to use the execve() call.
    execvp() looks for the user supplied cmd. in the dirs. given by the PATH env var.
    Code:
    execvp(ParaArray[0], ParaArray);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  2. CGI Environment Variables.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 06-10-2002, 07:51 PM
  3. Having trouble correctly setting DJGPP's env. variables in XP
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 06-05-2002, 10:51 PM
  4. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM
  5. What are environment variables?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-18-2002, 08:11 AM

Tags for this Thread