--- ray/src/common/wordfile.c 1993/04/02 09:11:35 2.6 +++ ray/src/common/wordfile.c 2016/03/22 15:14:39 2.17 @@ -1,53 +1,82 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: wordfile.c,v 2.17 2016/03/22 15:14:39 greg 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 */ +#include "copyright.h" + #include +#include +#include +#include +#include +#include -#define NULL 0 +#include "platform.h" +#include "standard.h" -#define MAXFLEN 8192 /* file must be smaller than this */ -extern char *bmalloc(); +#define MAXWLEN 4096 /* words must be shorter than this */ int -wordfile(words, fname) /* get words from fname, put in words */ -char **words; -char *fname; +wordfile( /* get words from fname, put in words */ + char **words, + int nargs, + char *fname +) { + int wrdcnt = 0; + int n = 0; int fd; - char buf[MAXFLEN]; - register int n; + char buf[MAXWLEN]; /* load file into buffer */ + if (fname == NULL || !*fname) + return(-1); /* no filename */ + if (nargs <= 1) + return(-1); if ((fd = open(fname, 0)) < 0) return(-1); /* open error */ - n = read(fd, buf, MAXFLEN); + words[0] = NULL; + while (nargs > 1 && (n += read(fd, buf+n, MAXWLEN-n)) > 0) { + int crem = 0; + if (n >= MAXWLEN) /* still something left? */ + while (!isspace(buf[--n])) { + if (n <= 0) /* one long word! */ + goto done; + ++crem; + } + buf[n] = '\0'; /* terminate & parse */ + n = wordstring(words, nargs, buf); + if (n < 0) { + wrdcnt = -1; /* memory error */ + break; + } + words += n; + nargs -= n; + wrdcnt += n; + if ((n = crem) > 0) /* move remainder */ + strncpy(buf, buf+MAXWLEN-crem, crem); + } +done: close(fd); - if (n < 0) /* read error */ - return(-1); - if (n == MAXFLEN) /* file too big, take what we can */ - while (!isspace(buf[--n])) - if (n <= 0) /* one long word! */ - return(-1); - buf[n] = '\0'; /* terminate */ - return(wordstring(words, buf)); /* wordstring does the rest */ + return(wrdcnt); } int -wordstring(avl, str) /* allocate and load argument list */ -char **avl; -char *str; +wordstring( /* allocate and load argument list */ + char **avl, + int nargs, + char *str +) { - register char *cp, **ap; + char *cp, **ap; if (str == NULL) return(-1); @@ -55,16 +84,17 @@ char *str; if (cp == NULL) /* ENOMEM */ return(-1); strcpy(cp, str); - ap = avl; /* parse into words */ - for ( ; ; ) { + /* parse into words */ + for (ap = avl; ap-avl < nargs-1; ap++) { while (isspace(*cp)) /* nullify spaces */ *cp++ = '\0'; if (!*cp) /* all done? */ break; - *ap++ = cp; /* add argument to list */ + *ap = cp; /* add argument to list */ while (*++cp && !isspace(*cp)) ; } + *cp = '\0'; /* terminates overflow */ *ap = NULL; return(ap - avl); }