Please help me with this Write a C program called lastnameEx
Please help me with this?
Write a C program called lastnameExecute which is able to execute the sampleCP(program written below), which takes two filepaths as parameters. Upon successful completion of the copy, your program lastnameExecute, and not the sampleCP program, should print out a confirmation message. You should not modify sampleCP. The input to lastnameExecute will be the same two filepaths passed to sampleCP. For example, running the following from the command line: $ ./lastnameExecute /tmp/file1 /tmp/file2 Should result in sampleCP performing the copy from file1 to file2 and produce terminal output similar to this: Beginning execution of sampleCP. tmp/file1 successfully copied to tmp/file2…Exiting.
SampleCp program:
steps:
1. compile sample cp program given above
2. write a program for lastName Execute and compile it. Use exec() of sample Cp program .
3. execute this program .pass command line arguments as file 1 and file 2
** Use any one of the exec() fns given below in the program:
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);
Solution
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
static int exec_prog(const char **argv)
{
pid_t my_procid;
int pstatus, timeover /* unused ifdef WAIT_FOR_COMPLETION */;
if (0 == (my_procid = fork())) {
if (-1 == execlp(\"sampleCP\", \"argv[1]\", \"argv[2]\" , (char *)NULL)) { /* argument for program to be called and using function execlp*/
perror(\"child process execve failed [%m]\");
return -1;
}
}
return 0;
}
