salamude <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <string.h>

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif

int
main(int argc, char *argv[])
{
    int inputFd, outputFd, openFlags;
    mode_t filePerms;
    ssize_t numRead;
    char buf[BUF_SIZE];

    if (argc != 3 || strcmp(argv[1], "--help") == 0)
        errx(EXIT_FAILURE, "usage: %s old-file new-file", argv[0]);

    inputFd = open(argv[1], O_RDONLY);
    if (inputFd == -1)
        err(EXIT_FAILURE, "opening file %s", argv[1]);

    openFlags = O_CREAT | O_WRONLY | O_TRUNC;
    filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                S_IROTH | S_IWOTH;      /* rw-rw-rw- */
    outputFd = open(argv[2], openFlags, filePerms);
    if (outputFd == -1)
        err(EXIT_FAILURE, "openning file %s", argv[2]);

    while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
        if (write(outputFd, buf, numRead) != numRead)
            err(EXIT_FAILURE, "write error");
    if (numRead == -1)
        err(EXIT_FAILURE, "read error");

    if (close(inputFd) == -1)
        err(EXIT_FAILURE, "close input");
    if (close(outputFd) == -1)
        err(EXIT_FAILURE, "close output");

    exit(EXIT_SUCCESS);
}

