1 |
greg |
2.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 |
greg |
2.2 |
#include <ctype.h> |
13 |
|
|
|
14 |
greg |
2.1 |
#define NULL 0 |
15 |
|
|
|
16 |
greg |
2.3 |
#define MAXFLEN 8192 /* file must be smaller than this */ |
17 |
greg |
2.1 |
|
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 |
|
|
int fd; |
27 |
greg |
2.3 |
char buf[MAXFLEN]; |
28 |
greg |
2.1 |
register int n; |
29 |
greg |
2.3 |
/* load file into buffer */ |
30 |
greg |
2.1 |
if ((fd = open(fname, 0)) < 0) |
31 |
greg |
2.3 |
return(-1); /* open error */ |
32 |
greg |
2.2 |
n = read(fd, buf, MAXFLEN); |
33 |
greg |
2.1 |
close(fd); |
34 |
greg |
2.3 |
if (n < 0) /* read error */ |
35 |
|
|
return(-1); |
36 |
greg |
2.2 |
if (n == MAXFLEN) /* file too big, take what we can */ |
37 |
greg |
2.1 |
while (!isspace(buf[--n])) |
38 |
greg |
2.3 |
if (n <= 0) /* one long word! */ |
39 |
|
|
return(-1); |
40 |
|
|
buf[n] = '\0'; /* terminate */ |
41 |
|
|
return(wordstring(words, buf)); /* wordstring does the rest */ |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
int |
46 |
|
|
wordstring(avl, str) /* allocate and load argument list */ |
47 |
|
|
char **avl; |
48 |
|
|
char *str; |
49 |
|
|
{ |
50 |
|
|
register char *cp, **ap; |
51 |
|
|
|
52 |
|
|
if (str == NULL) |
53 |
|
|
return(-1); |
54 |
|
|
cp = bmalloc(strlen(str)+1); |
55 |
|
|
if (cp == NULL) /* ENOMEM */ |
56 |
|
|
return(-1); |
57 |
|
|
strcpy(cp, str); |
58 |
greg |
2.4 |
ap = avl; /* parse into words */ |
59 |
|
|
for ( ; ; ) { |
60 |
|
|
while (isspace(*cp)) /* nullify spaces */ |
61 |
|
|
*cp++ = '\0'; |
62 |
|
|
if (!*cp) /* all done? */ |
63 |
|
|
break; |
64 |
|
|
*ap++ = cp; /* add argument to list */ |
65 |
greg |
2.5 |
while (*cp && !isspace(*cp)) |
66 |
|
|
cp++; |
67 |
greg |
2.1 |
} |
68 |
greg |
2.3 |
*ap = NULL; |
69 |
|
|
return(ap - avl); |
70 |
greg |
2.1 |
} |