pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

commit 278f5153a3922d1a58078477bf5ad96664bd2d30
parent 524aef616fbb6e0f89a0a4269b83ae9987b8abb8
Author: gearsix <gearsix@tuta.io>
Date:   Sat, 25 Dec 2021 18:46:18 +0000

fixed, stabalised & improved copyf

Diffstat:
Mcopy.go | 25+++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/copy.go b/copy.go @@ -9,21 +9,26 @@ int copyf(const char *src, const char *dst) int ret = EXIT_FAILURE; FILE *srcf, *dstf; - if ((!(srcf = fopen(src, "r"))) || - (!(dstf = fopen(dst, "w")))) + if ((!(srcf = fopen(src, "rb"))) || + (!(dstf = fopen(dst, "wb")))) goto ABORT; + + fseek(srcf, 0, SEEK_END); + size_t siz = ftell(srcf); + rewind(srcf); - char buf[BUFSIZ]; - int n; + char buf[4096]; // 4kb blocks + size_t r, w, total = 0; do { - n = fread(buf, sizeof(char), BUFSIZ, srcf); - if (ferror(srcf)) perror("fread failure"); + r = fread(buf, sizeof(char), sizeof(buf), srcf); + if (ferror(srcf)) goto ABORT; else { - fwrite(buf, sizeof(char), n, dstf); - if (ferror(dstf)) perror("fwrite failure"); + w = fwrite(buf, sizeof(char), r, dstf); + if (ferror(dstf)) goto ABORT; + total += w; } - } while (!feof(srcf) && !ferror(srcf) && !ferror(dstf)); - ret = EXIT_SUCCESS; + } while (!feof(srcf)); + if (total == siz) ret = EXIT_SUCCESS; ABORT: if (srcf) fclose(srcf);