Exercise: Copy A File

Requirement

Write a program cp-for-the-poor which exhibits the following behavior:

  • It interprets its two arguments as filenames, and copies the first to the second

  • The first filename must be an existing file

  • The second filename is the target of the copy

  • No existing file must be overwritten

  • The program operates at the system call layer. Use open()/read()/write()/close(), and not anything from <stdio.h>.

Note

Sunny Case: Source File Exists, Destination Does Not Exist

$ ./cp-for-the-poor /etc/passwd /tmp/passwd-copy
$ echo $?
0

(Test script (download))

Error: Wrong Number Of Arguments Specified

$ ./cp-for-the-poor
./cp-for-the-poor: SRCFILE DSTFILE
$ echo $?
1

(Test script (download))

Error: Source File Does Not Exist

$ ./cp-for-the-poor /etc/passwd-not-there /tmp/some-file-that-does-not-exist
/etc/passwd-not-there: No such file or directory
$ echo $?
2

(Test script (download))

Error: Destination File Exists

Provided that /tmp/passwd-copy already exists [1]:

$ ./cp-for-the-poor /etc/passwd /tmp/passwd-copy
/tmp/passwd-copy: File exists
$ echo $?
3

(Test script (download))

Error: Destination Directory Not Writable

Provided that /etc is not writable (because you are not root, for example),

$ ./cp-for-the-poor /etc/passwd /etc/passwd-copy
/etc/passwd-copy: Permission denied
$ echo $?
4

(Test script (download))

Submission

  • Create a directory exercise-1 which contains all the source (C code, and CMake build instructions)

  • From the parent directory of exercise-1, package that directory

    $ tar -J -c -f submission.tar.xz exercise-1/
    
  • Submit submission.tar.xz

Footnotes