-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsymlink.c
210 lines (193 loc) · 5.05 KB
/
symlink.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#define SHELL_SPECIAL_CHARS "\"\\ \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
#define SHELL_SPACE_CHARS " \001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
static void *
xmalloc(size_t size)
{
void * ret = malloc(size);
if ( ret == NULL ){
fputs("No memory\n",stderr);
exit(1);
}
return ret;
}
static char *
xstrdup(char *p)
{
char * ret = _strdup(p);
if ( ret == NULL ){
fputs("No memory\n",stderr);
exit(1);
}
return ret;
}
static char **
prepare_spawn (char **argv)
{
size_t argc;
char **new_argv;
size_t i;
/* Count number of arguments. */
for (argc = 0; argv[argc] != NULL; argc++)
;
/* Allocate new argument vector. */
new_argv = xmalloc((argc + 1) * sizeof *new_argv );
/* Put quoted arguments into the new argument vector. */
for (i = 0; i < argc; i++)
{
const char *string = argv[i];
if (string[0] == '\0')
new_argv[i] = xstrdup ("\"\"");
else if (strpbrk (string, SHELL_SPECIAL_CHARS) != NULL)
{
int quote_around = (strpbrk (string, SHELL_SPACE_CHARS) != NULL);
size_t length;
unsigned int backslashes;
const char *s;
char *quoted_string;
char *p;
length = 0;
backslashes = 0;
if (quote_around)
length++;
for (s = string; *s != '\0'; s++)
{
char c = *s;
if (c == '"')
length += backslashes + 1;
length++;
if (c == '\\')
backslashes++;
else
backslashes = 0;
}
if (quote_around)
length += backslashes + 1;
quoted_string = xmalloc (length + 1);
p = quoted_string;
backslashes = 0;
if (quote_around)
*p++ = '"';
for (s = string; *s != '\0'; s++)
{
char c = *s;
if (c == '"')
{
unsigned int j;
for (j = backslashes + 1; j > 0; j--)
*p++ = '\\';
}
*p++ = c;
if (c == '\\')
backslashes++;
else
backslashes = 0;
}
if (quote_around)
{
unsigned int j;
for (j = backslashes; j > 0; j--)
*p++ = '\\';
*p++ = '"';
}
*p = '\0';
new_argv[i] = quoted_string;
}
else
new_argv[i] = (char *) string;
}
new_argv[argc] = NULL;
return new_argv;
}
static int is_binary(char * path)
{
DWORD bin;
if ( GetBinaryType (path, &bin) ){
switch(bin){
case SCS_32BIT_BINARY: /* fall */
case SCS_64BIT_BINARY: /* fall */
case SCS_DOS_BINARY: /* fall */
case SCS_WOW_BINARY: /* fall */
return 1;
}
}
return 0;
}
int
main(int argc, char **argv)
{
char **new_argv;
char **new_argv_real;
int i;
int nargc = argc == 0 ? (argc + 2) : (argc + 1);
int code;
int pkg_config_is_binary = 0;
char * pkg_config = PKG_CONFIG;
char * pkgconf = PKGCONF;
if ( is_binary(pkg_config) ) {
new_argv = xmalloc(nargc * sizeof (char *) );
new_argv[0] = pkg_config;
for ( i=1 ; i < argc ; ++i ){
new_argv[i] = argv[i];
}
new_argv[nargc-1] = NULL;
new_argv_real = prepare_spawn(new_argv);
code = _spawnv(_P_WAIT, pkg_config , (const char **) new_argv_real );
}
else if ( is_binary(pkgconf) ) {
int do_add_prefix = 1;
if ( argc < 2 ) {
do_add_prefix = 0 ;
}
else {
for ( i=1; i < argc ; ++i ) {
char * cur = argv[i];
if ( strcmp(cur,"--help") == 0 ||
strcmp(cur,"-h") == 0 ||
strcmp(cur,"--about") == 0 ||
strcmp(cur,"--atleast-pkgconfig-version") == 0 ||
strcmp(cur,"--version") == 0 ) {
do_add_prefix = 0 ;
break ;
}
}
}
if ( do_add_prefix ) {
++nargc;
new_argv = xmalloc(nargc * sizeof (char *) );
new_argv[0] = pkgconf;
#ifdef __i386__
new_argv[1] = "--personality=i686-w64-mingw32";
#else
new_argv[1] = "--personality=x86_64-w64-mingw32";
#endif
for ( i=1 ; i < argc ; ++i ) {
new_argv[i+1] = argv[i];
}
}
else {
new_argv = xmalloc(nargc * sizeof (char *) );
new_argv[0] = pkgconf;
for ( i=1 ; i < argc ; ++i ){
new_argv[i] = argv[i];
}
}
new_argv[nargc-1] = NULL;
new_argv_real = prepare_spawn(new_argv);
code = _spawnv(_P_WAIT, pkgconf , (const char **) new_argv_real );
}
else {
fputs("pkg-config is not installed. Install it with 'opam depext conf-pkg-config'\n",stderr);
exit(2);
}
if (code == -1) {
perror("Cannot exec pkg-config");
exit(127);
}
exit(code);
}