source: abuse/trunk/src/imlib/wildargv.cpp @ 39

Last change on this file since 39 was 2, checked in by Sam Hocevar, 18 years ago
  • imported original 0.7.0 tarball
File size: 4.2 KB
Line 
1/*
2
3 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4
5 *%       Copyright (C) 1989, by WATCOM Systems Inc. All rights     %
6
7 *%       reserved. No part of this software may be reproduced      %
8
9 *%       in any form or by any means - graphic, electronic or      %
10
11 *%       mechanical, including photocopying, recording, taping     %
12
13 *%       or information storage and retrieval systems - except     %
14
15 *%       with the written permission of WATCOM Systems Inc.        %
16
17 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18
19  WILDARGV - split DOS command line into individual arguments expanding
20
21             those that contain ? or *.
22
23  This module is a substitute for the "initargv" module contained in the
24
25  library.
26
27
28
29  Modified:     By:             Reason:
30
31  ---------     ---             -------
32
33  23-aug-89     John Dahms      was ignoring files with Archive or
34
35                                read only attributes turned on. (Bug fix)
36
37  15-sep-91     F.W.Crigger     Use _LpCmdLine, _LpPgmName, _argc, _argv,
38
39                                ___Argc, ___Argv
40
41  02-nov-93     A.F.Scian       updated so that source will compile as C++
42
43*/
44
45#include <stdio.h>
46
47#include <stdlib.h>
48
49#include <string.h>
50
51#include <io.h>
52
53#include <direct.h>
54
55#include <malloc.h>
56
57
58
59#ifdef __cplusplus
60
61extern "C" {
62
63#endif
64
65
66
67extern  void    _Not_Enough_Memory();
68
69extern  char    *_LpCmdLine;
70
71extern  char    *_LpPgmName;
72
73extern  int     _argc;                  /* argument count  */
74
75extern  char  **_argv;                  /* argument vector */
76
77extern  int     ___Argc;                /* argument count */
78
79extern  char  **___Argv;                /* argument vector */
80
81
82
83#ifdef __cplusplus
84
85};
86
87#endif
88
89
90
91static void *_allocate( unsigned amount )
92
93{
94
95    void *p;
96
97
98
99#if defined(__386__)
100
101    p = malloc( amount );
102
103#else
104
105    p = _nmalloc( amount );
106
107#if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
108
109    if( (void near *) p == NULL )  p = malloc( amount );
110
111#endif
112
113#endif
114
115    if( p == NULL )  _Not_Enough_Memory();
116
117    return( p );
118
119}
120
121
122
123static int _make_argv( char *p, char ***argv )
124
125{
126
127    int                 argc;
128
129    char                *start;
130
131    char                *new_arg;
132
133    char                wildcard;
134
135    char                lastchar;
136
137    DIR *               dir;
138
139    struct dirent       *dirent;
140
141    char                drive[_MAX_DRIVE];
142
143    char                directory[_MAX_DIR];
144
145    char                name[_MAX_FNAME];
146
147    char                extin[_MAX_EXT];
148
149    char                pathin[_MAX_PATH];
150
151
152
153    argc = 1;
154
155    for(;;) {
156
157        while( *p == ' ' ) ++p; /* skip over blanks */
158
159        if( *p == '\0' ) break;
160
161        /* we are at the start of a parm */
162
163        wildcard = 0;
164
165        if( *p == '\"' ) {
166
167            p++;
168
169            new_arg = start = p;
170
171            for(;;) {
172
173                /* end of parm: NULLCHAR or quote */
174
175                if( *p == '\"' ) break;
176
177                if( *p == '\0' ) break;
178
179                if( *p == '\\' ) {
180
181                    if( p[1] == '\"'  ||  p[1] == '\\' )  ++p;
182
183                }
184
185                *new_arg++ = *p++;
186
187            }
188
189        } else {
190
191            new_arg = start = p;
192
193            for(;;) {
194
195                /* end of parm: NULLCHAR or blank */
196
197                if( *p == '\0' ) break;
198
199                if( *p == ' ' ) break;
200
201                if(( *p == '\\' )&&( p[1] == '\"' )) {
202
203                    ++p;
204
205                } else if( *p == '?'  ||  *p == '*' ) {
206
207                    wildcard = 1;
208
209                }
210
211                *new_arg++ = *p++;
212
213            }
214
215        }
216
217        *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) );
218
219        if( *argv == NULL )  _Not_Enough_Memory();
220
221        (*argv)[ argc ] = start;
222
223        ++argc;
224
225        lastchar = *p;
226
227        *new_arg = '\0';
228
229        ++p;
230
231        if( wildcard ) {
232
233            /* expand file names */
234
235            dir = opendir( start );
236
237            if( dir != NULL ) {
238
239                --argc;
240
241                _splitpath( start, drive, directory, name, extin );
242
243                for(;;) {
244
245                    dirent = readdir( dir );
246
247                    if( dirent == NULL ) break;
248
249                    if( dirent->d_attr &
250
251                      (_A_HIDDEN+_A_SYSTEM+_A_VOLID+_A_SUBDIR) ) continue;
252
253                    _splitpath( dirent->d_name, NULL, NULL, name, extin );
254
255                    _makepath( pathin, drive, directory, name, extin );
256
257                    *argv = (char **) realloc( *argv, (argc+2) * sizeof( char * ) );
258
259                    if( *argv == NULL )  _Not_Enough_Memory();
260
261                    new_arg = (char *) _allocate( strlen( pathin ) + 1 );
262
263                    strcpy( new_arg, pathin );
264
265                    (*argv)[argc++] = new_arg;
266
267                }
268
269                closedir( dir );
270
271            }
272
273        }
274
275        if( lastchar == '\0' ) break;
276
277    }
278
279    return( argc );
280
281}
282
283
284
285#ifdef __cplusplus
286
287extern "C"
288
289#endif
290
291void __Init_Argv()
292
293    {
294
295        _argv = (char **) _allocate( 2 * sizeof( char * ) );
296
297        _argv[0] = _LpPgmName;  /* fill in program name */
298
299        _argc = _make_argv( _LpCmdLine, &_argv );
300
301        _argv[_argc] = NULL;
302
303        ___Argc = _argc;
304
305        ___Argv = _argv;
306
307    }
308
Note: See TracBrowser for help on using the repository browser.