1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.15 |
static const char RCSid[] = "$Id: wordfile.c,v 2.14 2004/11/05 17:36:55 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
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 |
greg |
2.8 |
* |
8 |
|
|
* External symbols declared in standard.h |
9 |
|
|
*/ |
10 |
|
|
|
11 |
greg |
2.9 |
#include "copyright.h" |
12 |
greg |
2.1 |
|
13 |
greg |
2.2 |
#include <ctype.h> |
14 |
schorsch |
2.10 |
#include <string.h> |
15 |
|
|
#include <stdio.h> |
16 |
|
|
#include <sys/types.h> |
17 |
|
|
#include <sys/stat.h> |
18 |
|
|
#include <fcntl.h> |
19 |
|
|
|
20 |
greg |
2.12 |
#include "platform.h" |
21 |
schorsch |
2.10 |
#include "standard.h" |
22 |
greg |
2.2 |
|
23 |
greg |
2.1 |
|
24 |
greg |
2.14 |
#ifndef MAXFLEN |
25 |
greg |
2.15 |
#define MAXFLEN 65536 /* file must be smaller than this */ |
26 |
greg |
2.14 |
#endif |
27 |
greg |
2.1 |
|
28 |
|
|
|
29 |
|
|
int |
30 |
|
|
wordfile(words, fname) /* get words from fname, put in words */ |
31 |
|
|
char **words; |
32 |
|
|
char *fname; |
33 |
|
|
{ |
34 |
|
|
int fd; |
35 |
greg |
2.3 |
char buf[MAXFLEN]; |
36 |
greg |
2.1 |
register int n; |
37 |
greg |
2.3 |
/* load file into buffer */ |
38 |
greg |
2.7 |
if (fname == NULL) |
39 |
|
|
return(-1); /* no filename */ |
40 |
greg |
2.1 |
if ((fd = open(fname, 0)) < 0) |
41 |
greg |
2.3 |
return(-1); /* open error */ |
42 |
greg |
2.2 |
n = read(fd, buf, MAXFLEN); |
43 |
greg |
2.1 |
close(fd); |
44 |
greg |
2.3 |
if (n < 0) /* read error */ |
45 |
|
|
return(-1); |
46 |
greg |
2.2 |
if (n == MAXFLEN) /* file too big, take what we can */ |
47 |
greg |
2.1 |
while (!isspace(buf[--n])) |
48 |
greg |
2.3 |
if (n <= 0) /* one long word! */ |
49 |
|
|
return(-1); |
50 |
|
|
buf[n] = '\0'; /* terminate */ |
51 |
|
|
return(wordstring(words, buf)); /* wordstring does the rest */ |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
int |
56 |
|
|
wordstring(avl, str) /* allocate and load argument list */ |
57 |
|
|
char **avl; |
58 |
|
|
char *str; |
59 |
|
|
{ |
60 |
|
|
register char *cp, **ap; |
61 |
|
|
|
62 |
|
|
if (str == NULL) |
63 |
|
|
return(-1); |
64 |
|
|
cp = bmalloc(strlen(str)+1); |
65 |
|
|
if (cp == NULL) /* ENOMEM */ |
66 |
|
|
return(-1); |
67 |
|
|
strcpy(cp, str); |
68 |
greg |
2.4 |
ap = avl; /* parse into words */ |
69 |
|
|
for ( ; ; ) { |
70 |
|
|
while (isspace(*cp)) /* nullify spaces */ |
71 |
|
|
*cp++ = '\0'; |
72 |
|
|
if (!*cp) /* all done? */ |
73 |
|
|
break; |
74 |
|
|
*ap++ = cp; /* add argument to list */ |
75 |
greg |
2.6 |
while (*++cp && !isspace(*cp)) |
76 |
|
|
; |
77 |
greg |
2.1 |
} |
78 |
greg |
2.3 |
*ap = NULL; |
79 |
|
|
return(ap - avl); |
80 |
greg |
2.1 |
} |