CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010
    Posts
    19

    [RESOLVED] How to get execve() to take 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?

    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

    ------------------------------------------------------------------------------------------------
    --- MY CODE --------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------------------

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

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

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to get execve() to take environment vars?

    As far as I remember (haven't used them for a long time), none of the exec...() variants ever evaluates the path environment variable to scan for the target executable. Only the command shell does, and this is invoked using system().

    At least that's how it works in the MS world; don't know if it behaves differently under *nix. Hope it helps, though...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured