1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Load whitespace separated words from a file into an array. |
6 |
|
* Assume the passed pointer array is big enough to hold them all. |
7 |
+ |
* |
8 |
+ |
* External symbols declared in standard.h |
9 |
|
*/ |
10 |
|
|
11 |
< |
#define NULL 0 |
11 |
> |
#include "copyright.h" |
12 |
|
|
14 |
– |
#define BUFSIZ 4096 /* file must be smaller than this */ |
15 |
– |
|
13 |
|
#include <ctype.h> |
14 |
|
|
15 |
+ |
#define NULL 0 |
16 |
+ |
|
17 |
+ |
#define MAXFLEN 8192 /* file must be smaller than this */ |
18 |
+ |
|
19 |
|
extern char *bmalloc(); |
20 |
|
|
21 |
|
|
24 |
|
char **words; |
25 |
|
char *fname; |
26 |
|
{ |
26 |
– |
char **wp = words; |
27 |
|
int fd; |
28 |
< |
char *buf; |
28 |
> |
char buf[MAXFLEN]; |
29 |
|
register int n; |
30 |
< |
register char *cp; |
31 |
< |
/* allocate memory */ |
32 |
< |
if ((cp = buf = bmalloc(BUFSIZ)) == NULL) |
33 |
< |
return(-1); |
30 |
> |
/* load file into buffer */ |
31 |
> |
if (fname == NULL) |
32 |
> |
return(-1); /* no filename */ |
33 |
|
if ((fd = open(fname, 0)) < 0) |
34 |
< |
goto err; |
35 |
< |
n = read(fd, buf, BUFSIZ); |
34 |
> |
return(-1); /* open error */ |
35 |
> |
n = read(fd, buf, MAXFLEN); |
36 |
|
close(fd); |
37 |
< |
if (n < 0) |
38 |
< |
goto err; |
39 |
< |
if (n == BUFSIZ) /* file too big, take what we can */ |
37 |
> |
if (n < 0) /* read error */ |
38 |
> |
return(-1); |
39 |
> |
if (n == MAXFLEN) /* file too big, take what we can */ |
40 |
|
while (!isspace(buf[--n])) |
41 |
< |
if (n <= 0) |
42 |
< |
goto err; |
43 |
< |
bfree(buf+n+1, BUFSIZ-n-1); /* return unneeded memory */ |
44 |
< |
while (n > 0) { /* break buffer into words */ |
45 |
< |
while (isspace(*cp)) { |
46 |
< |
cp++; |
47 |
< |
if (--n <= 0) |
48 |
< |
return(wp - words); |
49 |
< |
} |
50 |
< |
*wp++ = cp; |
51 |
< |
while (!isspace(*cp)) { |
52 |
< |
cp++; |
53 |
< |
if (--n <= 0) |
54 |
< |
break; |
55 |
< |
} |
56 |
< |
*cp++ = '\0'; n--; |
41 |
> |
if (n <= 0) /* one long word! */ |
42 |
> |
return(-1); |
43 |
> |
buf[n] = '\0'; /* terminate */ |
44 |
> |
return(wordstring(words, buf)); /* wordstring does the rest */ |
45 |
> |
} |
46 |
> |
|
47 |
> |
|
48 |
> |
int |
49 |
> |
wordstring(avl, str) /* allocate and load argument list */ |
50 |
> |
char **avl; |
51 |
> |
char *str; |
52 |
> |
{ |
53 |
> |
register char *cp, **ap; |
54 |
> |
|
55 |
> |
if (str == NULL) |
56 |
> |
return(-1); |
57 |
> |
cp = bmalloc(strlen(str)+1); |
58 |
> |
if (cp == NULL) /* ENOMEM */ |
59 |
> |
return(-1); |
60 |
> |
strcpy(cp, str); |
61 |
> |
ap = avl; /* parse into words */ |
62 |
> |
for ( ; ; ) { |
63 |
> |
while (isspace(*cp)) /* nullify spaces */ |
64 |
> |
*cp++ = '\0'; |
65 |
> |
if (!*cp) /* all done? */ |
66 |
> |
break; |
67 |
> |
*ap++ = cp; /* add argument to list */ |
68 |
> |
while (*++cp && !isspace(*cp)) |
69 |
> |
; |
70 |
|
} |
71 |
< |
return(wp - words); |
72 |
< |
err: |
61 |
< |
bfree(buf, BUFSIZ); |
62 |
< |
return(-1); |
71 |
> |
*ap = NULL; |
72 |
> |
return(ap - avl); |
73 |
|
} |