-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixprefix.c
262 lines (218 loc) · 5.08 KB
/
fixprefix.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static const char prefix_header[] = "prefix=";
#define PREFIX_HEADER_LENGTH 7
static const char **argv = NULL;
static int argc = 0;
static int args_buf_size = 0;
static void
add_argument (const char *argument)
{
if (args_buf_size == 0)
argv = malloc (sizeof (char *) * (args_buf_size = 8));
else if (argc + 1 > args_buf_size)
argv = realloc (argv, sizeof (char *) * (args_buf_size *= 2));
argv[argc++] = argument;
}
static void
split_arguments (char *cmd_line)
{
char *cmd_line_copy = malloc (strlen (cmd_line) + 1);
char *out_p = cmd_line_copy;
BOOL in_quotes = FALSE;
BOOL in_arg = FALSE;
while (TRUE)
{
if (*cmd_line == '\0' || (!in_quotes && *cmd_line == ' '))
{
if (in_arg)
{
*(out_p++) = '\0';
in_arg = FALSE;
}
if (*cmd_line == '\0')
break;
}
else if (*cmd_line == '"')
{
if (in_quotes)
in_quotes = FALSE;
else
{
if (!in_arg)
{
add_argument (out_p);
in_arg = TRUE;
}
in_quotes = TRUE;
}
}
else
{
if (!in_arg)
{
add_argument (out_p);
in_arg = TRUE;
}
*(out_p++) = *cmd_line;
}
cmd_line++;
}
}
static BOOL
get_file_contents (const char *filename,
char **contents_out,
size_t *size_out)
{
int length = 0;
int buf_size = 0;
char *buf = NULL;
FILE *in_file = NULL;
size_t got;
if ((in_file = fopen (filename, "rb")) == NULL)
{
fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
goto error;
}
while (TRUE)
{
int to_read;
if (buf_size - length < 1024)
{
char *new_buf;
if (buf_size == 0)
new_buf = malloc (buf_size = 1024);
else
new_buf = realloc (buf, buf_size *= 2);
if (new_buf == NULL)
{
fprintf (stderr, "Out of memory\n");
goto error;
}
buf = new_buf;
}
to_read = buf_size - length;
got = fread (buf + length, 1, to_read, in_file);
length += got;
if (got < to_read)
{
if (ferror (in_file))
{
fprintf (stderr, "error reading %s\n", filename);
goto error;
}
break;
}
}
fclose (in_file);
*contents_out = buf;
*size_out = length;
return TRUE;
error:
if (buf)
free (buf);
if (in_file)
fclose (in_file);
return FALSE;
}
static void
output_prefix (const char *prefix,
FILE *out_file)
{
while (*prefix)
{
if (*prefix == '\\')
fputc ('/', out_file);
else
fputc (*prefix, out_file);
prefix++;
}
}
static BOOL
rewrite_file (const char *filename,
const char *file_buf,
size_t file_length,
const char *prefix)
{
FILE *out_file;
const char *line_end;
out_file = fopen (filename, "wb");
if (out_file == NULL)
{
fprintf (stderr, "error opening %s for writing: %s\n",
filename, strerror (errno));
return FALSE;
}
/* The file should begin with 'prefix=' */
if (file_length >= PREFIX_HEADER_LENGTH &&
!memcmp (file_buf, prefix_header, PREFIX_HEADER_LENGTH) &&
(line_end = memchr (file_buf, '\n', file_length)))
{
/* Output the corrected prefix instead */
fputs ("prefix=\"", out_file);
output_prefix (prefix, out_file);
fputc ('"', out_file);
file_length -= line_end - file_buf;
file_buf = line_end;
}
fwrite (file_buf, 1, file_length, out_file);
fclose (out_file);
return TRUE;
}
static char *
get_short_path (const char *long_path)
{
DWORD length;
char *short_path;
length = GetShortPathName (long_path, NULL, 0);
if (length == 0)
return NULL;
short_path = malloc (length);
if (short_path == NULL)
return NULL;
if (GetShortPathName (long_path, short_path, length) == 0)
{
free (short_path);
return NULL;
}
return short_path;
}
int CALLBACK
WinMain (HINSTANCE instance,
HINSTANCE prev_instance,
char *cmd_line,
int cmd_show)
{
char *file_buf;
char *prefix;
size_t file_length;
int i;
split_arguments (cmd_line);
if (argc < 2)
{
fprintf (stderr, "usage: fixprefix <prefix> <file>...\n");
return 1;
}
prefix = get_short_path (argv[0]);
if (prefix == NULL)
{
fprintf (stderr, "error getting short path\n");
return 1;
}
for (i = 1; i < argc; i++)
{
if (!get_file_contents (argv[i], &file_buf, &file_length))
return 1;
if (!rewrite_file (argv[i], file_buf, file_length, prefix))
{
free (file_buf);
return 1;
}
free (file_buf);
}
free (prefix);
return 0;
}