--- ray/src/common/wordfile.c 2016/03/22 15:14:39 2.17 +++ ray/src/common/wordfile.c 2016/05/13 09:12:19 2.22 @@ -1,24 +1,20 @@ #ifndef lint -static const char RCSid[] = "$Id: wordfile.c,v 2.17 2016/03/22 15:14:39 greg Exp $"; +static const char RCSid[] = "$Id: wordfile.c,v 2.22 2016/05/13 09:12:19 schorsch Exp $"; #endif /* * Load whitespace separated words from a file into an array. - * Assume the passed pointer array is big enough to hold them all. * - * External symbols declared in standard.h + * External symbols declared in rtio.h */ #include "copyright.h" #include #include -#include -#include -#include -#include #include "platform.h" -#include "standard.h" +#include "rtio.h" +#include "rtmisc.h" #define MAXWLEN 4096 /* words must be shorter than this */ @@ -33,6 +29,7 @@ wordfile( /* get words from fname, put in words */ { int wrdcnt = 0; int n = 0; + int dlen = 0; int fd; char buf[MAXWLEN]; /* load file into buffer */ @@ -44,8 +41,9 @@ wordfile( /* get words from fname, put in words */ return(-1); /* open error */ words[0] = NULL; while (nargs > 1 && (n += read(fd, buf+n, MAXWLEN-n)) > 0) { + dlen = n; int crem = 0; - if (n >= MAXWLEN) /* still something left? */ + if (n > MAXWLEN/2) /* check for mid-word end */ while (!isspace(buf[--n])) { if (n <= 0) /* one long word! */ goto done; @@ -57,11 +55,10 @@ wordfile( /* get words from fname, put in words */ wrdcnt = -1; /* memory error */ break; } - words += n; - nargs -= n; + words += n; nargs -= n; wrdcnt += n; if ((n = crem) > 0) /* move remainder */ - strncpy(buf, buf+MAXWLEN-crem, crem); + memmove(buf, buf+dlen-crem, crem); } done: close(fd);