From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/fsize.c | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/fsize.c (limited to 'reference/C/CONTRIB/SNIP/fsize.c') diff --git a/reference/C/CONTRIB/SNIP/fsize.c b/reference/C/CONTRIB/SNIP/fsize.c new file mode 100755 index 0000000..319a719 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/fsize.c @@ -0,0 +1,78 @@ +/* +** FSIZE.C - Determine apparent file size of buffered file. Returns size +** corrected for text mode character translation. +** +** public domain demo by Bob Stout +*/ + +#include +#include + +long fsize(FILE *fp) +{ + size_t bufsize, bytes_read; + char *bufptr; + long size = 0L, pos; + + for (bufsize = 0x8000; NULL == (bufptr = malloc(bufsize)); bufsize /= 2) + ; + if (!bufptr) + return -1L; + pos = ftell(fp); + do + { + bytes_read = fread(bufptr, sizeof(char), bufsize, fp); + size += bytes_read; + } while (bytes_read); + free(bufptr); + fseek(fp, pos, SEEK_SET); + return size; +} + +#ifdef TEST + +#include + +#ifdef MSDOS + #define fl(x) filelength(x) + #define getsize(fp) fl(fileno(fp)) +#else + #define fl(x) puts("Install compiler-specific file length function here") + #define getsize(fp) fl(fp) +#endif + +int main(int argc, char *argv[]) +{ + FILE *fp; + long size, csize, lsize; + char buf[256]; + + while (--argc) + { + if (NULL == (fp = fopen(*++argv, "r"))) + printf("Can't open %s\n", *argv); + + size = getsize(fp); + printf("\n\"Real\" size of %s is %ld\n", *argv, size); + + for (csize = 0L; EOF != fgetc(fp); ++csize) + ; + rewind(fp); + + for (lsize = 0L; !feof(fp); ) + { + if (NULL != fgets(buf, 256, fp)) + lsize += strlen(buf); + } + rewind(fp); + + printf("fsize() returned a size = %s is %ld\n", + *argv, fsize(fp)); + printf("Reading chars returned an apparent size of %ld\n", + csize); + printf("Reading lines returned an apparent size of %ld\n", + lsize); + } +} + +#endif -- cgit v1.2.3-54-g00ecf