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, ie do not show the name of each file before its contents, etc. The program will be
invoked as follows:
$ mycat file file file filen
Some things to consider:
If zero arguments are passed to your program, it should terminate without an error message,
ie 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.
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 mainint 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 file file file
the value of argc would be four, ie 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 twodimensional array of characters. The first array, argv is the name of the program,
iemycat Each subsequent array argv through argvn 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, eg 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 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 usrincludeasmerrnoh 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 openfilename O RDONLY, ; open file for read only
if fd open indicated an error condition
printfUnable to open s: s
filename, strerrorerrno;
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 fopenfgetsfclose or printf
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
