| 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 |
+ |
#include "copyright.h" |
| 12 |
+ |
|
| 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 |
< |
#define NULL 0 |
| 20 |
> |
#include "standard.h" |
| 21 |
> |
#include "platform.h" |
| 22 |
|
|
| 16 |
– |
#define MAXFLEN 10240 /* file must be smaller than this */ |
| 23 |
|
|
| 24 |
+ |
#define MAXFLEN 8192 /* file must be smaller than this */ |
| 25 |
+ |
|
| 26 |
|
extern char *bmalloc(); |
| 27 |
|
|
| 28 |
|
|
| 31 |
|
char **words; |
| 32 |
|
char *fname; |
| 33 |
|
{ |
| 26 |
– |
char **wp = words; |
| 34 |
|
int fd; |
| 35 |
< |
char *buf; |
| 35 |
> |
char buf[MAXFLEN]; |
| 36 |
|
register int n; |
| 37 |
< |
register char *cp; |
| 38 |
< |
/* allocate memory */ |
| 39 |
< |
if ((cp = buf = bmalloc(MAXFLEN)) == NULL) |
| 33 |
< |
return(-1); |
| 37 |
> |
/* load file into buffer */ |
| 38 |
> |
if (fname == NULL) |
| 39 |
> |
return(-1); /* no filename */ |
| 40 |
|
if ((fd = open(fname, 0)) < 0) |
| 41 |
< |
goto err; |
| 41 |
> |
return(-1); /* open error */ |
| 42 |
|
n = read(fd, buf, MAXFLEN); |
| 43 |
|
close(fd); |
| 44 |
< |
if (n < 0) |
| 45 |
< |
goto err; |
| 44 |
> |
if (n < 0) /* read error */ |
| 45 |
> |
return(-1); |
| 46 |
|
if (n == MAXFLEN) /* file too big, take what we can */ |
| 47 |
|
while (!isspace(buf[--n])) |
| 48 |
< |
if (n <= 0) |
| 49 |
< |
goto err; |
| 50 |
< |
bfree(buf+n+1, MAXFLEN-n-1); /* return unneeded memory */ |
| 51 |
< |
while (n > 0) { /* break buffer into words */ |
| 52 |
< |
while (isspace(*cp)) { |
| 53 |
< |
cp++; |
| 54 |
< |
if (--n <= 0) |
| 55 |
< |
return(wp - words); |
| 56 |
< |
} |
| 57 |
< |
*wp++ = cp; |
| 58 |
< |
while (!isspace(*cp)) { |
| 59 |
< |
cp++; |
| 60 |
< |
if (--n <= 0) |
| 61 |
< |
break; |
| 62 |
< |
} |
| 63 |
< |
*cp++ = '\0'; n--; |
| 48 |
> |
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 |
> |
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 |
> |
while (*++cp && !isspace(*cp)) |
| 76 |
> |
; |
| 77 |
|
} |
| 78 |
< |
*wp = NULL; /* null terminator */ |
| 79 |
< |
return(wp - words); |
| 61 |
< |
err: |
| 62 |
< |
bfree(buf, MAXFLEN); |
| 63 |
< |
return(-1); |
| 78 |
> |
*ap = NULL; |
| 79 |
> |
return(ap - avl); |
| 80 |
|
} |