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.16 by greg, Mon Mar 21 19:06:08 2016 UTC

# Line 1 | Line 1
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 "platform.h"
21 > #include "standard.h"
22  
16 #define MAXFLEN         10240           /* file must be smaller than this */
23  
24 < extern char     *bmalloc();
24 > #ifndef MAXFLEN
25 > #define MAXFLEN         204800  /* file must be smaller than this */
26 > #endif
27  
28  
29   int
30 < wordfile(words, fname)          /* get words from fname, put in words */
31 < char    **words;
32 < char    *fname;
30 > wordfile(                       /* get words from fname, put in words */
31 >        char    **words,
32 >        int     nargs,
33 >        char    *fname
34 > )
35   {
26        char    **wp = words;
36          int     fd;
37 <        char    *buf;
38 <        register int    n;
39 <        register char   *cp;
40 <                                        /* allocate memory */
41 <        if ((cp = buf = bmalloc(MAXFLEN)) == NULL)
33 <                return(-1);
37 >        char    buf[MAXFLEN];
38 >        int     n;
39 >                                        /* load file into buffer */
40 >        if (fname == NULL)
41 >                return(-1);                     /* no filename */
42          if ((fd = open(fname, 0)) < 0)
43 <                goto err;
43 >                return(-1);                     /* open error */
44          n = read(fd, buf, MAXFLEN);
45          close(fd);
46 <        if (n < 0)
47 <                goto err;
46 >        if (n < 0)                              /* read error */
47 >                return(-1);
48          if (n == MAXFLEN)               /* file too big, take what we can */
49                  while (!isspace(buf[--n]))
50 <                        if (n <= 0)
51 <                                goto err;
52 <        bfree(buf+n+1, MAXFLEN-n-1);    /* return unneeded memory */
53 <        while (n > 0) {                 /* break buffer into words */
54 <                while (isspace(*cp)) {
55 <                        cp++;
56 <                        if (--n <= 0)
57 <                                return(wp - words);
58 <                }
59 <                *wp++ = cp;
60 <                while (!isspace(*cp)) {
61 <                        cp++;
62 <                        if (--n <= 0)
63 <                                break;
64 <                }
65 <                *cp++ = '\0'; n--;
50 >                        if (n <= 0)             /* one long word! */
51 >                                return(-1);
52 >        buf[n] = '\0';                          /* terminate */
53 >        return(wordstring(words, nargs, buf));  /* wordstring does the rest */
54 > }
55 >
56 >
57 > int
58 > wordstring(                             /* allocate and load argument list */
59 >        char    **avl,
60 >        int     nargs,
61 >        char    *str
62 > )
63 > {
64 >        char    *cp, **ap;
65 >        
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 >                                        /* parse into words */
73 >        for (ap = avl; ap-avl < nargs-1; ap++) {
74 >                while (isspace(*cp))    /* nullify spaces */
75 >                        *cp++ = '\0';
76 >                if (!*cp)               /* all done? */
77 >                        break;
78 >                *ap = cp;               /* add argument to list */
79 >                while (*++cp && !isspace(*cp))
80 >                        ;
81          }
82 <        *wp = NULL;                     /* null terminator */
83 <        return(wp - words);
61 < err:
62 <        bfree(buf, MAXFLEN);
63 <        return(-1);
82 >        *ap = NULL;
83 >        return(ap - avl);
84   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines