I need to create a program that allows the user to execute a command passed as argument using execve in linux. I'm not sure about the syntax of the execve command. I wrote the program but it doesn't work with multiple arguments and I can't figure out why.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char **argv)
{
int status;
pid_t pid;
if((pid = fork())>0)
{
///Father process
wait(&status);
printf("Process terminated with status = %d\n",status);
}
else
{
///son process
int i;
char param[100];
printf("I'm the son woth PID= %d\n",getpid());
printf("%s\n",argv[0]);
printf("%s\n",argv[1]);
printf("%s\n",argv[2]);
strcpy(param,"/bin/");
strcat(param,argv[1]);
execve(param,argv,NULL);
exit(-1);
}
return 0;
}
A command that doesn't work using this code is
cp file1.txt file2.txt
Can someone help me?