Review generated file copy

from C file I/O
C23 (GCC 13.3.0) advanced 12 min 5 issues to find

Review this generated function for copying an arbitrary file.

Copy arbitrary bytes from an existing source to a newly created destination, never replace an existing destination, handle short transfers and every I/O error, and report success only after closing succeeds.

C
#include <stdio.h>

int copy_file(const char *source_path, const char *destination_path) {
    FILE *source = fopen(source_path, "r");
    if (source == NULL) {
        return -1;
    }

    FILE *destination = fopen(destination_path, "wb");
    unsigned char buffer[4096];

    while (!feof(source)) {
        size_t count = fread(buffer, 1, sizeof buffer, source);
        (void)count;
        fwrite(buffer, sizeof buffer, 1, destination);
        fflush(destination);
    }

    fclose(source);
    fclose(destination);
    return 0;
}

generated code is illustrative, not from any one model

Open in playground
Report an error