--- ray/src/util/rcollate.c 2016/03/04 00:21:21 2.22 +++ ray/src/util/rcollate.c 2018/10/18 22:59:48 2.27 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rcollate.c,v 2.22 2016/03/04 00:21:21 greg Exp $"; +static const char RCSid[] = "$Id: rcollate.c,v 2.27 2018/10/18 22:59:48 greg Exp $"; #endif /* * Utility to re-order records in a binary or ASCII data file (matrix) @@ -11,13 +11,13 @@ static const char RCSid[] = "$Id: rcollate.c,v 2.22 20 #include "platform.h" #include "rtio.h" #include "resolu.h" -#ifdef _WIN32 -#undef ftello -#define ftello ftell -#undef ssize_t -#define ssize_t size_t +#if defined(_WIN32) || defined(_WIN64) + #undef ftello + #define ftello ftell + #undef ssize_t + #define ssize_t size_t #else -#include + #include #endif typedef struct { @@ -90,9 +90,10 @@ static int load_file(MEMLOAD *mp, FILE *fp) { int fd; - off_t skip, flen; + off_t skip, flen, skipped; -#ifdef _WIN32 /* too difficult to fix this */ +#if defined(_WIN32) || defined(_WIN64) + /* too difficult to fix this */ return load_stream(mp, fp); #endif if (mp == NULL) @@ -110,8 +111,9 @@ load_file(MEMLOAD *mp, FILE *fp) mp->len = (size_t)(flen - skip); #ifdef MAP_FILE if (mp->len > 1L<<20) { /* map file if > 1 MByte */ - mp->base = mmap(NULL, mp->len, PROT_READ, MAP_PRIVATE, fd, skip); + mp->base = mmap(NULL, flen, PROT_READ, MAP_PRIVATE, fd, 0); if (mp->base != MAP_FAILED) { + mp->base = (char *)mp->base + skip; mp->mapped = 1; return(1); /* mmap() success */ } @@ -123,9 +125,15 @@ load_file(MEMLOAD *mp, FILE *fp) mp->len = 0; return(-1); } - if (read(fd, (char *)mp->base, mp->len) != mp->len) { - free_load(mp); - return(-1); + skipped = skip; /* read() fails on really big buffers */ + while (skipped < flen) { + ssize_t nread = read(fd, (char *)mp->base+(skipped-skip), + (flen-skipped <= 1L<<30) ? flen-skipped : 1L<<30); + if (nread <= 0) { + free_load(mp); + return(-1); + } + skipped += nread; } return(1); } @@ -137,6 +145,7 @@ load_file(MEMLOAD *mp, FILE *fp) static RECINDEX * index_records(const MEMLOAD *mp, int nw_rec) { + int nall = 0; RECINDEX *rp; char *cp, *mend; int n; @@ -145,7 +154,8 @@ index_records(const MEMLOAD *mp, int nw_rec) return(NULL); if (nw_rec <= 0) return(NULL); - rp = (RECINDEX *)malloc(sizeof(RECINDEX) + mp->len/(2*nw_rec)*sizeof(char *)); + nall = 1000; + rp = (RECINDEX *)malloc(sizeof(RECINDEX) + nall*sizeof(char *)); if (rp == NULL) return(NULL); rp->nw_rec = nw_rec; @@ -157,6 +167,13 @@ index_records(const MEMLOAD *mp, int nw_rec) ++cp; if (cp >= mend) break; + if (rp->nrecs >= nall) { + nall += nall>>1; /* get more record space */ + rp = (RECINDEX *)realloc(rp, + sizeof(RECINDEX) + nall*sizeof(char *)); + if (rp == NULL) + return(NULL); + } rp->rec[rp->nrecs++] = cp; /* point to first non-white */ n = rp->nw_rec; while (++cp < mend) /* find end of record */ @@ -352,9 +369,9 @@ do_transpose(const MEMLOAD *mp) print_record(rp, j*ni_columns + i); putc(tabEOL[j >= no_columns-1], stdout); } else { /* binary output */ - fwrite((char *)mp->base + - (n_comp*comp_size)*(j*ni_columns + i), - n_comp*comp_size, 1, stdout); + putbinary((char *)mp->base + + (unsigned long)(n_comp*comp_size)*(j*ni_columns + i), + comp_size, n_comp, stdout); } if (ferror(stdout)) { fprintf(stderr, "Error writing to stdout\n"); @@ -431,7 +448,7 @@ done: static int headline(char *s, void *p) { - static char fmt[32]; + static char fmt[MAXFMTLEN]; int n; if (formatval(fmt, s)) {