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 BUFSIZ 4096 /* file must be smaller than this */ |
16 |
> |
#define MAXFLEN 8192 /* file must be smaller than this */ |
17 |
|
|
16 |
– |
#include <ctype.h> |
17 |
– |
|
18 |
|
extern char *bmalloc(); |
19 |
|
|
20 |
|
|
23 |
|
char **words; |
24 |
|
char *fname; |
25 |
|
{ |
26 |
– |
char **wp = words; |
26 |
|
int fd; |
27 |
< |
char *buf; |
27 |
> |
char buf[MAXFLEN]; |
28 |
|
register int n; |
29 |
< |
register char *cp; |
31 |
< |
/* allocate memory */ |
32 |
< |
if ((cp = buf = bmalloc(BUFSIZ)) == NULL) |
33 |
< |
return(-1); |
29 |
> |
/* load file into buffer */ |
30 |
|
if ((fd = open(fname, 0)) < 0) |
31 |
< |
goto err; |
32 |
< |
n = read(fd, buf, BUFSIZ); |
31 |
> |
return(-1); /* open error */ |
32 |
> |
n = read(fd, buf, MAXFLEN); |
33 |
|
close(fd); |
34 |
< |
if (n < 0) |
35 |
< |
goto err; |
36 |
< |
if (n == BUFSIZ) /* file too big, take what we can */ |
34 |
> |
if (n < 0) /* read error */ |
35 |
> |
return(-1); |
36 |
> |
if (n == MAXFLEN) /* file too big, take what we can */ |
37 |
|
while (!isspace(buf[--n])) |
38 |
< |
if (n <= 0) |
39 |
< |
goto err; |
40 |
< |
bfree(buf+n+1, BUFSIZ-n-1); /* return unneeded memory */ |
41 |
< |
while (n > 0) { /* break buffer into words */ |
42 |
< |
while (isspace(*cp)) { |
38 |
> |
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 |
> |
for (ap = avl; *cp; *cp++ = '\0') { |
59 |
> |
while (isspace(*cp)) /* skip leading space */ |
60 |
|
cp++; |
61 |
< |
if (--n <= 0) |
62 |
< |
return(wp - words); |
61 |
> |
if (*cp) { /* add argument to list */ |
62 |
> |
*ap++ = cp; |
63 |
> |
while (*cp && !isspace(*cp)) |
64 |
> |
cp++; |
65 |
|
} |
51 |
– |
*wp++ = cp; |
52 |
– |
while (!isspace(*cp)) { |
53 |
– |
cp++; |
54 |
– |
if (--n <= 0) |
55 |
– |
break; |
56 |
– |
} |
57 |
– |
*cp++ = '\0'; n--; |
66 |
|
} |
67 |
< |
return(wp - words); |
68 |
< |
err: |
61 |
< |
bfree(buf, BUFSIZ); |
62 |
< |
return(-1); |
67 |
> |
*ap = NULL; |
68 |
> |
return(ap - avl); |
69 |
|
} |