2

The only part I am confused on thus far is how to set up execv with the first parameter as the current working directory. I've tried both "." and "~", neither are executing anything to the screen; same for "/." and "/~". I'm confused on how to have execv run something like this:

$ ./prog ls -t -al

And have it execute the commands after the program execution (which are stored into argv) in the current directory, or the same directory as the file is in (which will vary based on who is using it.)

My code:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

void main(int argc, char *argv[])
{
    int pid;
    int count = 0;
    char *argv2[argc+1];

    for(count = 0; count < argc-1; count++){
        argv2[count] = argv[count+1];
        printf("Argv2: %s\n", argv2[count]);  //just double checking
        argv2[argc-1] = NULL;
    }

    pid = fork();
    if(pid == 0){
        printf("Child's PID is %d. Parent's PID is %d\n", (int)getpid, (int)getppid());
        execv(".", argv2);       //<---- confused here
    }
    else{
        wait(pid);
        exit(0);
    }
}

Some sample output:

$ ./prog ls -t -al
Argv2: ls
Argv2: -t
Argv2: -al
Child's PID is 19194. Parent's PID is 19193
jww
  • 97,681
  • 90
  • 411
  • 885
Baelix
  • 261
  • 2
  • 7
  • 17
  • I would recommend increasing your setting for "compiler warnings". It should have been telling you to return int from main. On my system it also complained about a missing #include . And wait takes an integer pointer as an argument, not integer. – Scooter Sep 25 '12 at 10:56

6 Answers6

3

I guess execv is what is required to be used. execvp is a lot nicer since it will look for commands in your PATH setting.

execv(".", argv2);       //<---- confused here

...

#include <errno.h>
#include <string.h>
if ( execv(argv2[0],argv2) )
{
    printf("execv failed with error %d %s\n",errno,strerror(errno));
    return 254;  
}

wait(pid);

...

pid_t wait_status = wait(&pid);
Scooter
  • 6,802
  • 8
  • 41
  • 64
1

The first argument, by convention, should point to the filename associated with the file being executed. You want to exec ls, so the first argument should be /bin/ls,that means the code is

execv("/bin/ls", argv2);

You could try it

Lyle Malik
  • 71
  • 2
1
/* main() returns int */
int main(int argc, char *argv[])
{
        int pid;

        pid = fork();
        if(pid == 0){
                printf("Child's PID is %d. Parent's PID is %d\n"
                      , (int)getpid, (int)getppid());
                execv(argv[1], argv+1);
        }
        else{
        wait(NULL);
        exit(0);
        }

        return 0;
}

UPDATE: execv() needs the absolute path of the executable; for files in the current directory you would have to construct that path (eg via pwd()). If you want the executable to seached via the $PATH environment variable, you could use execvp(), which does all the seaching for you.

wildplasser
  • 43,142
  • 8
  • 66
  • 109
0

The first arg to execv() is not the directory in which to run but rather the path to the file you want to exec. By default, your exec-ed program will run in the context of the caller's current directory.

Marc Cohen
  • 3,742
  • 2
  • 19
  • 19
  • Well, I have also tried running it with `execv("/prog.c", argv2);`but that doesn't work either. I can't hard-code the path of the file into the program because the path will vary based on who runs the code. Any suggestions? (Sorry this is so ambiguous, my professor isn't quite top notch, if you catch my drift). – Baelix Sep 25 '12 at 03:56
  • And have also tried creating a folder and then subfolder just to allow it to be "/Lab1/Prog/". Still nothing :/ – Baelix Sep 25 '12 at 04:03
  • /prog.c is not a relative path - it's a full path to the file prog.c in the root directory. Also, the first arg to execv should be an executable file, not a c source file. I'm confused as to what you're trying to do here. – Marc Cohen Sep 25 '12 at 04:11
  • @baelix execv("/prog.c", argv2); would try to run a .c file, which makes no sense. Based on your actual description and the code you posted, you just need to do `execv(argv2[0], argv2);` The you compile & run your own program like you already mentioned: `./prog ls -t -al` – nos Sep 25 '12 at 11:54
0

The first argument is the absolute path of the program you want to run; in other words, the first part of the command you type in a terminal. You can find where the program using the command whereis. Also, execv function can look up the path for you, which makes your code more portable.

Niall C.
  • 10,878
  • 7
  • 69
  • 61
weito
  • 3
  • 2
0

Code sample for your reference:

#include <stdio.h>

int main() {

    int ret = fork();
    if(ret == 0)
    {
            char *params[4]  = {"/bin/ls", "-l",0}; //cmd params filled

            int res = execv( "/bin/ls" , params);  //parameters for cmd
            //int res = execv( "/bin/ls" , NULL);  //this is fail case (when child-exit with -1 status can be seen)
            printf("\n child exiting (%d) .. \n", res); //on successful execution of cmd, this exit never appears
        }
        else
        {
            waitpid(ret,1,0);
            printf("parent exiting\n");
    }

    return 1;
 }
parasrish
  • 3,864
  • 26
  • 32