--- ray/src/common/wordfile.c 1992/10/16 13:02:46 2.2 +++ ray/src/common/wordfile.c 2016/04/27 17:26:45 2.20 @@ -1,64 +1,95 @@ -/* 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.20 2016/04/27 17:26:45 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 rtio.h */ +#include "copyright.h" + #include +#include -#define NULL 0 +#include "platform.h" +#include "rtio.h" +#include "rtmisc.h" -#define MAXFLEN 10240 /* 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 +) { - char **wp = words; + int wrdcnt = 0; + int n = 0; int fd; - char *buf; - register int n; - register char *cp; - /* allocate memory */ - if ((cp = buf = bmalloc(MAXFLEN)) == NULL) + 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) - goto err; - n = read(fd, buf, MAXFLEN); - close(fd); - if (n < 0) - goto err; - if (n == MAXFLEN) /* file too big, take what we can */ - while (!isspace(buf[--n])) - if (n <= 0) - goto err; - bfree(buf+n+1, MAXFLEN-n-1); /* return unneeded memory */ - while (n > 0) { /* break buffer into words */ - while (isspace(*cp)) { - cp++; - if (--n <= 0) - return(wp - words); + return(-1); /* open error */ + words[0] = NULL; + while (nargs > 1 && (n += read(fd, buf+n, MAXWLEN-n)) > 0) { + int crem = 0; + if (n > MAXWLEN/2) /* check for mid-word end */ + 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; } - *wp++ = cp; - while (!isspace(*cp)) { - cp++; - if (--n <= 0) - break; - } - *cp++ = '\0'; n--; + words += n; nargs -= n; + wrdcnt += n; + if ((n = crem) > 0) /* move remainder */ + memmove(buf, buf+MAXWLEN-crem, crem); } - *wp = NULL; /* null terminator */ - return(wp - words); -err: - bfree(buf, MAXFLEN); - return(-1); +done: + close(fd); + return(wrdcnt); +} + + +int +wordstring( /* allocate and load argument list */ + char **avl, + int nargs, + char *str +) +{ + char *cp, **ap; + + if (str == NULL) + return(-1); + cp = bmalloc(strlen(str)+1); + if (cp == NULL) /* ENOMEM */ + return(-1); + strcpy(cp, str); + /* 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 */ + while (*++cp && !isspace(*cp)) + ; + } + *cp = '\0'; /* terminates overflow */ + *ap = NULL; + return(ap - avl); }