ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/wordfile.c
Revision: 2.7
Committed: Fri Jun 18 09:00:22 1993 UTC (30 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.6: +2 -0 lines
Log Message:
fixed potential bug when wordfile is passed NULL as file name

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1992 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Load whitespace separated words from a file into an array.
9     * Assume the passed pointer array is big enough to hold them all.
10     */
11    
12 greg 2.2 #include <ctype.h>
13    
14 greg 2.1 #define NULL 0
15    
16 greg 2.3 #define MAXFLEN 8192 /* file must be smaller than this */
17 greg 2.1
18     extern char *bmalloc();
19    
20    
21     int
22     wordfile(words, fname) /* get words from fname, put in words */
23     char **words;
24     char *fname;
25     {
26     int fd;
27 greg 2.3 char buf[MAXFLEN];
28 greg 2.1 register int n;
29 greg 2.3 /* load file into buffer */
30 greg 2.7 if (fname == NULL)
31     return(-1); /* no filename */
32 greg 2.1 if ((fd = open(fname, 0)) < 0)
33 greg 2.3 return(-1); /* open error */
34 greg 2.2 n = read(fd, buf, MAXFLEN);
35 greg 2.1 close(fd);
36 greg 2.3 if (n < 0) /* read error */
37     return(-1);
38 greg 2.2 if (n == MAXFLEN) /* file too big, take what we can */
39 greg 2.1 while (!isspace(buf[--n]))
40 greg 2.3 if (n <= 0) /* one long word! */
41     return(-1);
42     buf[n] = '\0'; /* terminate */
43     return(wordstring(words, buf)); /* wordstring does the rest */
44     }
45    
46    
47     int
48     wordstring(avl, str) /* allocate and load argument list */
49     char **avl;
50     char *str;
51     {
52     register char *cp, **ap;
53    
54     if (str == NULL)
55     return(-1);
56     cp = bmalloc(strlen(str)+1);
57     if (cp == NULL) /* ENOMEM */
58     return(-1);
59     strcpy(cp, str);
60 greg 2.4 ap = avl; /* parse into words */
61     for ( ; ; ) {
62     while (isspace(*cp)) /* nullify spaces */
63     *cp++ = '\0';
64     if (!*cp) /* all done? */
65     break;
66     *ap++ = cp; /* add argument to list */
67 greg 2.6 while (*++cp && !isspace(*cp))
68     ;
69 greg 2.1 }
70 greg 2.3 *ap = NULL;
71     return(ap - avl);
72 greg 2.1 }