| 1 | 
greg | 
2.1 | 
#ifndef lint | 
| 2 | 
schorsch | 
2.10 | 
static const char       RCSid[] = "$Id: wordfile.c,v 2.9 2003/02/25 02:47:22 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 | 
  | 
  | 
#ifdef _WIN32 | 
| 21 | 
  | 
  | 
 #include <io.h> | 
| 22 | 
  | 
  | 
#endif | 
| 23 | 
  | 
  | 
 | 
| 24 | 
  | 
  | 
#include "standard.h" | 
| 25 | 
greg | 
2.2 | 
 | 
| 26 | 
greg | 
2.1 | 
 | 
| 27 | 
greg | 
2.3 | 
#define MAXFLEN         8192            /* file must be smaller than this */ | 
| 28 | 
greg | 
2.1 | 
 | 
| 29 | 
  | 
  | 
extern char     *bmalloc(); | 
| 30 | 
  | 
  | 
 | 
| 31 | 
  | 
  | 
 | 
| 32 | 
  | 
  | 
int | 
| 33 | 
  | 
  | 
wordfile(words, fname)          /* get words from fname, put in words */ | 
| 34 | 
  | 
  | 
char    **words; | 
| 35 | 
  | 
  | 
char    *fname; | 
| 36 | 
  | 
  | 
{ | 
| 37 | 
  | 
  | 
        int     fd; | 
| 38 | 
greg | 
2.3 | 
        char    buf[MAXFLEN]; | 
| 39 | 
greg | 
2.1 | 
        register int    n; | 
| 40 | 
greg | 
2.3 | 
                                        /* load file into buffer */ | 
| 41 | 
greg | 
2.7 | 
        if (fname == NULL) | 
| 42 | 
  | 
  | 
                return(-1);                     /* no filename */ | 
| 43 | 
greg | 
2.1 | 
        if ((fd = open(fname, 0)) < 0) | 
| 44 | 
greg | 
2.3 | 
                return(-1);                     /* open error */ | 
| 45 | 
greg | 
2.2 | 
        n = read(fd, buf, MAXFLEN); | 
| 46 | 
greg | 
2.1 | 
        close(fd); | 
| 47 | 
greg | 
2.3 | 
        if (n < 0)                              /* read error */ | 
| 48 | 
  | 
  | 
                return(-1); | 
| 49 | 
greg | 
2.2 | 
        if (n == MAXFLEN)               /* file too big, take what we can */ | 
| 50 | 
greg | 
2.1 | 
                while (!isspace(buf[--n])) | 
| 51 | 
greg | 
2.3 | 
                        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 | 
greg | 
2.4 | 
        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 | 
greg | 
2.6 | 
                while (*++cp && !isspace(*cp)) | 
| 79 | 
  | 
  | 
                        ; | 
| 80 | 
greg | 
2.1 | 
        } | 
| 81 | 
greg | 
2.3 | 
        *ap = NULL; | 
| 82 | 
  | 
  | 
        return(ap - avl); | 
| 83 | 
greg | 
2.1 | 
} |