1

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[])

closed as off-topic by DopeGhoti, muru, Jakuje, Jeff Schaller, Anthon Feb 17 '16 at 20:10

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.