|
|
1.1 root 1: /*****************************************************************
2: | envargs - add default options from environment to command line
3: |----------------------------------------------------------------
4: | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991.
5: | This program is in the public domain.
6: |----------------------------------------------------------------
7: | Minor program notes:
8: | 1. Yes, the indirection is a tad complex
9: | 2. Parenthesis were added where not needed in some cases
10: | to make the action of the code less obscure.
11: | 3. Set tabsize to four to make this pretty
12: |----------------------------------------------------------------
13: | UnZip notes 24 May 92 ("v1.4"):
14: | 1. #include "unzip.h" for prototypes
15: | 2. changed ch to type char
16: | 3. added an ifdef to avoid Borland warnings
17: *****************************************************************/
18:
19: #include "unzip.h"
20: static int count_args __((char *));
21: static void mem_err __((void));
22:
23: #if (defined(SCCS) && !defined(lint)) /* causes warnings: annoying */
24: static char *SCCSid = "@(#)envargs.c 1.3 23 Oct 1991";
25: #endif
26:
27: void
28: envargs(Pargc, Pargv, envstr)
29: int *Pargc;
30: char ***Pargv;
31: char *envstr;
32: {
33: char *getenv();
34: char *envptr; /* value returned by getenv */
35: char *bufptr; /* copy of env info */
36: int argc = 0; /* internal arg count */
37: char ch; /* spare temp value */
38: char **argv; /* internal arg vector */
39: char **argvect; /* copy of vector address */
40:
41: /* see if anything in the environment */
42: envptr = getenv(envstr);
43: if (envptr == (char *)NULL || *envptr == 0) return;
44:
45: /* count the args so we can allocate room for them */
46: argc = count_args(envptr);
47: bufptr = (char *)malloc(1+strlen(envptr));
48: if (bufptr == (char *)NULL) mem_err();
49: strcpy(bufptr, envptr);
50:
51: /* allocate a vector large enough for all args */
52: argv = (char **)malloc((argc+*Pargc+1)*sizeof(char *));
53: if (argv == (char **)NULL) mem_err();
54: argvect = argv;
55:
56: /* copy the program name first, that's always true */
57: *(argv++) = *((*Pargv)++);
58:
59: /* copy the environment args next, may be changed */
60: do {
61: *(argv++) = bufptr;
62: /* skip the arg and any trailing blanks */
63: while (((ch = *bufptr) != '\0') && ch != ' ') ++bufptr;
64: if (ch == ' ') *(bufptr++) = '\0';
65: while (((ch = *bufptr) != '\0') && ch == ' ') ++bufptr;
66: } while (ch);
67:
68: /* now save old argc and copy in the old args */
69: argc += *Pargc;
70: while (--(*Pargc)) *(argv++) = *((*Pargv)++);
71:
72: /* finally, add a NULL after the last arg, like UNIX */
73: *argv = (char *)NULL;
74:
75: /* save the values and return */
76: *Pargv = argvect;
77: *Pargc = argc;
78: }
79:
80: static int
81: count_args(s)
82: char *s;
83: {
84: int count = 0;
85: char ch;
86:
87: do {
88: /* count and skip args */
89: ++count;
90: while (((ch = *s) != '\0') && ch != ' ') ++s;
91: while (((ch = *s) != '\0') && ch == ' ') ++s;
92: } while (ch);
93:
94: return count;
95: }
96:
97: static void
98: mem_err()
99: {
100: perror("Can't get memory for arguments");
101: exit(2);
102: }
103:
104: #ifdef TEST
105: main(argc, argv)
106: int argc;
107: char **argv;
108: {
109: int i;
110:
111: printf("Orig argv: %p\n", argv);
112: dump_args(argc, argv);
113: envargs(&argc, &argv, "ENVTEST");
114: printf(" New argv: %p\n", argv);
115: dump_args(argc, argv);
116: }
117:
118: dump_args(argc, argv)
119: int argc;
120: char *argv[];
121: {
122: int i;
123:
124: printf("\nDump %d args:\n", argc);
125: for (i=0; i<argc; ++i) {
126: printf("%3d %s\n", i, argv[i]);
127: }
128: }
129: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.