/**
 * File: copy.c
 * --------------
 * This program is the starter code for an example that shows how we can
 * make a copy of a specified file into another specified file, similar to the
 * `cp` unix command.
 */

#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

// The permissions to set if the destination file doesn't exist
static const int kDefaultPermissions = 0644;  // number equivalent of "rw-r--r--"

// The number of bytes we copy at once from source to destination
static const int kCopyIncrement = 1024;

/* This function copies the contents of the source file into the destination
 * file in chunks until the entire file is copied.
 */
void copyContents(int sourceFD, int destinationFD) {
    // TODO: implement this function!
}

int main(int argc, char *argv[]) {
    // Open the specified source file for reading
    int sourceFD = open(argv[1], O_RDONLY);

    // open error checking omitted (see copy-soln-full.c for added error checking)

    // Open the specified destination file for writing, creating if nonexistent
    int destinationFD = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, kDefaultPermissions);
    if (destinationFD == -1) {
        // Can also use the error function for cleaner error checking - see copy-soln-full.c.
        printf("%s: destination file could not be created.\n", argv[2]);
        return 1;
    }

    copyContents(sourceFD, destinationFD);

    // Close the source and destination files (see copy-soln-full.c for added error checking)
    close(sourceFD);
    close(destinationFD);

    return 0;
}
