… | |
… | |
14 | static u_handle pack_handle; |
14 | static u_handle pack_handle; |
15 | static u_handle lock_handle; |
15 | static u_handle lock_handle; |
16 | static char tmppath[MAX_PATH]; |
16 | static char tmppath[MAX_PATH]; |
17 | |
17 | |
18 | static int exe_argc; |
18 | static int exe_argc; |
19 | static const char *exe_argv[MAX_ARGC]; |
19 | static u_dynbuf exe_argv; |
20 | static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ |
20 | static u_dynbuf exe_args; /* actual arguments strings copied here */ |
21 | static unsigned int exe_argo; |
|
|
22 | |
21 | |
23 | static void |
22 | static void |
24 | tmpdir (const char *dir) |
23 | tmpdir (const char *dir) |
25 | { |
24 | { |
26 | static int cnt; |
25 | static int cnt; |
… | |
… | |
72 | #endif |
71 | #endif |
73 | systemv (argv); |
72 | systemv (argv); |
74 | } |
73 | } |
75 | |
74 | |
76 | static char *pack_base, *pack_end; |
75 | static char *pack_base, *pack_end; |
|
|
76 | static struct u_pack_tail *pack_tail; |
77 | static struct u_pack_hdr *pack_cur; |
77 | static struct u_pack_hdr *pack_cur; |
78 | static char *scratch; |
78 | static char *scratch; |
79 | static unsigned int scratch_size; |
79 | static unsigned int scratch_size; |
80 | |
80 | |
81 | #define PACK_NAME ((char *)(pack_cur + 1)) |
81 | #define PACK_NAME ((char *)(pack_cur + 1)) |
… | |
… | |
125 | |
125 | |
126 | addr = u_mmap (pack_handle, size); |
126 | addr = u_mmap (pack_handle, size); |
127 | if (!addr) |
127 | if (!addr) |
128 | return 0; |
128 | return 0; |
129 | |
129 | |
130 | pack_unmap (); |
130 | /*pack_unmap ();*/ |
131 | |
131 | |
132 | pack_base = addr; |
132 | pack_base = addr; |
133 | pack_end = pack_base + size; |
133 | pack_end = pack_base + size; |
134 | |
134 | |
135 | struct u_pack_tail *tail; |
|
|
136 | |
|
|
137 | tail = (void *)(pack_end - sizeof (*tail)); |
135 | pack_tail = (void *)(pack_end - sizeof (*pack_tail)); |
138 | |
136 | |
139 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
137 | if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
140 | return 0; |
138 | return 0; |
141 | |
139 | |
142 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); |
140 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size)); |
143 | |
141 | |
|
|
142 | if (pack_cur->type != T_META) |
|
|
143 | return 0; |
|
|
144 | |
144 | scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); |
145 | scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed)); |
145 | if (!scratch) |
146 | if (!scratch) |
146 | return 0; |
147 | return 0; |
147 | |
148 | |
148 | return 1; |
149 | return 1; |
149 | } |
150 | } |
150 | |
151 | |
151 | static void |
152 | static void |
152 | exe_info (void) |
153 | add_arg (char *arg, unsigned int len) |
153 | { |
154 | { |
154 | if (!pack_map ()) |
155 | char *addr = u_dynbuf_append (&exe_args, arg, len); |
155 | u_fatal ("unable to locate packfile in executable - executable corrupted?"); |
156 | u_dynbuf_append (&exe_argv, &addr, sizeof (addr)); |
156 | |
|
|
157 | if (pack_cur->type != T_META) |
|
|
158 | u_fatal ("unable to locate executable metadata - executable corrupted?"); |
|
|
159 | |
|
|
160 | strcpy (exe_id, PACK_NAME); |
|
|
161 | } |
157 | } |
162 | |
158 | |
163 | static void |
159 | static void |
164 | load (void) |
160 | load (void) |
165 | { |
161 | { |
|
|
162 | strcpy (exe_id , PACK_NAME); |
|
|
163 | strcpy (exe_ver, PACK_DATA); |
|
|
164 | u_set_exe_info (); |
|
|
165 | |
166 | if (u_chdir (exe_dir)) |
166 | if (u_chdir (exe_dir)) |
167 | u_fatal ("unable to change to application data directory"); |
167 | u_fatal ("unable to change to application data directory"); |
168 | |
168 | |
169 | u_handle h = u_open ("override"); |
169 | u_handle h = u_open ("override"); |
170 | if (u_valid (h)) |
170 | if (u_valid (h)) |
171 | { |
171 | { |
172 | u_handle oh = pack_handle; |
172 | u_handle oh = pack_handle; |
173 | |
173 | |
|
|
174 | pack_unmap (); |
174 | pack_handle = h; |
175 | pack_handle = h; |
|
|
176 | |
175 | if (!pack_map ()) |
177 | if (pack_map () |
|
|
178 | && strcmp (exe_id , PACK_NAME) == 0 |
|
|
179 | && strcmp (exe_ver, PACK_DATA) <= 0) |
|
|
180 | u_setenv ("URLADER_OVERRIDE", "override"); |
|
|
181 | else |
176 | { |
182 | { |
|
|
183 | pack_unmap (); |
177 | pack_handle = oh; |
184 | pack_handle = oh; |
178 | oh = h; |
185 | oh = h; |
|
|
186 | pack_map (); |
179 | } |
187 | } |
180 | |
188 | |
181 | u_close (oh); |
189 | u_close (oh); |
182 | } |
190 | } |
183 | |
|
|
184 | if (pack_cur->type != T_META) |
|
|
185 | u_fatal ("unable to locate override metadata"); |
|
|
186 | |
191 | |
187 | strcpy (exe_ver, PACK_DATA); |
192 | strcpy (exe_ver, PACK_DATA); |
188 | u_set_exe_info (); |
193 | u_set_exe_info (); |
189 | pack_next (); |
194 | pack_next (); |
190 | |
195 | |
191 | for (;;) |
196 | for (;;) |
192 | { |
197 | { |
193 | if (pack_cur->type == T_ENV) |
198 | if (pack_cur->type == T_ENV) |
194 | u_setenv (PACK_NAME, PACK_DATA); |
199 | u_setenv (PACK_NAME, PACK_DATA); |
195 | else if (pack_cur->type == T_ARG) |
200 | else if (pack_cur->type == T_ARG) |
196 | { |
201 | add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1); |
197 | int len = u_16 (pack_cur->namelen) + 1; |
|
|
198 | exe_argv [exe_argc++] = exe_args + exe_argo; |
|
|
199 | memcpy (exe_args + exe_argo, PACK_NAME, len); |
|
|
200 | exe_argo += len; |
|
|
201 | } |
|
|
202 | else |
202 | else |
203 | break; |
203 | break; |
204 | |
204 | |
205 | pack_next (); |
205 | pack_next (); |
206 | } |
206 | } |
… | |
… | |
293 | } |
293 | } |
294 | |
294 | |
295 | static void |
295 | static void |
296 | execute (void) |
296 | execute (void) |
297 | { |
297 | { |
298 | exe_argv [exe_argc] = 0; |
298 | char *null = 0; |
299 | systemv (exe_argv); |
299 | u_dynbuf_append (&exe_argv, &null, sizeof (null)); |
|
|
300 | systemv ((const char *const *)exe_argv.addr); |
300 | } |
301 | } |
301 | |
302 | |
|
|
303 | // this argc/argv is without argv [0] |
302 | static void |
304 | static void |
303 | doit (void) |
305 | doit (int argc, char *argv[]) |
304 | { |
306 | { |
|
|
307 | int i; |
|
|
308 | |
|
|
309 | u_setenv ("URLADER_CURRDIR", currdir); |
|
|
310 | |
305 | u_set_datadir (); |
311 | u_set_datadir (); |
306 | u_mkdir (datadir); |
312 | u_mkdir (datadir); |
307 | |
313 | |
308 | exe_info (); |
314 | if (!pack_map ()) |
309 | u_set_exe_info (); |
315 | u_fatal ("unable to map pack file - executable corrupted?"); |
310 | |
316 | |
311 | load (); |
317 | load (); |
|
|
318 | |
|
|
319 | while (argc--) |
|
|
320 | { |
|
|
321 | add_arg (*argv, strlen (*argv) + 1); |
|
|
322 | ++argv; |
|
|
323 | } |
|
|
324 | |
312 | execute (); |
325 | execute (); |
313 | } |
326 | } |
314 | |
327 | |
315 | #ifdef _WIN32 |
328 | #ifdef _WIN32 |
316 | |
329 | |
317 | int APIENTRY |
330 | int APIENTRY |
318 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
331 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
319 | { |
332 | { |
320 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
333 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
321 | u_fatal ("unable to find executable pack"); |
334 | u_fatal ("unable to find executable pack"); |
|
|
335 | |
|
|
336 | u_setenv ("URLADER_EXEPATH", tmppath); |
322 | |
337 | |
323 | pack_handle = u_open (tmppath); |
338 | pack_handle = u_open (tmppath); |
324 | if (!u_valid (pack_handle)) |
339 | if (!u_valid (pack_handle)) |
325 | u_fatal ("unable to open executable pack"); |
340 | u_fatal ("unable to open executable pack"); |
326 | |
341 | |
327 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
342 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
328 | strcpy (currdir, "."); |
343 | strcpy (currdir, "."); |
329 | |
344 | |
|
|
345 | doit (1, &argv); |
|
|
346 | |
330 | return 0; |
347 | return 0; |
331 | } |
348 | } |
332 | |
349 | |
333 | #else |
350 | #else |
334 | |
351 | |
335 | int |
352 | int |
336 | main (int argc, char *argv[]) |
353 | main (int argc, char *argv[]) |
337 | { |
354 | { |
|
|
355 | u_setenv ("URLADER_EXEPATH", argv [0]); |
|
|
356 | |
338 | pack_handle = u_open (argv [0]); |
357 | pack_handle = u_open (argv [0]); |
339 | if (!u_valid (pack_handle)) |
358 | if (!u_valid (pack_handle)) |
340 | u_fatal ("unable to open executable pack"); |
359 | u_fatal ("unable to open executable pack"); |
341 | |
360 | |
342 | if (!getcwd (currdir, sizeof (currdir))) |
361 | if (!getcwd (currdir, sizeof (currdir))) |
343 | strcpy (currdir, "."); |
362 | strcpy (currdir, "."); |
344 | |
363 | |
345 | #if 0 |
364 | #if 0 |
|
|
365 | /* intersperse hostname, for whatever reason */ |
|
|
366 | |
346 | if (gethostname (tmppath, sizeof (tmppath))) |
367 | if (gethostname (tmppath, sizeof (tmppath))) |
347 | strcpy (tmppath, "default"); |
368 | strcpy (tmppath, "default"); |
348 | |
369 | |
349 | u_append (datadir, tmppath); |
370 | u_append (datadir, tmppath); |
350 | #endif |
371 | #endif |
351 | |
372 | |
352 | doit (); |
373 | doit (argc - 1, argv + 1); |
353 | |
374 | |
354 | return 0; |
375 | return 0; |
355 | } |
376 | } |
356 | |
377 | |
357 | #endif |
378 | #endif |