-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceargv.c
123 lines (107 loc) · 2.34 KB
/
ceargv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ceargv.c
//
// Time-stamp: <03/02/01 10:40:18 keuchel@lightning>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <windows.h>
#include "glob.h"
#include "errno.h"
#define GLOBFLAGS (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE | GLOB_NOMAGIC)
// GLOB_BRACE seems to have a bug...
// GLOB_TILDE also
static int
errfunc(const char *name, int err)
{
fprintf(stderr, "glob:cannot access %s - %s\n", name, xcestrerror(err));
return -1;
}
// argv size;
static int _size;
static void
argv_append(char *s)
{
if(__argc >= _size - 1)
{
// expand argv
_size *= 2;
__argv = realloc(__argv, _size * sizeof(char *));
}
// we do not free the glob vector or command-line, so we do not dup
__argv[__argc++] = s;
__argv[__argc] = NULL;
}
// Simple Visual C _setargv replacement, does not handle nested
// quotes.. The original matches short names! So *.cpp matches #xxx.cpp#
// (which is XXXX~1.CPP as short name). This is very bad...
void
_setargv()
{
int i, res;
char *p;
char *s;
glob_t gbuf;
gbuf.gl_pathc = 0;
gbuf.gl_offs = 0;
gbuf.gl_pathv = NULL;
_size = 10;
__argc = 0;
__argv = malloc(_size * sizeof(char *));
p = malloc(MAX_PATH);
XCEGetModuleFileNameA(NULL, p, MAX_PATH);
argv_append(p);
// argv[0] is part of commandline
s = p = (char *) XCEGetCommandLineA();
// printf("Program is %s Commandline is %s\n", __argv[0], s);
for(;;)
{
if(*p == 0)
{
if(s != p)
{
glob(s, GLOBFLAGS, errfunc, &gbuf);
for(i = 0; i < gbuf.gl_pathc; i++)
argv_append(gbuf.gl_pathv[i]);
}
break;
}
else if(*p == '"')
{
argv_append(++p);
while(*p && *p != '"')
p++;
*p++ = 0;
s = p;
}
else if(*p == '\'')
{
argv_append(++p);
while(*p && *p != '\'')
p++;
*p++ = 0;
s = p;
}
else if(*p == ' ' || *p == '\t')
{
if(s != p)
{
*p++ = 0;
glob(s, GLOBFLAGS, errfunc, &gbuf);
for(i = 0; i < gbuf.gl_pathc; i++)
argv_append(gbuf.gl_pathv[i]);
}
while(*p && (*p == '\t' || *p == ' '))
p++;
s = p;
}
else
{
p++;
}
}
// convert program name to unix style name
for(p = __argv[0]; *p; p++)
if(*p == '\\')
*p = '/';
}