Question: Write a program called mycat that takes an arbitrary number of files ( which could be zero ) on the command line. This should not

Write a program called mycat that takes an arbitrary number of files (which could be zero) on the
command line. This should not be an interactive program; do not have your program prompt a user
for file names. Open each file sequentially in other order specified, then read the contents of each file
(splitting it up into chunks that fit into a fixed size buffer), and output each chunk read to standard
output (stdout). Your program should only output file contents; no additional information should
be displayed, i.e., do not show the name of each file before its contents, etc. The program will be
invoked as follows:
$ mycat file1 file2 file3... filen
Some things to consider:
If zero arguments are passed to your program, it should terminate without an error message,
i.e., specifying zero files to dump is not an error condition
If a file name given is invalid, your program should display (to the standard error stream) an
appropriate error message for that file indicating the reason for the error then move on to the
next file.
Because files can be of arbitrary length and contents, after displaying the contents of a file,
be sure to add a new line before you begin displaying the contents of the next file. This will
avoid having two files glued together on the same line.
You are not required to implement any option switches or other features found in the real
cat command.
3 Hints
Reading Command Line Arguments. File names will be specified on the command line when
the program is invoked. You can read each one by using the arguments argc and argv, which are
passed to the main() function of your program as such:
int main(int argc, char** argv)
The variable argc indicates the number of arguments passed, including the name of the pro-
gram. For instance, if invoking your program as:
mycat file1 file2 file3
the value of argc would be four, i.e., there are three arguments passed to the program plus the
program name itself. This can help you as you write that portion of your program that iterates
through the command line arguments. To access each file name, use argv, which can be thought
of as a two-dimensional array of characters. The first array, argv[0], is the name of the program,
i.e.,./mycat. Each subsequent array argv[1] through argv[n] contains each argument passed to
the program already parsed for you. Simply cycle through the array indices to access each file name.
Error Handling. The basic outline of your program will contain the following steps:
loop {
open each file using open()
For each file, loop {
read() a chunk of data from the file
Output that same chunk of data to standard output using write()
}
Output newline character
close() file
}
Exit the program
Within this program outline, there are primary two places where errors can occur: when opening a
file and when reading a chunk of data from a file ( a write to standard output will not fail, nor will
closing a valid file descriptor). These places are where you must include some error handling code.
Specifically, your program must check the return value of each open() or read() system call to see
if it was successful or if an error occurred.
For system calls, success or failure is indicated by the return value of the function. As a convention,
nearly every Unix system call that returns an integer, e.g., open(), read(), write(), close(),
etc., does the following:
If the call is successful, it returns an integer value greater than or equal to zero.
If the call failed, it returns an integer value less than zero (typically -1, but you should not
assume a specific value) and sets the value of a global variable called errno to indicate the
reason for the error.
Additionally, you can use the errno variable to retrieve the error number when failures do occur.
The errno variable is nothing more than an global integer variable that is set when certain error
conditions occur, and its value indicates the particular condition. Each error condition value is
defined by a name that is relatively easy to remember, like EPERM for permission denied. All avail-
able error types are specified in the header file /usr/include/asm/errno.h included with every C
compiler.
To use errno, pass it to the strerror() library function declared in the header file ,
which will convert the errno integer value into a string. You can then use that string to print a
descriptive error message:
fd = open(filename, O RDONLY, 0); // open file for read only
if( fd <0)// open() indicated an error condition
{
printf("Unable to open %s: %s
", filename, strerror(errno));
}
In the case of your program, if either an open() or a read() system call returns an error, an error
message should be displayed to standard error before moving on to the next file. An unsuccessful
attempt to open a file should not be considered a fatal error for this program.
*MUST USE open(), read(), write(), close() and NOT fopen/fgets/fclose or printf

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 Programming Questions!