How can I succesfully call the execv function?

10,625

Change your copy array, and the function call. The following is a minimal example:

#include <unistd.h>

int main(int arcg, char *argv[])
{
    char *const args[] = {"cp","-p","-i", argv[1], argv[2], 0}; 
    execv("/bin/cp", args);
}
Share:
10,625

Related videos on Youtube

Mohamed Medhat Sallam
Author by

Mohamed Medhat Sallam

Linux for me is a to enjoy life. Not just a kernel. echo "Thank you Linus trovalds"

Updated on September 18, 2022

Comments

  • Mohamed Medhat Sallam
    Mohamed Medhat Sallam almost 2 years

    I am trying to make a program that will copy file1 into file2 the following way:

    cp -i -p file1 file2
    

    Now I call my executable copy and so by calling

    copy file1 file2
    

    It will do the same thing like the first command (-i and -p).

    I was able to do this using execl

    char const *copy[] = {"/bin/cp","cp","-p","-i",0};
    
    execl(copy[0],copy[1],copy[2],copy[3],argv[1],argv[2],copy[4]);
    

    However, I want to do it now with execv

    I saw the man page of exec* functions

    execl(const char *path, const char *arg, ...);
    
    execv(const char *path, char *const argv[]);
    

    and so the first argument seems to be the same however,

    How the second argument for execv is char *const argv[]

    what do I need to change in the execv function to get the same result ?

    I have my main function arguments like the following:

    main(int argc,char * argv[])