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 |
+ |
#include <string.h> |
15 |
+ |
#include <stdio.h> |
16 |
+ |
#include <sys/types.h> |
17 |
+ |
#include <sys/stat.h> |
18 |
+ |
#include <fcntl.h> |
19 |
|
|
20 |
+ |
#ifdef _WIN32 |
21 |
+ |
#include <io.h> |
22 |
+ |
#endif |
23 |
+ |
|
24 |
+ |
#include "standard.h" |
25 |
+ |
|
26 |
+ |
|
27 |
+ |
#define MAXFLEN 8192 /* file must be smaller than this */ |
28 |
+ |
|
29 |
|
extern char *bmalloc(); |
30 |
|
|
31 |
|
|
34 |
|
char **words; |
35 |
|
char *fname; |
36 |
|
{ |
26 |
– |
char **wp = words; |
37 |
|
int fd; |
38 |
< |
char *buf; |
38 |
> |
char buf[MAXFLEN]; |
39 |
|
register int n; |
40 |
< |
register char *cp; |
41 |
< |
/* allocate memory */ |
42 |
< |
if ((cp = buf = bmalloc(BUFSIZ)) == NULL) |
33 |
< |
return(-1); |
40 |
> |
/* load file into buffer */ |
41 |
> |
if (fname == NULL) |
42 |
> |
return(-1); /* no filename */ |
43 |
|
if ((fd = open(fname, 0)) < 0) |
44 |
< |
goto err; |
45 |
< |
n = read(fd, buf, BUFSIZ); |
44 |
> |
return(-1); /* open error */ |
45 |
> |
n = read(fd, buf, MAXFLEN); |
46 |
|
close(fd); |
47 |
< |
if (n < 0) |
48 |
< |
goto err; |
49 |
< |
if (n == BUFSIZ) /* file too big, take what we can */ |
47 |
> |
if (n < 0) /* read error */ |
48 |
> |
return(-1); |
49 |
> |
if (n == MAXFLEN) /* file too big, take what we can */ |
50 |
|
while (!isspace(buf[--n])) |
51 |
< |
if (n <= 0) |
52 |
< |
goto err; |
53 |
< |
bfree(buf+n+1, BUFSIZ-n-1); /* return unneeded memory */ |
54 |
< |
while (n > 0) { /* break buffer into words */ |
55 |
< |
while (isspace(*cp)) { |
56 |
< |
cp++; |
57 |
< |
if (--n <= 0) |
58 |
< |
return(wp - words); |
59 |
< |
} |
60 |
< |
*wp++ = cp; |
61 |
< |
while (!isspace(*cp)) { |
62 |
< |
cp++; |
63 |
< |
if (--n <= 0) |
64 |
< |
break; |
65 |
< |
} |
66 |
< |
*cp++ = '\0'; n--; |
51 |
> |
if (n <= 0) /* one long word! */ |
52 |
> |
return(-1); |
53 |
> |
buf[n] = '\0'; /* terminate */ |
54 |
> |
return(wordstring(words, buf)); /* wordstring does the rest */ |
55 |
> |
} |
56 |
> |
|
57 |
> |
|
58 |
> |
int |
59 |
> |
wordstring(avl, str) /* allocate and load argument list */ |
60 |
> |
char **avl; |
61 |
> |
char *str; |
62 |
> |
{ |
63 |
> |
register char *cp, **ap; |
64 |
> |
|
65 |
> |
if (str == NULL) |
66 |
> |
return(-1); |
67 |
> |
cp = bmalloc(strlen(str)+1); |
68 |
> |
if (cp == NULL) /* ENOMEM */ |
69 |
> |
return(-1); |
70 |
> |
strcpy(cp, str); |
71 |
> |
ap = avl; /* parse into words */ |
72 |
> |
for ( ; ; ) { |
73 |
> |
while (isspace(*cp)) /* nullify spaces */ |
74 |
> |
*cp++ = '\0'; |
75 |
> |
if (!*cp) /* all done? */ |
76 |
> |
break; |
77 |
> |
*ap++ = cp; /* add argument to list */ |
78 |
> |
while (*++cp && !isspace(*cp)) |
79 |
> |
; |
80 |
|
} |
81 |
< |
return(wp - words); |
82 |
< |
err: |
61 |
< |
bfree(buf, BUFSIZ); |
62 |
< |
return(-1); |
81 |
> |
*ap = NULL; |
82 |
> |
return(ap - avl); |
83 |
|
} |