Execv Problem

This is a discussion on Execv Problem within the C Programming forums, part of the General Programming Boards category; Hi, my task is to write a shell starting any program matching the input, e.g. .../Folder1/Folder2/Folder3/file to make sure my ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    11

    Execv Problem

    Hi,

    my task is to write a shell starting any program matching the input, e.g. .../Folder1/Folder2/Folder3/file

    to make sure my program does what it is supposed to do I tried to start a simple program, it is only
    Code:
    #include <stdio.h>
    int main(void) {
    "printf("Hello");
    return 0;
    }
    nothing else.

    How to start this program using one of the execv commands?

    I tried:

    Code:
    char* const args[] = { "test", "-l", (char*) 0 };
    int i = execv("Desktop/test", args);
    but failed

    What am I doing wrong?
    Last edited by The-Forgotten; 11-19-2011 at 11:43 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Did you read the manual to find out what is in i (or indeed errno) when it failed?
    execl(3): execute file - Linux man page

    Perhaps try with an absolute path to the file to execute.
    The function is very stupid - it has none of the "comforts" like a shell has, where a PATH is searched for things matching the filename to run.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    11
    Found my error and fixed it, now I have another question. How to print the exit state of the execv call?
    Code:
    printf("Exit State: %i\n",execv(path, args));
    works only, if an error occured.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > works only, if an error occured.
    That's because exec() functions do NOT return on success.
    Success means they managed to run the process you wanted to run.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execv failed :(
    By Andaluz in forum Linux Programming
    Replies: 6
    Last Post: 09-25-2009, 01:41 AM
  2. execv problem
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-25-2009, 10:48 AM
  3. Using execv
    By Canadian0469 in forum C Programming
    Replies: 4
    Last Post: 11-03-2007, 09:52 PM
  4. No call to execv
    By protocol78 in forum C Programming
    Replies: 4
    Last Post: 05-08-2007, 11:43 AM
  5. Something wrong with execv?
    By fnoyan in forum Linux Programming
    Replies: 7
    Last Post: 03-03-2006, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21