--- ray/src/common/wordfile.c 1992/03/04 09:59:19 2.1 +++ ray/src/common/wordfile.c 1992/10/17 08:08:02 2.3 @@ -9,12 +9,12 @@ static char SCCSid[] = "$SunId$ LBL"; * Assume the passed pointer array is big enough to hold them all. */ +#include + #define NULL 0 -#define BUFSIZ 4096 /* file must be smaller than this */ +#define MAXFLEN 8192 /* file must be smaller than this */ -#include - extern char *bmalloc(); @@ -23,41 +23,47 @@ wordfile(words, fname) /* get words from fname, put i char **words; char *fname; { - char **wp = words; int fd; - char *buf; + char buf[MAXFLEN]; register int n; - register char *cp; - /* allocate memory */ - if ((cp = buf = bmalloc(BUFSIZ)) == NULL) - return(-1); + /* load file into buffer */ if ((fd = open(fname, 0)) < 0) - goto err; - n = read(fd, buf, BUFSIZ); + return(-1); /* open error */ + n = read(fd, buf, MAXFLEN); close(fd); - if (n < 0) - goto err; - if (n == BUFSIZ) /* file too big, take what we can */ + if (n < 0) /* read error */ + return(-1); + if (n == MAXFLEN) /* file too big, take what we can */ while (!isspace(buf[--n])) - if (n <= 0) - goto err; - bfree(buf+n+1, BUFSIZ-n-1); /* return unneeded memory */ - while (n > 0) { /* break buffer into words */ - while (isspace(*cp)) { + if (n <= 0) /* one long word! */ + return(-1); + buf[n] = '\0'; /* terminate */ + return(wordstring(words, buf)); /* wordstring does the rest */ +} + + +int +wordstring(avl, str) /* allocate and load argument list */ +char **avl; +char *str; +{ + register char *cp, **ap; + + if (str == NULL) + return(-1); + cp = bmalloc(strlen(str)+1); + if (cp == NULL) /* ENOMEM */ + return(-1); + strcpy(cp, str); + for (ap = avl; *cp; *cp++ = '\0') { + while (isspace(*cp)) /* skip leading space */ cp++; - if (--n <= 0) - return(wp - words); + if (*cp) { /* add argument to list */ + *ap++ = cp; + while (*cp && !isspace(*cp)) + cp++; } - *wp++ = cp; - while (!isspace(*cp)) { - cp++; - if (--n <= 0) - break; - } - *cp++ = '\0'; n--; } - return(wp - words); -err: - bfree(buf, BUFSIZ); - return(-1); + *ap = NULL; + return(ap - avl); }