Question: Write a C program which is called multiple_copies. This program copies one source file to two destination files as follows: $./multiple_copies source_file destination_file1 destination_file2 multiple_copies

Write a C program which is called multiple_copies. This program copies one source file to two destination files as follows:

$./multiple_copies source_file destination_file1 destination_file2

multiple_copies program copies the contents of source file (i.e., source_file) to any named two destination files in the command line.

The number of arguments should be 3 (without the multiple_copies). If the number of arguments is not 3, the program should display an error message saying:

Usage: multiple_copies source_file destination_file1 destination_file2.

3. When the source_file does not exist, the program should display an error message that includes the source file name, e.g., Opening Error!: source_file.

4. If the destination file exists, existing content of that file should be truncated (removed).

Note: You can use the sample code (fileio/copy.c in the tlpi-dist) covered in the lecture. Work in the fileio directory given in the source file distribution of the textbook, update the Makefile in the fileio directory to include the new file multiple_copies_yourName, compile using make multiple_copies_yourName'.

Below is copy.c

int main(int argc, char *argv[]) { int inputFd, outputFd, openFlags; mode_t filePerms; ssize_t numRead; char buf[BUF_SIZE]; if (argc != 3 || strcmp(argv[1], "--help") == 0) usageErr("%s old-file new-file ", argv[0]); /* Open input and output files */ inputFd = open(argv[1], O_RDONLY); if (inputFd == -1) errExit("opening file %s", argv[1]); openFlags = O_CREAT | O_WRONLY | O_TRUNC; filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw-rw-rw- */ outputFd = open(argv[2], openFlags, filePerms); if (outputFd == -1) errExit("opening file %s", argv[2]); /* Transfer data until we encounter end of input or an error */ while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0) if (write(outputFd, buf, numRead) != numRead) fatal("write() returned error or partial write occurred"); if (numRead == -1) errExit("read"); if (close(inputFd) == -1) errExit("close input"); if (close(outputFd) == -1) errExit("close output"); exit(EXIT_SUCCESS); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!