This assignment focuses on system-level programming in a Linux environment. You will design and implement the
C++ program which copies the contents of one file to another file, as described below.
It is worth 40 points (4% of course grade) and must be completed no later than 11:59 PM on Thursday, 1/30.
The deliverables for this assignment are the following files:
proj03.makefile – the makefile which produces proj03
proj03.student.c – the source code file for your solution
Be sure to use the specified file names and to submit your files for grading via the CSE Handin system before the
1. The purpose of the program is to copy the contents of one file (the source file) to another file (the destination
file). The user will interact with the program through command-line arguments.
a) The program will process the command line arguments from left to right.
b) If an argument begins with the character ‘-‘, it will be processed as an option which controls the behavior of the
program. Valid options are “-b”, “-t” and “-a” (defined below).
c) If an argument does not begin with the character ‘-‘, it will be processed as a file name. Exactly two file names
must be provided by the user, with the source file listed first and the destination file listed second.
2. The program will use the following functions to manage the files:
int open( const char *pathname, int flags, mode_t mode );
int close( int fd );
3. The program will use the following functions to perform input and output operations on the files:
ssize_t read( int fd, void *buf, size_t count );
ssize_t write( int fd, const void *buf, size_t count );
The size of the buffers associated with those functions will be determined by the “-b” option.
4. The program will recognize the following options:
a) The option “-b” will be followed by a separate command-line argument which indicates the size of the buffer (in
bytes) to be used during the processing. The default buffer size will be 256 bytes.
b) The option “-t” will cause the program to truncate an existing destination file, and then copy the source file to
the destination file.
c) The option “-a” will cause the program to append the source file to the end of an existing destination file.
d) If neither option “-t” nor option “-a” is selected by the user, the program will not modify a destination file
which already exists when the program begins execution.
e) The options “-t”, and “-a” will have no impact on a destination file which does not exist when the program
begins execution (the program will always attempt to create a destination file which does not exist).
5. The program will minimize the number of calls to function “write”, within the constraints imposed by the size of
6. The program will include appropriate logic to handle exceptional cases and errors.
1. As stated above, your source code file will be named “proj03.student.c”; that source code file may contain C or
2. You must use “g++” to translate your source code file in the CSE Linux environment.
3. As stated above, the command-line arguments will be processed from left to right. For example, consider the
following command line:
proj03 fileA -a fileB –b 128
The program will use a buffer size of 128 bytes, and will append the contents of the source file (“fileA”) to the end
of the destination file (“fileB”).
4. Each command-line argument is constructed as a low-level character string (array of type “char”, with a
terminating null byte). If you prefer to process a command-line argument as a C++ string class object, you should
consider converting the low-level character string. For example:
string prog = string( argv );
5. Information about the four system calls you will use for the project is available in section 2 of the “man” utility:
man 2 open
man 2 close
man 2 read
man 2 write
6. The third argument to function “open” allows you to control the file permissions associated with a file which is
being created. You would be wise to use “S_IRUSR | S_IWUSR” (or the equivalent) so that you can examine the
contents of any new files which your program creates. Otherwise, you will have to use the “chmod” command for
each file to change the file permissions for each new file.