ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/wordfile.c
(Generate patch)

Comparing ray/src/common/wordfile.c (file contents):
Revision 2.2 by greg, Fri Oct 16 13:02:46 1992 UTC vs.
Revision 2.7 by greg, Fri Jun 18 09:00:22 1993 UTC

# Line 13 | Line 13 | static char SCCSid[] = "$SunId$ LBL";
13  
14   #define NULL            0
15  
16 < #define MAXFLEN         10240           /* file must be smaller than this */
16 > #define MAXFLEN         8192            /* file must be smaller than this */
17  
18   extern char     *bmalloc();
19  
# Line 23 | Line 23 | wordfile(words, fname)         /* get words from fname, put i
23   char    **words;
24   char    *fname;
25   {
26        char    **wp = words;
26          int     fd;
27 <        char    *buf;
27 >        char    buf[MAXFLEN];
28          register int    n;
29 <        register char   *cp;
30 <                                        /* allocate memory */
31 <        if ((cp = buf = bmalloc(MAXFLEN)) == NULL)
33 <                return(-1);
29 >                                        /* load file into buffer */
30 >        if (fname == NULL)
31 >                return(-1);                     /* no filename */
32          if ((fd = open(fname, 0)) < 0)
33 <                goto err;
33 >                return(-1);                     /* open error */
34          n = read(fd, buf, MAXFLEN);
35          close(fd);
36 <        if (n < 0)
37 <                goto err;
36 >        if (n < 0)                              /* read error */
37 >                return(-1);
38          if (n == MAXFLEN)               /* file too big, take what we can */
39                  while (!isspace(buf[--n]))
40 <                        if (n <= 0)
41 <                                goto err;
42 <        bfree(buf+n+1, MAXFLEN-n-1);    /* return unneeded memory */
43 <        while (n > 0) {                 /* break buffer into words */
44 <                while (isspace(*cp)) {
45 <                        cp++;
46 <                        if (--n <= 0)
47 <                                return(wp - words);
48 <                }
49 <                *wp++ = cp;
50 <                while (!isspace(*cp)) {
51 <                        cp++;
52 <                        if (--n <= 0)
53 <                                break;
54 <                }
55 <                *cp++ = '\0'; n--;
40 >                        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 >        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 >                while (*++cp && !isspace(*cp))
68 >                        ;
69          }
70 <        *wp = NULL;                     /* null terminator */
71 <        return(wp - words);
61 < err:
62 <        bfree(buf, MAXFLEN);
63 <        return(-1);
70 >        *ap = NULL;
71 >        return(ap - avl);
72   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines