2 Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
4 See the accompanying file LICENSE, version 2000-Apr-09 or later
5 (the contents of which are also included in unzip.h) for terms of use.
6 If, for some reason, all these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
9 /*----------------------------------------------------------------*
10 | envargs - add default options from environment to command line
11 |----------------------------------------------------------------
12 | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991.
13 | This program is in the public domain.
14 |----------------------------------------------------------------
15 | Minor program notes:
16 | 1. Yes, the indirection is a tad complex
17 | 2. Parentheses were added where not needed in some cases
18 | to make the action of the code less obscure.
19 |----------------------------------------------------------------
20 | UnZip notes: 24 May 92 ("v1.4"):
21 | 1. #include "unzip.h" for prototypes (24 May 92)
22 | 2. changed ch to type char (24 May 92)
23 | 3. added an ifdef to avoid Borland warnings (24 May 92)
24 | 4. included Rich Wales' mksargs() routine (for MS-DOS, maybe
25 | OS/2? NT?) (4 Dec 93)
26 | 5. added alternate-variable string envstr2 (21 Apr 94)
27 | 6. added support for quoted arguments (6 Jul 96)
28 *----------------------------------------------------------------*/
31 #define __ENVARGS_C /* identifies this source module */
32 #define UNZIP_INTERNAL
35 #ifdef __EMX__ /* emx isspace() returns TRUE on extended ASCII !! */
36 # define ISspace(c) ((c) & 0x80 ? 0 : isspace((unsigned)c))
38 # define ISspace(c) isspace((unsigned)c)
41 static int count_args
OF((ZCONST
char *));
44 /* envargs() returns PK-style error code */
46 int envargs(Pargc
, Pargv
, envstr
, envstr2
)
49 ZCONST
char *envstr
, *envstr2
;
54 char *envptr
; /* value returned by getenv */
55 char *bufptr
; /* copy of env info */
56 int argc
= 0; /* internal arg count */
57 register int ch
; /* spare temp value */
58 char **argv
; /* internal arg vector */
59 char **argvect
; /* copy of vector address */
61 /* see if anything in the environment */
62 if ((envptr
= getenv(envstr
)) != (char *)NULL
) /* usual var */
63 while (ISspace(*envptr
)) /* must discard leading spaces */
65 if (envptr
== (char *)NULL
|| *envptr
== '\0')
66 if ((envptr
= getenv(envstr2
)) != (char *)NULL
) /* alternate var */
67 while (ISspace(*envptr
))
69 if (envptr
== (char *)NULL
|| *envptr
== '\0')
72 bufptr
= malloc(1 + strlen(envptr
));
73 if (bufptr
== (char *)NULL
)
75 #if (defined(WIN32) || defined(WINDLL))
78 /* SPC: don't know codepage of 'real' WinNT console */
79 strcpy(bufptr
, envptr
);
81 /* Win95 environment is DOS and uses OEM character coding */
82 OEM_TO_INTERN(envptr
, bufptr
);
85 /* DOS environment uses OEM codepage */
86 OEM_TO_INTERN(envptr
, bufptr
);
88 #else /* !(WIN32 || WINDLL) */
89 strcpy(bufptr
, envptr
);
90 #endif /* ?(WIN32 || WINDLL) */
92 /* count the args so we can allocate room for them */
93 argc
= count_args(bufptr
);
94 /* allocate a vector large enough for all args */
95 argv
= (char **)malloc((argc
+ *Pargc
+ 1) * sizeof(char *));
96 if (argv
== (char **)NULL
) {
102 /* copy the program name first, that's always true */
103 *(argv
++) = *((*Pargv
)++);
105 /* copy the environment args next, may be changed */
107 #if defined(AMIGA) || defined(UNIX)
108 if (*bufptr
== '"') {
109 char *argstart
= ++bufptr
;
111 *(argv
++) = argstart
;
112 for (ch
= *bufptr
; ch
!= '\0' && ch
!= '\"';
113 ch
= *PREINCSTR(bufptr
))
114 if (ch
== '\\' && bufptr
[1] != '\0')
115 ++bufptr
; /* advance to char after backslash */
117 *(bufptr
++) = '\0'; /* overwrite trailing " */
119 /* remove escape characters */
120 while ((argstart
= MBSCHR(argstart
, '\\')) != (char *)NULL
) {
121 strcpy(argstart
, argstart
+ 1);
127 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
133 #ifdef DOS_FLX_NLM_OS2_W32
134 /* we do not support backslash-quoting of quotes in quoted
135 * strings under DOS_FLX_NLM_OS2_W32, because backslashes are
136 * directory separators and double quotes are illegal in filenames */
137 if (*bufptr
== '"') {
138 *(argv
++) = ++bufptr
;
139 while ((ch
= *bufptr
) != '\0' && ch
!= '\"')
145 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
152 while ((ch
= *bufptr
) != '\0' && !ISspace(ch
))
156 #endif /* ?DOS_FLX_NLM_OS2_W32 */
157 #endif /* ?(AMIGA || UNIX) */
158 while ((ch
= *bufptr
) != '\0' && ISspace(ch
))
162 /* now save old argc and copy in the old args */
165 *(argv
++) = *((*Pargv
)++);
167 /* finally, add a NULL after the last arg, like Unix */
168 *argv
= (char *)NULL
;
170 /* save the values and return, indicating succes */
179 static int count_args(s
)
186 /* count and skip args */
188 #if defined(AMIGA) || defined(UNIX)
190 for (ch
= *PREINCSTR(s
); ch
!= '\0' && ch
!= '\"';
192 if (ch
== '\\' && s
[1] != '\0')
195 ++s
; /* trailing quote */
198 #ifdef DOS_FLX_NLM_OS2_W32
200 ++s
; /* leading quote */
201 while ((ch
= *s
) != '\0' && ch
!= '\"')
204 ++s
; /* trailing quote */
206 #endif /* DOS_FLX_NLM_OS2_W32 */
207 #endif /* ?(AMIGA || UNIX) */
208 while ((ch
= *s
) != '\0' && !ISspace(ch
)) /* note else-clauses above */
210 while ((ch
= *s
) != '\0' && ISspace(ch
))
227 printf("Orig argv: %p\n", argv
);
228 dump_args(argc
, argv
);
229 if ((err
= envargs(&argc
, &argv
, "ENVTEST")) != PK_OK
) {
230 perror("envargs: cannot get memory for arguments");
233 printf(" New argv: %p\n", argv
);
234 dump_args(argc
, argv
);
239 void dump_args(argc
, argv
)
245 printf("\nDump %d args:\n", argc
);
246 for (i
= 0; i
< argc
; ++i
)
247 printf("%3d %s\n", i
, argv
[i
]);
254 #ifdef MSDOS /* DOS_OS2? DOS_OS2_W32? */
257 * void mksargs(int *argcp, char ***argvp)
259 * Substitutes the extended command line argument list produced by
260 * the MKS Korn Shell in place of the command line info from DOS.
262 * The MKS shell gets around DOS's 128-byte limit on the length of
263 * a command line by passing the "real" command line in the envi-
264 * ronment. The "real" arguments are flagged by prepending a tilde
267 * This "mksargs" routine creates a new argument list by scanning
268 * the environment from the beginning, looking for strings begin-
269 * ning with a tilde character. The new list replaces the original
270 * "argv" (pointed to by "argvp"), and the number of arguments
271 * in the new list replaces the original "argc" (pointed to by
276 void mksargs(argcp
, argvp
)
280 #ifndef MSC /* declared differently in MSC 7.0 headers, at least */
282 extern char **environ
; /* environment */
285 char **envp
; /* pointer into environment */
286 char **newargv
; /* new argument list */
287 char **argp
; /* pointer into new arg list */
288 int newargc
; /* new argument count */
291 if (environ
== NULL
|| argcp
== NULL
|| argvp
== NULL
|| *argvp
== NULL
)
294 /* find out how many environment arguments there are */
295 for (envp
= environ
, newargc
= 0;
296 *envp
!= NULL
&& (*envp
)[0] == '~';
300 return; /* no environment arguments */
302 /* set up new argument list */
303 newargv
= (char **) malloc(sizeof(char **) * (newargc
+1));
305 return; /* malloc failed */
307 for (argp
= newargv
, envp
= environ
; *envp
!= NULL
&& (*envp
)[0] == '~';
308 *argp
++ = &(*envp
++)[1])
310 *argp
= NULL
; /* null-terminate the list */
312 /* substitute new argument list in place of old one */