Thread: Using execv

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Using execv

    Hi all,

    I have an array of strings named token declared as follows:
    char * token[32];
    I'm reading a line of input from the user, parsing the string and making each index of token contain each word input by the user (I removed the \n at the end too)
    My goal is to pass the input to the shell so that it can execute the line entered by the user as though the user had typed them directly into the terminal.
    I tried to do this as follows:

    Code:
    execv("/bin/tcsh", token);
    but it doesn't work...

    Let's say I enter ls somedir as the input, then I print out the values of the initialized indices of my array I can see that
    token[0] = ls
    token[1] = somedir
    so I know that the problem isn't that the array isn't being properly initialized...

    any ideas why it doesn't work?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Depends what you mean by "doesn't work".

    Did you (for example) set
    token[2] = NULL;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Salem: Yes, I've done that.

    Correction: Sorry. the result is actually that the /bin/tcsh/ just executes as though I'd just typed /bin/tcsh directly in a terminal (i.e. it seems to ignore the parameters).
    Last edited by Canadian0469; 11-03-2007 at 05:03 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if tsch is anything like bash, then passing a command line to the shell looks something like this.
    Code:
    $ ls *.c
    foo.c  new.c  old.c  util.c  wrapper.c
    $ bash -c "ls *.c"
    foo.c  new.c  old.c  util.c  wrapper.c
    So
    token[0] would be "-c"
    token[1] would be "ls *.c"
    token[2] would be NULL
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Lightbulb

    Thanks for the help guys. I figured it out and my solution is as follows:

    Code:
    {
    int pid;
    switch(pid = fork())
    {
    case -1:
    display("Fork failed during execution of shell handled commands.");
    break;
    case 0:
    //child
    token[i] = NULL;
    execvp(token[0], token);
    break;
    default:
    //parent
    wait(NULL);
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execv or execve?
    By jcafaro10 in forum C Programming
    Replies: 6
    Last Post: 04-12-2009, 11:18 AM
  2. getting stderr from an app launched via execv
    By KPexEA in forum Linux Programming
    Replies: 1
    Last Post: 08-16-2008, 10:36 PM
  3. No call to execv
    By protocol78 in forum C Programming
    Replies: 4
    Last Post: 05-08-2007, 11:43 AM
  4. execv and defunct process
    By groorj in forum C Programming
    Replies: 5
    Last Post: 08-05-2005, 07:37 AM
  5. execv extension list for shell
    By newbie29 in forum C Programming
    Replies: 1
    Last Post: 02-21-2005, 12:56 PM