| 1 |
/* Copyright (c) 1992 Regents of the University of California */
|
| 2 |
|
| 3 |
#ifndef lint
|
| 4 |
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
#endif
|
| 6 |
|
| 7 |
/*
|
| 8 |
* Load whitespace separated words from a file into an array.
|
| 9 |
* Assume the passed pointer array is big enough to hold them all.
|
| 10 |
*/
|
| 11 |
|
| 12 |
#define NULL 0
|
| 13 |
|
| 14 |
#define BUFSIZ 4096 /* file must be smaller than this */
|
| 15 |
|
| 16 |
#include <ctype.h>
|
| 17 |
|
| 18 |
extern char *bmalloc();
|
| 19 |
|
| 20 |
|
| 21 |
int
|
| 22 |
wordfile(words, fname) /* get words from fname, put in words */
|
| 23 |
char **words;
|
| 24 |
char *fname;
|
| 25 |
{
|
| 26 |
char **wp = words;
|
| 27 |
int fd;
|
| 28 |
char *buf;
|
| 29 |
register int n;
|
| 30 |
register char *cp;
|
| 31 |
/* allocate memory */
|
| 32 |
if ((cp = buf = bmalloc(BUFSIZ)) == NULL)
|
| 33 |
return(-1);
|
| 34 |
if ((fd = open(fname, 0)) < 0)
|
| 35 |
goto err;
|
| 36 |
n = read(fd, buf, BUFSIZ);
|
| 37 |
close(fd);
|
| 38 |
if (n < 0)
|
| 39 |
goto err;
|
| 40 |
if (n == BUFSIZ) /* file too big, take what we can */
|
| 41 |
while (!isspace(buf[--n]))
|
| 42 |
if (n <= 0)
|
| 43 |
goto err;
|
| 44 |
bfree(buf+n+1, BUFSIZ-n-1); /* return unneeded memory */
|
| 45 |
while (n > 0) { /* break buffer into words */
|
| 46 |
while (isspace(*cp)) {
|
| 47 |
cp++;
|
| 48 |
if (--n <= 0)
|
| 49 |
return(wp - words);
|
| 50 |
}
|
| 51 |
*wp++ = cp;
|
| 52 |
while (!isspace(*cp)) {
|
| 53 |
cp++;
|
| 54 |
if (--n <= 0)
|
| 55 |
break;
|
| 56 |
}
|
| 57 |
*cp++ = '\0'; n--;
|
| 58 |
}
|
| 59 |
return(wp - words);
|
| 60 |
err:
|
| 61 |
bfree(buf, BUFSIZ);
|
| 62 |
return(-1);
|
| 63 |
}
|