From b93f70544f62811a35afc9d607fb933fb061f370 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 24 Nov 2018 19:06:45 +0000 Subject: [PATCH] progs/cc-progress.c: Use `fstat' to discover the file size. And `lseek' to discover the current offset. Annoyingly, Android only developed `ftello64' and `fseeko64' in API24, so we can't use these (and it was a pretty grim circumlocution anyway). On the other hand, Android has had `lseek64' forever, and its `fstat' is natively 64-bit; and there's no portability benefit to using the other functions because Windows doesn't have them anyway. (Indeed, `lseek' and `stat' are ancient Unix, so probably more portable.) --- progs/cc-progress.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/progs/cc-progress.c b/progs/cc-progress.c index 918eb2c4..055556c3 100644 --- a/progs/cc-progress.c +++ b/progs/cc-progress.c @@ -31,6 +31,8 @@ #include "config.h" +#include + #include "cc.h" #ifndef PATHSEP @@ -129,16 +131,15 @@ static void prhuman_data(FILE *fp, off_t n) int fprogress_init(fprogress *f, const char *name, FILE *fp) { const char *p; + struct stat st; off_t o, sz = -1; size_t n; /* --- Set up the offset --- */ - if ((o = ftello(fp)) >= 0 && - fseeko(fp, 0, SEEK_END) >= 0 && - (sz = ftello(fp), - fseeko(fp, o, SEEK_SET) < 0)) - return (-1); + o = lseek(fileno(fp), 0, SEEK_CUR); + if (fstat(fileno(fp), &st)) return (-1); + sz = (S_ISREG(st.st_mode)) ? st.st_size : -1; if (o != -1 && sz != -1) sz -= o; f->o = f->olast = 0; f->sz = sz; -- 2.11.0