1 | /* |
---|
2 | *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
3 | *% Copyright (C) 1989, by WATCOM Systems Inc. All rights % |
---|
4 | *% reserved. No part of this software may be reproduced % |
---|
5 | *% in any form or by any means - graphic, electronic or % |
---|
6 | *% mechanical, including photocopying, recording, taping % |
---|
7 | *% or information storage and retrieval systems - except % |
---|
8 | *% with the written permission of WATCOM Systems Inc. % |
---|
9 | *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
10 | WILDARGV - split DOS command line into individual arguments expanding |
---|
11 | those that contain ? or *. |
---|
12 | This module is a substitute for the "initargv" module contained in the |
---|
13 | library. |
---|
14 | |
---|
15 | Modified: By: Reason: |
---|
16 | --------- --- ------- |
---|
17 | 23-aug-89 John Dahms was ignoring files with Archive or |
---|
18 | read only attributes turned on. (Bug fix) |
---|
19 | 15-sep-91 F.W.Crigger Use _LpCmdLine, _LpPgmName, _argc, _argv, |
---|
20 | ___Argc, ___Argv |
---|
21 | 02-nov-93 A.F.Scian updated so that source will compile as C++ |
---|
22 | */ |
---|
23 | #include <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | #include <io.h> |
---|
27 | #include <direct.h> |
---|
28 | #include <malloc.h> |
---|
29 | |
---|
30 | #ifdef __cplusplus |
---|
31 | extern "C" { |
---|
32 | #endif |
---|
33 | |
---|
34 | extern void _Not_Enough_Memory(); |
---|
35 | extern char *_LpCmdLine; |
---|
36 | extern char *_LpPgmName; |
---|
37 | extern int _argc; /* argument count */ |
---|
38 | extern char **_argv; /* argument vector */ |
---|
39 | extern int ___Argc; /* argument count */ |
---|
40 | extern char **___Argv; /* argument vector */ |
---|
41 | |
---|
42 | #ifdef __cplusplus |
---|
43 | }; |
---|
44 | #endif |
---|
45 | |
---|
46 | static void *_allocate( unsigned amount ) |
---|
47 | { |
---|
48 | void *p; |
---|
49 | |
---|
50 | #if defined(__386__) |
---|
51 | p = malloc( amount ); |
---|
52 | #else |
---|
53 | p = _nmalloc( amount ); |
---|
54 | #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__) |
---|
55 | if( (void near *) p == NULL ) p = malloc( amount ); |
---|
56 | #endif |
---|
57 | #endif |
---|
58 | if( p == NULL ) _Not_Enough_Memory(); |
---|
59 | return( p ); |
---|
60 | } |
---|
61 | |
---|
62 | static int _make_argv( char *p, char ***argv ) |
---|
63 | { |
---|
64 | int argc; |
---|
65 | char *start; |
---|
66 | char *new_arg; |
---|
67 | char wildcard; |
---|
68 | char lastchar; |
---|
69 | DIR * dir; |
---|
70 | struct dirent *dirent; |
---|
71 | char drive[_MAX_DRIVE]; |
---|
72 | char directory[_MAX_DIR]; |
---|
73 | char name[_MAX_FNAME]; |
---|
74 | char extin[_MAX_EXT]; |
---|
75 | char pathin[_MAX_PATH]; |
---|
76 | |
---|
77 | argc = 1; |
---|
78 | for(;;) { |
---|
79 | while( *p == ' ' ) ++p; /* skip over blanks */ |
---|
80 | if( *p == '\0' ) break; |
---|
81 | /* we are at the start of a parm */ |
---|
82 | wildcard = 0; |
---|
83 | if( *p == '\"' ) { |
---|
84 | p++; |
---|
85 | new_arg = start = p; |
---|
86 | for(;;) { |
---|
87 | /* end of parm: NULLCHAR or quote */ |
---|
88 | if( *p == '\"' ) break; |
---|
89 | if( *p == '\0' ) break; |
---|
90 | if( *p == '\\' ) { |
---|
91 | if( p[1] == '\"' || p[1] == '\\' ) ++p; |
---|
92 | } |
---|
93 | *new_arg++ = *p++; |
---|
94 | } |
---|
95 | } else { |
---|
96 | new_arg = start = p; |
---|
97 | for(;;) { |
---|
98 | /* end of parm: NULLCHAR or blank */ |
---|
99 | if( *p == '\0' ) break; |
---|
100 | if( *p == ' ' ) break; |
---|
101 | if(( *p == '\\' )&&( p[1] == '\"' )) { |
---|
102 | ++p; |
---|
103 | } else if( *p == '?' || *p == '*' ) { |
---|
104 | wildcard = 1; |
---|
105 | } |
---|
106 | *new_arg++ = *p++; |
---|
107 | } |
---|
108 | } |
---|
109 | *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) ); |
---|
110 | if( *argv == NULL ) _Not_Enough_Memory(); |
---|
111 | (*argv)[ argc ] = start; |
---|
112 | ++argc; |
---|
113 | lastchar = *p; |
---|
114 | *new_arg = '\0'; |
---|
115 | ++p; |
---|
116 | if( wildcard ) { |
---|
117 | /* expand file names */ |
---|
118 | dir = opendir( start ); |
---|
119 | if( dir != NULL ) { |
---|
120 | --argc; |
---|
121 | _splitpath( start, drive, directory, name, extin ); |
---|
122 | for(;;) { |
---|
123 | dirent = readdir( dir ); |
---|
124 | if( dirent == NULL ) break; |
---|
125 | if( dirent->d_attr & |
---|
126 | (_A_HIDDEN+_A_SYSTEM+_A_VOLID+_A_SUBDIR) ) continue; |
---|
127 | _splitpath( dirent->d_name, NULL, NULL, name, extin ); |
---|
128 | _makepath( pathin, drive, directory, name, extin ); |
---|
129 | *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) ); |
---|
130 | if( *argv == NULL ) _Not_Enough_Memory(); |
---|
131 | new_arg = (char *) _allocate( strlen( pathin ) + 1 ); |
---|
132 | strcpy( new_arg, pathin ); |
---|
133 | (*argv)[argc++] = new_arg; |
---|
134 | } |
---|
135 | closedir( dir ); |
---|
136 | } |
---|
137 | } |
---|
138 | if( lastchar == '\0' ) break; |
---|
139 | } |
---|
140 | return( argc ); |
---|
141 | } |
---|
142 | |
---|
143 | #ifdef __cplusplus |
---|
144 | extern "C" |
---|
145 | #endif |
---|
146 | void __Init_Argv() |
---|
147 | { |
---|
148 | _argv = (char **) _allocate( 2 * sizeof( char * ) ); |
---|
149 | _argv[0] = _LpPgmName; /* fill in program name */ |
---|
150 | _argc = _make_argv( _LpCmdLine, &_argv ); |
---|
151 | _argv[_argc] = NULL; |
---|
152 | ___Argc = _argc; |
---|
153 | ___Argv = _argv; |
---|
154 | } |
---|