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

Error: Wrong Number Of Arguments Specified

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

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

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

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

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

Dependencies

cluster_linux Linux cluster_linux_sysprog Linux Systems Programming cluster_linux_sysprog_fileio File I/O cluster_linux_sysprog_basics Linux Systems Programming: Basics cluster_linux_basics Linux Basics cluster_linux_basics_shell The Shell (Bash - “Bourne Again Shell”) cluster_linux_basics_intro Introduction: Concepts and Terminology cluster_linux_basics_permissions File System Permissions cluster_linux_toolchain Toolchain, And Cross Development linux_sysprog_fileio_basics File I/O: Basics linux_sysprog_basics_groupnode Linux Systems Programming: Basics linux_sysprog_fileio_basics->linux_sysprog_basics_groupnode linux_basics_permissions_basics Permissions: Mode, User and Group Ownership linux_sysprog_fileio_basics->linux_basics_permissions_basics linux_sysprog_fileio_basics_exercise_copy Exercise: Copy A File linux_sysprog_fileio_basics_exercise_copy->linux_sysprog_fileio_basics linux_sysprog_basics_errorhandling Error Handling linux_sysprog_fileio_basics_exercise_copy->linux_sysprog_basics_errorhandling linux_sysprog_fileio_basics_exercise_copy->linux_basics_permissions_basics linux_sysprog_basics_groupnode->linux_sysprog_basics_errorhandling linux_sysprog_basics_syscalls System Calls vs. Library Functions linux_sysprog_basics_groupnode->linux_sysprog_basics_syscalls linux_sysprog_basics_errorhandling->linux_sysprog_basics_syscalls linux_toolchain_basics Toolchain: Basics linux_sysprog_basics_syscalls->linux_toolchain_basics linux_basics_shell_ls Directory Listings: The ls Command linux_basics_shell_paths Absolute and Relative Paths linux_basics_shell_ls->linux_basics_shell_paths linux_basics_shell_cwd Current Working Directory linux_basics_shell_ls->linux_basics_shell_cwd linux_basics_intro_overview Overview linux_basics_shell_ls->linux_basics_intro_overview linux_basics_shell_file_dir_create_rm Creating And Removing Files and Directories linux_basics_shell_file_dir_create_rm->linux_basics_shell_paths linux_basics_shell_file_dir_create_rm->linux_basics_shell_cwd linux_basics_shell_commandline Commandline linux_basics_shell_paths->linux_basics_shell_commandline linux_basics_shell_commandline->linux_basics_intro_overview linux_basics_shell_cwd->linux_basics_shell_paths linux_basics_shell_cwd->linux_basics_shell_commandline linux_basics_intro_process Processes, Scheduling, Address Spaces linux_basics_shell_cwd->linux_basics_intro_process linux_basics_intro_process->linux_basics_intro_overview linux_basics_permissions_basics->linux_basics_shell_ls linux_toolchain_basics->linux_basics_shell_file_dir_create_rm

Footnotes