1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.16 |
static const char RCSid[] = "$Id: wordfile.c,v 2.15 2005/06/10 16:42:11 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.16 |
#define MAXFLEN 204800 /* file must be smaller than this */ |
26 |
greg |
2.14 |
#endif |
27 |
greg |
2.1 |
|
28 |
|
|
|
29 |
|
|
int |
30 |
greg |
2.16 |
wordfile( /* get words from fname, put in words */ |
31 |
|
|
char **words, |
32 |
|
|
int nargs, |
33 |
|
|
char *fname |
34 |
|
|
) |
35 |
greg |
2.1 |
{ |
36 |
|
|
int fd; |
37 |
greg |
2.3 |
char buf[MAXFLEN]; |
38 |
greg |
2.16 |
int n; |
39 |
greg |
2.3 |
/* load file into buffer */ |
40 |
greg |
2.7 |
if (fname == NULL) |
41 |
|
|
return(-1); /* no filename */ |
42 |
greg |
2.1 |
if ((fd = open(fname, 0)) < 0) |
43 |
greg |
2.3 |
return(-1); /* open error */ |
44 |
greg |
2.2 |
n = read(fd, buf, MAXFLEN); |
45 |
greg |
2.1 |
close(fd); |
46 |
greg |
2.3 |
if (n < 0) /* read error */ |
47 |
|
|
return(-1); |
48 |
greg |
2.2 |
if (n == MAXFLEN) /* file too big, take what we can */ |
49 |
greg |
2.1 |
while (!isspace(buf[--n])) |
50 |
greg |
2.3 |
if (n <= 0) /* one long word! */ |
51 |
|
|
return(-1); |
52 |
greg |
2.16 |
buf[n] = '\0'; /* terminate */ |
53 |
|
|
return(wordstring(words, nargs, buf)); /* wordstring does the rest */ |
54 |
greg |
2.3 |
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
int |
58 |
greg |
2.16 |
wordstring( /* allocate and load argument list */ |
59 |
|
|
char **avl, |
60 |
|
|
int nargs, |
61 |
|
|
char *str |
62 |
|
|
) |
63 |
greg |
2.3 |
{ |
64 |
greg |
2.16 |
char *cp, **ap; |
65 |
greg |
2.3 |
|
66 |
|
|
if (str == NULL) |
67 |
|
|
return(-1); |
68 |
|
|
cp = bmalloc(strlen(str)+1); |
69 |
|
|
if (cp == NULL) /* ENOMEM */ |
70 |
|
|
return(-1); |
71 |
|
|
strcpy(cp, str); |
72 |
greg |
2.16 |
/* parse into words */ |
73 |
|
|
for (ap = avl; ap-avl < nargs-1; ap++) { |
74 |
greg |
2.4 |
while (isspace(*cp)) /* nullify spaces */ |
75 |
|
|
*cp++ = '\0'; |
76 |
|
|
if (!*cp) /* all done? */ |
77 |
|
|
break; |
78 |
greg |
2.16 |
*ap = cp; /* add argument to list */ |
79 |
greg |
2.6 |
while (*++cp && !isspace(*cp)) |
80 |
|
|
; |
81 |
greg |
2.1 |
} |
82 |
greg |
2.3 |
*ap = NULL; |
83 |
|
|
return(ap - avl); |
84 |
greg |
2.1 |
} |