ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/expandarg.c
Revision: 2.10
Committed: Mon Mar 21 19:06:08 2016 UTC (8 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad5R1
Changes since 2.9: +8 -7 lines
Log Message:
Improved error-checking for too many words in file or string

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: expandarg.c,v 2.9 2008/05/17 02:49:41 greg Exp $";
3 #endif
4 /*
5 * Get additional command arguments from file or environment.
6 *
7 * External symbols declared in rtio.h
8 */
9
10 #include "copyright.h"
11
12 #include <errno.h>
13 #include <string.h>
14
15 #include "rtio.h"
16 #include "rtmisc.h"
17
18
19 #define MAXARGEXP 4096 /* maximum argument expansion */
20
21 /* set the following to suit, -1 to disable */
22 int envexpchr = '$'; /* environment expansion character */
23 int filexpchr = '@'; /* file expansion character */
24
25
26 int
27 expandarg( /* expand list at argument n */
28 int *acp,
29 char ***avp,
30 int n
31 )
32 {
33 int ace;
34 char *ave[MAXARGEXP];
35 char **newav;
36 /* check argument */
37 if (n >= *acp)
38 return(0);
39 errno = 0;
40 if ((*avp)[n][0] == filexpchr) { /* file name */
41 ace = wordfile(ave, MAXARGEXP, (*avp)[n]+1);
42 if (ace < 0)
43 return(-1); /* no such file */
44 } else if ((*avp)[n][0] == envexpchr) { /* env. variable */
45 ace = wordstring(ave, MAXARGEXP, getenv((*avp)[n]+1));
46 if (ace < 0)
47 return(-1); /* no such variable */
48 } else /* regular argument */
49 return(0);
50 /* allocate new pointer list */
51 newav = (char **)bmalloc((*acp+ace)*sizeof(char *));
52 if (newav == NULL)
53 return(-1);
54 /* copy preceeding arguments */
55 memcpy((void *)newav, (void *)*avp, n*sizeof(char *));
56 /* copy expanded argument */
57 memcpy((void *)(newav+n), (void *)ave, ace*sizeof(char *));
58 /* copy trailing arguments + NULL */
59 memcpy((void *)(newav+n+ace), (void *)(*avp+n+1), (*acp-n)*sizeof(char *));
60 /* free old list */
61 bfree((char *)*avp, (*acp+1)*sizeof(char *));
62 /* assign new list */
63 *acp += ace-1;
64 *avp = newav;
65 return(1); /* return success */
66 }