Question: Consider the following C program to combine multiple files into one file. The command line to run the program is combinefiles file1 file2 fileN o
Consider the following C program to combine multiple files into one file. The command line to run the program is
combinefiles file1 file2 fileN o output
In the command line, file1, file2, , fileN are pathnames of N input files. output is the pathname of the file generated to contain the contents from the input files.
From the following lines of code, choose the line of code that best completes the functionality of the program. Note that it may be necessary to use one line multiple times. Choose option U if no code is needed. Please enter the letter corresponding to your answers on the lines provided below.
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int opt, output=0, len, int fd_o, fd_i;
char *addr=NULL;
struct stat stat_input;
const char *errmsg="Invalid argument: ";
const char *usage=
"CombileFiles file1 file2 ... fileN -o output_file ";
/* Analyze command line arguments */
while((___________________________________!= -1) {
________________
{
case 'o':
output=1;
/* Open output file. Create it if it does not exist.*/
_____________________________________________
if (fd_o == -1){
perror("open");
exit(1);
}
break;
case '?':
write(2, errmsg, strlen(errmsg));
write(2, usage, strlen(usage));
exit(1);
}
}
if (output==0){
write(2, errmsg, strlen(errmsg));
write(2, usage, strlen(usage));
exit(1);
}
/* copy file content */
for(; optind < argc; optind++){
_____________________________
if (fd_i == -1){
perror("open");
exit(1);
}
_____________________________
len=stat_input.st_size;
addr=mmap(0, len, PROT_READ, MAP_SHARED, fd_i, 0);
if (addr == MAP_FAILED) {
close(fd_i);
perror("mmap");
exit(1);
}
_______________________________
close(fd_i);
munmap(addr,len);
_______________________________
}
close(fd_o);
} Choices:
a. fd_o=open(argv[argc-1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
b. fd_o=open(argv[argc-1], O_WRONLY|O_TRUNC, 0644);
c. fd_o=open(optarg, O_WRONLY|O_CREAT|O_TRUNC, 0644);
d. fd_o=open(optarg, O_WRONLY|O_TRUNC, 0644);
e. opt = getopt(argc, argv, "o"))
f. opt = getopt(argc, argv, "o:"))
g. fd_i=open(optarg, O_RDONLY);
h. fd_i=open(optarg, O_WRONLY);
i. fd_i=open(argv[optind], O_RDONLY);
j. fd_i=open(argv[optind], O_WRONLY);
k. fstat(fd_i, &stat_input);
l. fstat(fd_i, stat_input);
m. stat(fd_i, &stat_input);
n. stat(fd_i, stat_input);
o. write(fd_o, addr, len);
p. write(fd_o, *addr, len);
q. write(2, addr, len);
r. write(2, *addr, len);
s. /* No code needed on this line */
t. switch(optind)
u. switch(opt) Just list the letters to make it easier for you and i, in order of each line. And providde a brief explanation on why its chosen if possible.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
