/**
 * File: copy-extended.c
 * --------------
 * This program is an implementation of an extended copy command that shows how we can
 * make a copy of a specified file into multiple other specified files, as well
 * as printing the contents to the terminal.
 */

#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 is a helper function wrapper around `write` that guarantees the first
 * nbytes bytes in buffer will be written to all destinationFDs (unless write
 * encounters an error and returns -1 - in this implementation, this is
 * assumed not to happen, but full error checking is implemented in
 * copy-extended-soln-full.c).
 */
void writeAllBytes(int destinationFDs[], int nFDs, char buffer[], size_t nbytes) {
    // For each destination file, write nbytes
    for (int i = 0; i < nFDs; i++) {
        size_t bytesWritten = 0;
        while (bytesWritten < nbytes) {
            ssize_t count = write(destinationFDs[i], buffer + bytesWritten, nbytes - bytesWritten);

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

            bytesWritten += count;
        }
    }
}

/* This function copies the contents of the source file into each destination
 * file in chunks until the entire file is copied.
 */
void copyContents(int sourceFD, int destinationFDs[], int nFDs) {
    while (true) {
        char buffer[kCopyIncrement];

        // Read the next chunk of bytes from the source
        ssize_t bytesRead = read(sourceFD, buffer, sizeof(buffer));

        // check if we are at the end of the file
        if (bytesRead == 0) break;

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

        writeAllBytes(destinationFDs, nFDs, buffer, bytesRead);
    }
}

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-extended-soln-full.c for added error checking)

    // Open the specified destination file(s) for writing, creating if nonexistent
    int destinationFDs[argc - 1];

    // Include the terminal (STDOUT) as the first "file" so it's also printed
    destinationFDs[0] = STDOUT_FILENO;

    for (size_t i = 2; i < argc; i++) {
        destinationFDs[i - 1] = open(argv[i], O_WRONLY | O_CREAT | O_EXCL, kDefaultPermissions);
        // open error checking omitted (see copy-extended-soln-full.c for added error checking)
    }

    copyContents(sourceFD, destinationFDs, argc - 1);

    // Close the files (except for STDOUT) (see copy-extended-soln-full.c for added error checking)
    close(sourceFD);
    for (size_t i = 1; i < argc - 1; i++) {
        close(destinationFDs[i]);
    }

    return 0;
}
