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 |
#include <ctype.h> |
13 |
|
14 |
#define NULL 0 |
15 |
|
16 |
#define MAXFLEN 10240 /* file must be smaller than this */ |
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(MAXFLEN)) == NULL) |
33 |
return(-1); |
34 |
if ((fd = open(fname, 0)) < 0) |
35 |
goto err; |
36 |
n = read(fd, buf, MAXFLEN); |
37 |
close(fd); |
38 |
if (n < 0) |
39 |
goto err; |
40 |
if (n == MAXFLEN) /* file too big, take what we can */ |
41 |
while (!isspace(buf[--n])) |
42 |
if (n <= 0) |
43 |
goto err; |
44 |
bfree(buf+n+1, MAXFLEN-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 |
*wp = NULL; /* null terminator */ |
60 |
return(wp - words); |
61 |
err: |
62 |
bfree(buf, MAXFLEN); |
63 |
return(-1); |
64 |
} |