--- ray/src/common/wordfile.c 1992/03/04 09:59:19 2.1 +++ ray/src/common/wordfile.c 2004/03/28 20:33:12 2.13 @@ -1,63 +1,78 @@ -/* 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.13 2004/03/28 20:33:12 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 */ -#define NULL 0 +#include "copyright.h" -#define BUFSIZ 4096 /* file must be smaller than this */ - #include +#include +#include +#include +#include +#include -extern char *bmalloc(); +#include "platform.h" +#include "standard.h" +#define MAXFLEN 8192 /* file must be smaller than this */ + + int wordfile(words, fname) /* get words from fname, put in words */ 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 (fname == NULL) + return(-1); /* no filename */ 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)) { - cp++; - if (--n <= 0) - return(wp - words); - } - *wp++ = cp; - while (!isspace(*cp)) { - cp++; - if (--n <= 0) - break; - } - *cp++ = '\0'; n--; + 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); + ap = avl; /* parse into words */ + for ( ; ; ) { + while (isspace(*cp)) /* nullify spaces */ + *cp++ = '\0'; + if (!*cp) /* all done? */ + break; + *ap++ = cp; /* add argument to list */ + while (*++cp && !isspace(*cp)) + ; } - return(wp - words); -err: - bfree(buf, BUFSIZ); - return(-1); + *ap = NULL; + return(ap - avl); }