can we send arguments to system() call

Hi friends

my C code is

int main()
{
system("cp <source> <destination>");
}

my question is
how to set variables for <source> and <destination>
how can we pass it to system() call.

can you suggest me
thankyou
kingskar

In such situations it's always better to do a combination of fork and one of the exec family member.

if(fork()==0){
//do nothing
}
else{

exec....( what ever suits you)

}

You can also check the exec man page to see the all members of exec family!!

regards
Apoorva kumar

Thankyou for your valuable suggestion

Cheers
Shekar

vfork and exec - that is what is done in system function call..

and to answer your question, something like this would do..

/* source file in char source[512] and dest file in char dest[512] */
char str[512];
memset(str, '\0', sizeof(512));
sprintf(str, "/usr/bin/cp %s %s", source, dest);
ret=system(str);

Yes
your code is exactly fits for my requirement.
Thankyou
Cheers
kingskar

if you are using glibc it may be better to use:
#define _GNU_SOURCE
#include <stdio.h>

char buf;
asprintf(&buf,"cp %s/%s",src,dst); /
do not forget error check */
system(buf);
free(buf);

the advantage is that you have no limits for pathlenght.
you can also emulate asprintf with 2 calls to snprintf()

what is the need to use free here ?