1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.17 |
static const char RCSid[] = "$Id: wordfile.c,v 2.16 2016/03/21 19:06:08 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.17 |
#define MAXWLEN 4096 /* words must be shorter than this */ |
25 |
greg |
2.1 |
|
26 |
|
|
|
27 |
|
|
int |
28 |
greg |
2.16 |
wordfile( /* get words from fname, put in words */ |
29 |
|
|
char **words, |
30 |
|
|
int nargs, |
31 |
|
|
char *fname |
32 |
|
|
) |
33 |
greg |
2.1 |
{ |
34 |
greg |
2.17 |
int wrdcnt = 0; |
35 |
|
|
int n = 0; |
36 |
greg |
2.1 |
int fd; |
37 |
greg |
2.17 |
char buf[MAXWLEN]; |
38 |
greg |
2.3 |
/* load file into buffer */ |
39 |
greg |
2.17 |
if (fname == NULL || !*fname) |
40 |
greg |
2.7 |
return(-1); /* no filename */ |
41 |
greg |
2.17 |
if (nargs <= 1) |
42 |
|
|
return(-1); |
43 |
greg |
2.1 |
if ((fd = open(fname, 0)) < 0) |
44 |
greg |
2.3 |
return(-1); /* open error */ |
45 |
greg |
2.17 |
words[0] = NULL; |
46 |
|
|
while (nargs > 1 && (n += read(fd, buf+n, MAXWLEN-n)) > 0) { |
47 |
|
|
int crem = 0; |
48 |
|
|
if (n >= MAXWLEN) /* still something left? */ |
49 |
|
|
while (!isspace(buf[--n])) { |
50 |
|
|
if (n <= 0) /* one long word! */ |
51 |
|
|
goto done; |
52 |
|
|
++crem; |
53 |
|
|
} |
54 |
|
|
buf[n] = '\0'; /* terminate & parse */ |
55 |
|
|
n = wordstring(words, nargs, buf); |
56 |
|
|
if (n < 0) { |
57 |
|
|
wrdcnt = -1; /* memory error */ |
58 |
|
|
break; |
59 |
|
|
} |
60 |
|
|
words += n; |
61 |
|
|
nargs -= n; |
62 |
|
|
wrdcnt += n; |
63 |
|
|
if ((n = crem) > 0) /* move remainder */ |
64 |
|
|
strncpy(buf, buf+MAXWLEN-crem, crem); |
65 |
|
|
} |
66 |
|
|
done: |
67 |
greg |
2.1 |
close(fd); |
68 |
greg |
2.17 |
return(wrdcnt); |
69 |
greg |
2.3 |
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
int |
73 |
greg |
2.16 |
wordstring( /* allocate and load argument list */ |
74 |
|
|
char **avl, |
75 |
|
|
int nargs, |
76 |
|
|
char *str |
77 |
|
|
) |
78 |
greg |
2.3 |
{ |
79 |
greg |
2.16 |
char *cp, **ap; |
80 |
greg |
2.3 |
|
81 |
|
|
if (str == NULL) |
82 |
|
|
return(-1); |
83 |
|
|
cp = bmalloc(strlen(str)+1); |
84 |
|
|
if (cp == NULL) /* ENOMEM */ |
85 |
|
|
return(-1); |
86 |
|
|
strcpy(cp, str); |
87 |
greg |
2.16 |
/* parse into words */ |
88 |
|
|
for (ap = avl; ap-avl < nargs-1; ap++) { |
89 |
greg |
2.4 |
while (isspace(*cp)) /* nullify spaces */ |
90 |
|
|
*cp++ = '\0'; |
91 |
|
|
if (!*cp) /* all done? */ |
92 |
|
|
break; |
93 |
greg |
2.16 |
*ap = cp; /* add argument to list */ |
94 |
greg |
2.6 |
while (*++cp && !isspace(*cp)) |
95 |
|
|
; |
96 |
greg |
2.1 |
} |
97 |
greg |
2.17 |
*cp = '\0'; /* terminates overflow */ |
98 |
greg |
2.3 |
*ap = NULL; |
99 |
|
|
return(ap - avl); |
100 |
greg |
2.1 |
} |