Question
Purpose: Use getopt() to parse the command line argument for implementing the revised cp command Instruction: Modify the sample cp1.c code to implement the -i
Purpose: Use getopt() to parse the command line argument for implementing the revised cp command
Instruction:
Modify the sample cp1.c code to implement the "-i" option (interactive) by using getopt() function.
Refer to getopt() function in C to parse command line arguments - GeeksforGeeks to learn how to use getopt() function.
Step 1: Determine if the interactive mode is on.
After "usage error" checking and "file open error" checking, add a while loop to check if a user enters a "-i" option
For example, the while loop can be like:
while((opt = getopt(ac, av, "i")) != -1) { switch(opt) { case "i":
//turn on the interactive mode to ask the user "overwrite output file?".
//If the user enters 'y' or 'Y', then overwrite the output file, if the user answers 'N' or 'n', then
//create a new file by attaching a number. The number starts with 1, then 2, 3, etc.
break;
}
}
Step 2:
If the interactive is on and the output file exists, prompt the user, "cp: overwrite 'output file'?"
If the answer is No or N, and if the output file exists, then create an output file by attaching a number starting from 1, otherwise, create and copy the file.
If the answer is Yes or Y, and if the output file exists, overwrite it or create it if the output file does not exist.
For example,
> cp -i input output
>"cp: overwrite 'output file'?"
>N
(copy input to output1)
(if the output file exists)
Please provide the following as well:
1. .c file
2. Screenshots for the successful execution of your program with all the possible cases.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started