1 | #ifndef URLADER |
1 | #include "urlib.h" |
2 | # define URLADER "urlader" |
2 | #include "urlib.c" |
3 | #endif |
|
|
4 | #define URLADER_VERSION "1.0" |
|
|
5 | |
3 | |
6 | #include <stdio.h> |
4 | #include <stdio.h> |
7 | #include <stdlib.h> |
5 | #include <stdlib.h> |
8 | #include <unistd.h> |
6 | #include <unistd.h> |
9 | #include <errno.h> |
7 | #include <errno.h> |
10 | #include <string.h> |
8 | #include <string.h> |
11 | #include <time.h> |
9 | #include <time.h> |
12 | #include <fcntl.h> |
|
|
13 | |
10 | |
14 | #include "liblzf/lzf_d.c" |
11 | #include "liblzf/lzf_d.c" |
15 | |
12 | |
16 | #define TAIL_MAGIC "SCHMORPPACK0" |
13 | /* some simple? dynamic memory management for paths might save ltos of ram */ |
17 | |
|
|
18 | #ifdef _WIN32 |
|
|
19 | |
|
|
20 | #include <windows.h> |
|
|
21 | //#include <winbase.h> |
|
|
22 | #include <shlobj.h> |
|
|
23 | #include <shlwapi.h> |
|
|
24 | #include <wininet.h> |
|
|
25 | |
|
|
26 | static DWORD dword; |
|
|
27 | |
|
|
28 | #define u_handle HANDLE |
|
|
29 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
|
|
30 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
|
|
31 | #define u_chdir(path) !SetCurrentDirectory (path) |
|
|
32 | #define u_rename(fr,to) !MoveFile (fr, to) |
|
|
33 | #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
|
|
34 | #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
|
|
35 | #define u_close(handle) CloseHandle (handle) |
|
|
36 | #define u_append(path,add) PathAppend (path, add) |
|
|
37 | #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1) |
|
|
38 | |
|
|
39 | #else |
|
|
40 | |
|
|
41 | #include <sys/mman.h> |
|
|
42 | #include <sys/types.h> |
|
|
43 | #include <pwd.h> |
|
|
44 | |
|
|
45 | #ifdef PATH_MAX |
|
|
46 | #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX) |
|
|
47 | #else |
|
|
48 | #define MAX_PATH 4096 |
|
|
49 | #endif |
|
|
50 | |
|
|
51 | #define u_handle int |
|
|
52 | #define u_setenv(name,value) setenv (name, value, 1) |
|
|
53 | #define u_mkdir(path) mkdir (path, 0777) |
|
|
54 | #define u_chdir(path) chdir (path) |
|
|
55 | #define u_rename(fr,to) rename (fr, to) |
|
|
56 | #define u_open(path) open (path, O_RDONLY) + 1 |
|
|
57 | #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1 |
|
|
58 | #define u_close(handle) close (handle - 1) |
|
|
59 | #define u_append(path,add) strcat (strcat (path, "/"), add) |
|
|
60 | #define u_write(handle,data,len) write ((handle) - 1, data, len) |
|
|
61 | |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | #define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1]) |
|
|
65 | #define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3]) |
|
|
66 | |
|
|
67 | static u_handle pack_handle; |
14 | static u_handle pack_handle; |
|
|
15 | static u_handle lock_handle; |
68 | static char tmppath[MAX_PATH]; |
16 | static char tmppath[MAX_PATH]; |
69 | static char currdir[MAX_PATH]; |
|
|
70 | static char datadir[MAX_PATH]; // %AppData%/urlader |
|
|
71 | static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID |
|
|
72 | static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER |
|
|
73 | static char exe_id[MAX_PATH]; |
|
|
74 | static char exe_ver[MAX_PATH]; |
|
|
75 | |
17 | |
76 | static int exe_argc; |
18 | static int exe_argc; |
77 | static const char *exe_argv[32]; |
19 | static u_dynbuf exe_argv; |
78 | |
20 | static u_dynbuf exe_args; /* actual arguments strings copied here */ |
79 | static void |
|
|
80 | fatal (const char *msg) |
|
|
81 | { |
|
|
82 | #ifdef _WIN32 |
|
|
83 | MessageBox (0, msg, URLADER, 0); |
|
|
84 | #else |
|
|
85 | fprintf (stderr, "%s: %s\n", URLADER, msg); |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | _exit (1); |
|
|
89 | } |
|
|
90 | |
21 | |
91 | static void |
22 | static void |
92 | tmpdir (const char *dir) |
23 | tmpdir (const char *dir) |
93 | { |
24 | { |
94 | static int cnt; |
25 | static int cnt; |
… | |
… | |
105 | return; |
36 | return; |
106 | } |
37 | } |
107 | } |
38 | } |
108 | |
39 | |
109 | static void |
40 | static void |
110 | systemv (const char *const argv[], int dowait) |
41 | systemv (const char *const argv[]) |
111 | { |
42 | { |
112 | #ifdef _WIN32 |
43 | #ifdef _WIN32 |
113 | _spawnv (P_WAIT, argv [0], argv); |
44 | _spawnv (P_WAIT, argv [0], argv); |
114 | #else |
45 | #else |
115 | pid_t pid = dowait ? fork () : 0; |
46 | pid_t pid = fork (); |
116 | |
47 | |
117 | if (pid < 0) |
48 | if (pid < 0) |
118 | fatal ("fork failure"); |
49 | u_fatal ("fork failure"); |
119 | |
50 | |
120 | if (!pid) |
51 | if (!pid) |
121 | { |
52 | { |
122 | execv (argv [0], (void *)argv); |
53 | execv (argv [0], (void *)argv); |
123 | _exit (124); |
54 | _exit (124); |
… | |
… | |
136 | const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; |
67 | const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; |
137 | sprintf (buf, "\"%s\"", path); |
68 | sprintf (buf, "\"%s\"", path); |
138 | #else |
69 | #else |
139 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
70 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
140 | #endif |
71 | #endif |
141 | systemv (argv, 1); |
72 | systemv (argv); |
142 | } |
73 | } |
143 | |
|
|
144 | enum |
|
|
145 | { |
|
|
146 | T_NULL, // 5 |
|
|
147 | T_META, // 1 : exe_id, exe_ver |
|
|
148 | T_ARG, // 2 : arg |
|
|
149 | T_ENV, // 3 : name, value |
|
|
150 | T_DIR, // 4+: path |
|
|
151 | T_FILE, // 4+: path, data |
|
|
152 | T_NUM |
|
|
153 | }; |
|
|
154 | |
|
|
155 | enum |
|
|
156 | { |
|
|
157 | F_EXEC = 1, |
|
|
158 | F_LZF = 2, |
|
|
159 | F_NULL = 0 |
|
|
160 | }; |
|
|
161 | |
|
|
162 | struct hdr |
|
|
163 | { |
|
|
164 | unsigned char type; |
|
|
165 | unsigned char flags; |
|
|
166 | unsigned char namelen[2]; |
|
|
167 | unsigned char datalen[4]; |
|
|
168 | }; |
|
|
169 | |
|
|
170 | struct tail { |
|
|
171 | unsigned char max_filesize[4]; |
|
|
172 | unsigned char size[4]; |
|
|
173 | char magic[12]; |
|
|
174 | }; |
|
|
175 | |
74 | |
176 | static char *pack_base, *pack_end; |
75 | static char *pack_base, *pack_end; |
|
|
76 | static struct u_pack_tail *pack_tail; |
177 | static struct hdr *pack_cur; |
77 | static struct u_pack_hdr *pack_cur; |
178 | static char *scratch; |
78 | static char *scratch; |
179 | static unsigned int scratch_len; |
79 | static unsigned int scratch_size; |
180 | |
80 | |
181 | #define PACK_NAME ((char *)(pack_cur + 1)) |
81 | #define PACK_NAME ((char *)(pack_cur + 1)) |
182 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
82 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
183 | #define PACK_VALID pack_cur->type |
83 | #define PACK_VALID pack_cur->type |
184 | |
84 | |
185 | static void |
85 | static void |
186 | pack_next (void) |
86 | pack_next (void) |
187 | { |
87 | { |
188 | unsigned int d = u_32 (pack_cur->datalen); |
88 | unsigned int d = u_32 (pack_cur->datalen); |
189 | |
89 | |
190 | pack_cur = (struct hdr *)(PACK_DATA + d); |
90 | pack_cur = (struct u_pack_hdr *)(PACK_DATA + d); |
191 | } |
91 | } |
192 | |
92 | |
193 | static void |
93 | static void |
194 | pack_unmap (void) |
94 | pack_unmap (void) |
195 | { |
95 | { |
196 | if (!pack_base) |
96 | if (pack_base) |
197 | return; |
97 | { |
198 | |
|
|
199 | free (scratch); |
|
|
200 | |
|
|
201 | #ifdef _WIN32 |
|
|
202 | UnmapViewOfFile (pack_base); |
|
|
203 | #else |
|
|
204 | munmap (pack_base, pack_end - pack_base); |
98 | u_munmap (pack_base, pack_end - pack_base); |
205 | #endif |
99 | pack_base = 0; |
|
|
100 | } |
|
|
101 | |
|
|
102 | if (scratch) |
|
|
103 | { |
|
|
104 | u_free (scratch, scratch_size); |
|
|
105 | scratch = 0; |
|
|
106 | } |
206 | } |
107 | } |
207 | |
108 | |
208 | static int |
109 | static int |
209 | pack_map (void) |
110 | pack_map (void) |
210 | { |
111 | { |
|
|
112 | char *addr; |
|
|
113 | unsigned int size; |
|
|
114 | |
211 | #ifdef _WIN32 |
115 | #ifdef _WIN32 |
212 | BY_HANDLE_FILE_INFORMATION fi; |
116 | BY_HANDLE_FILE_INFORMATION fi; |
213 | |
117 | |
214 | if (!GetFileInformationByHandle (pack_handle, &fi)) |
118 | if (!GetFileInformationByHandle (pack_handle, &fi)) |
215 | return 0; |
119 | return 0; |
216 | |
120 | |
217 | if (fi.nFileSizeHigh) // too big |
121 | size = fi.nFileSizeLow; |
218 | return 0; |
|
|
219 | |
|
|
220 | HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL); |
|
|
221 | |
|
|
222 | if (!handle) |
|
|
223 | return 0; |
|
|
224 | |
|
|
225 | pack_unmap (); |
|
|
226 | |
|
|
227 | pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow); |
|
|
228 | |
|
|
229 | CloseHandle (handle); |
|
|
230 | |
|
|
231 | pack_end = pack_base + fi.nFileSizeLow; |
|
|
232 | #else |
122 | #else |
233 | off_t len = lseek (pack_handle - 1, 0, SEEK_END); |
123 | size = lseek (pack_handle, 0, SEEK_END); |
|
|
124 | #endif |
234 | |
125 | |
|
|
126 | addr = u_mmap (pack_handle, size); |
|
|
127 | if (!addr) |
|
|
128 | return 0; |
|
|
129 | |
235 | pack_unmap (); |
130 | /*pack_unmap ();*/ |
236 | |
131 | |
237 | pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0); |
|
|
238 | if (pack_base == (void *)-1) |
|
|
239 | pack_base = 0; |
132 | pack_base = addr; |
240 | |
|
|
241 | pack_end = pack_base + len; |
133 | pack_end = pack_base + size; |
242 | #endif |
|
|
243 | |
134 | |
244 | if (!pack_base) |
|
|
245 | return 0; |
|
|
246 | |
|
|
247 | struct tail *tail; |
|
|
248 | |
|
|
249 | tail = (void *)(pack_end - sizeof (*tail)); |
135 | pack_tail = (void *)(pack_end - sizeof (*pack_tail)); |
250 | |
136 | |
251 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
137 | if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
252 | return 0; |
138 | return 0; |
253 | |
139 | |
254 | pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); |
140 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size)); |
255 | |
141 | |
|
|
142 | if (pack_cur->type != T_META) |
|
|
143 | return 0; |
|
|
144 | |
|
|
145 | scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed)); |
256 | free (scratch); |
146 | if (!scratch) |
257 | scratch = malloc (scratch_len = u_32 (tail->max_filesize)); |
147 | return 0; |
258 | |
148 | |
259 | return 1; |
149 | return 1; |
260 | } |
150 | } |
261 | |
151 | |
262 | static void |
152 | static void |
263 | exe_info (void) |
153 | add_arg (char *arg, unsigned int len) |
264 | { |
154 | { |
265 | if (!pack_map ()) |
155 | char *addr = u_dynbuf_append (&exe_args, arg, len); |
266 | fatal ("unable to locate packfile in executable - executable corrupted?"); |
156 | u_dynbuf_append (&exe_argv, &addr, sizeof (addr)); |
267 | |
|
|
268 | if (pack_cur->type != T_META) |
|
|
269 | fatal ("unable to locate executable metadata - executable corrupted?"); |
|
|
270 | |
|
|
271 | strcpy (exe_id, PACK_NAME); |
|
|
272 | } |
157 | } |
273 | |
158 | |
274 | static void |
159 | static void |
275 | load (void) |
160 | load (void) |
276 | { |
161 | { |
277 | u_mkdir (datadir); |
162 | strcpy (exe_id , PACK_NAME); |
278 | |
163 | strcpy (exe_ver, PACK_DATA); |
279 | strcpy (exe_dir, datadir); |
164 | u_set_exe_info (); |
280 | u_append (exe_dir, exe_id); |
|
|
281 | u_mkdir (exe_dir); |
|
|
282 | |
165 | |
283 | if (u_chdir (exe_dir)) |
166 | if (u_chdir (exe_dir)) |
284 | fatal ("unable to change to application data directory"); |
167 | u_fatal ("unable to change to application data directory"); |
285 | |
168 | |
286 | u_handle h = u_open ("override"); |
169 | u_handle h = u_open ("override"); |
287 | if (h) |
170 | if (u_valid (h)) |
288 | { |
171 | { |
289 | u_handle oh = pack_handle; |
172 | u_handle oh = pack_handle; |
290 | |
173 | |
|
|
174 | pack_unmap (); |
291 | pack_handle = h; |
175 | pack_handle = h; |
|
|
176 | |
292 | 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 |
293 | { |
182 | { |
|
|
183 | pack_unmap (); |
294 | pack_handle = oh; |
184 | pack_handle = oh; |
295 | oh = h; |
185 | oh = h; |
|
|
186 | pack_map (); |
296 | } |
187 | } |
297 | |
188 | |
298 | u_close (oh); |
189 | u_close (oh); |
299 | } |
190 | } |
300 | |
191 | |
301 | if (pack_cur->type != T_META) |
|
|
302 | fatal ("unable to locate override metadata"); |
|
|
303 | |
|
|
304 | strcpy (exe_ver, PACK_DATA); |
192 | strcpy (exe_ver, PACK_DATA); |
|
|
193 | u_set_exe_info (); |
305 | pack_next (); |
194 | pack_next (); |
306 | |
195 | |
|
|
196 | for (;;) |
|
|
197 | { |
|
|
198 | if (pack_cur->type == T_ENV) |
|
|
199 | u_setenv (PACK_NAME, PACK_DATA); |
307 | while (pack_cur->type == T_ARG) |
200 | else if (pack_cur->type == T_ARG) |
308 | { |
201 | add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1); |
309 | exe_argv [exe_argc++] = strdup (PACK_NAME); |
202 | else |
|
|
203 | break; |
|
|
204 | |
310 | pack_next (); |
205 | pack_next (); |
311 | } |
206 | } |
|
|
207 | |
|
|
208 | done_env_arg: |
312 | |
209 | |
313 | while (pack_cur->type == T_ENV) |
210 | strcat (strcpy (tmppath, execdir), ".lck"); |
314 | { |
211 | lock_handle = u_lock (tmppath, 0, 1); |
315 | u_setenv (PACK_NAME, PACK_DATA); |
212 | if (!lock_handle) |
316 | pack_next (); |
213 | u_fatal ("unable to lock application instance"); |
317 | } |
|
|
318 | |
|
|
319 | strcpy (execdir, exe_dir); |
|
|
320 | u_append (execdir, "i-"); |
|
|
321 | strcat (execdir, exe_ver); |
|
|
322 | |
214 | |
323 | if (access (execdir, F_OK)) |
215 | if (access (execdir, F_OK)) |
324 | { |
216 | { |
325 | // does not exist yet, so unpack and move |
217 | // does not exist yet, so unpack and move |
326 | tmpdir (exe_dir); |
218 | tmpdir (exe_dir); |
327 | |
219 | |
328 | if (u_chdir (tmppath)) |
220 | if (u_chdir (tmppath)) |
329 | fatal ("unable to change to new instance directory"); |
221 | u_fatal ("unable to change to new instance directory"); |
330 | |
222 | |
331 | for (;;) |
223 | for (;;) |
332 | { |
224 | { |
333 | switch (pack_cur->type) |
225 | switch (pack_cur->type) |
334 | { |
226 | { |
… | |
… | |
341 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); |
233 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); |
342 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
234 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
343 | char *data = PACK_DATA; |
235 | char *data = PACK_DATA; |
344 | |
236 | |
345 | if (pack_cur->flags & F_LZF) |
237 | if (pack_cur->flags & F_LZF) |
346 | if (dlen = lzf_decompress (data, len, scratch, scratch_len)) |
238 | if (dlen = lzf_decompress (data, len, scratch, scratch_size)) |
347 | { |
239 | { |
348 | data = scratch; |
240 | data = scratch; |
349 | len = dlen; |
241 | len = dlen; |
350 | } |
242 | } |
351 | else |
243 | else |
352 | fatal ("unable to uncompress file data - pack corrupted?"); |
244 | u_fatal ("unable to uncompress file data - pack corrupted?"); |
353 | |
245 | |
354 | if (!h) |
246 | if (!u_valid (h)) |
355 | fatal ("unable to unpack file from packfile - disk full?"); |
247 | u_fatal ("unable to unpack file from packfile - disk full?"); |
356 | |
248 | |
357 | if (u_write (h, data, len) != len) |
249 | if (u_write (h, data, len) != len) |
358 | fatal ("unable to unpack file from packfile - disk full?"); |
250 | u_fatal ("unable to unpack file from packfile - disk full?"); |
359 | |
251 | |
|
|
252 | u_fsync (h); |
360 | u_close (h); |
253 | u_close (h); |
361 | } |
254 | } |
362 | break; |
255 | break; |
363 | |
256 | |
364 | case T_NULL: |
257 | case T_NULL: |
… | |
… | |
368 | pack_next (); |
261 | pack_next (); |
369 | } |
262 | } |
370 | |
263 | |
371 | done: |
264 | done: |
372 | if (u_chdir (datadir)) |
265 | if (u_chdir (datadir)) |
373 | fatal ("unable to change to data directory"); |
266 | u_fatal ("unable to change to data directory"); |
|
|
267 | |
|
|
268 | u_sync (); |
374 | |
269 | |
375 | if (u_rename (tmppath, execdir)) |
270 | if (u_rename (tmppath, execdir)) |
376 | deltree (tmppath); // if move fails, delete new, assume other process created it independently |
271 | deltree (tmppath); // if move fails, delete new, assume other process created it independently |
377 | } |
272 | } |
378 | |
273 | |
379 | pack_unmap (); |
274 | pack_unmap (); |
380 | u_close (pack_handle); |
275 | u_close (pack_handle); |
381 | |
276 | |
382 | if (u_chdir (execdir)) |
277 | if (u_chdir (execdir)) |
383 | fatal ("unable to change to application instance directory"); |
278 | u_fatal ("unable to change to application instance directory"); |
384 | |
279 | |
385 | u_setenv ("URLADER_VERSION", URLADER_VERSION); |
280 | u_setenv ("URLADER_VERSION", URLADER_VERSION); |
386 | u_setenv ("URLADER_DATADIR", datadir); |
|
|
387 | u_setenv ("URLADER_EXECDIR", execdir); |
|
|
388 | u_setenv ("URLADER_EXE_ID" , exe_id); |
|
|
389 | u_setenv ("URLADER_EXE_DIR", exe_dir); |
|
|
390 | u_setenv ("URLADER_EXE_VER", exe_ver); |
|
|
391 | |
281 | |
|
|
282 | #if 0 |
392 | // yes, this is overkill |
283 | // yes, this is overkill |
393 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
284 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
394 | u_setenv ("LIBPATH" , execdir); // aix |
285 | u_setenv ("LIBPATH" , execdir); // aix |
395 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
286 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
396 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
287 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
397 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
288 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
398 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
289 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
399 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
290 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
400 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
291 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
|
|
292 | #endif |
401 | } |
293 | } |
402 | |
294 | |
403 | static void |
295 | static void |
404 | execute (void) |
296 | execute (void) |
405 | { |
297 | { |
406 | exe_argv [exe_argc] = 0; |
298 | char *null = 0; |
407 | systemv (exe_argv, 0); |
299 | u_dynbuf_append (&exe_argv, &null, sizeof (null)); |
|
|
300 | systemv ((const char *const *)exe_argv.addr); |
|
|
301 | } |
|
|
302 | |
|
|
303 | // this argc/argv is without argv [0] |
|
|
304 | static void |
|
|
305 | doit (int argc, char *argv[]) |
|
|
306 | { |
|
|
307 | int i; |
|
|
308 | |
|
|
309 | u_setenv ("URLADER_CURRDIR", currdir); |
|
|
310 | |
|
|
311 | u_set_datadir (); |
|
|
312 | u_mkdir (datadir); |
|
|
313 | |
|
|
314 | if (!pack_map ()) |
|
|
315 | u_fatal ("unable to map pack file - executable corrupted?"); |
|
|
316 | |
|
|
317 | load (); |
|
|
318 | |
|
|
319 | while (argc--) |
|
|
320 | { |
|
|
321 | add_arg (*argv, strlen (*argv) + 1); |
|
|
322 | ++argv; |
|
|
323 | } |
|
|
324 | |
|
|
325 | execute (); |
408 | } |
326 | } |
409 | |
327 | |
410 | #ifdef _WIN32 |
328 | #ifdef _WIN32 |
411 | |
329 | |
412 | int APIENTRY |
330 | int APIENTRY |
413 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
331 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
414 | { |
332 | { |
415 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
333 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
416 | fatal ("unable to find executable pack"); |
334 | u_fatal ("unable to find executable pack"); |
417 | |
335 | |
|
|
336 | u_setenv ("URLADER_EXEPATH", tmppath); |
|
|
337 | |
418 | if (!(pack_handle = u_open (tmppath))) |
338 | pack_handle = u_open (tmppath); |
|
|
339 | if (!u_valid (pack_handle)) |
419 | fatal ("unable to open executable pack"); |
340 | u_fatal ("unable to open executable pack"); |
420 | |
|
|
421 | exe_info (); |
|
|
422 | |
341 | |
423 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
342 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
424 | strcpy (currdir, "."); |
343 | strcpy (currdir, "."); |
425 | |
344 | |
426 | if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) |
345 | doit (1, &argv); |
427 | fatal ("unable to find application data directory"); |
|
|
428 | |
|
|
429 | u_mkdir (datadir); |
|
|
430 | u_append (datadir, URLADER); |
|
|
431 | |
|
|
432 | load (); |
|
|
433 | execute (); |
|
|
434 | |
346 | |
435 | return 0; |
347 | return 0; |
436 | } |
348 | } |
437 | |
349 | |
438 | #else |
350 | #else |
439 | |
351 | |
440 | int |
352 | int |
441 | main (int argc, char *argv[]) |
353 | main (int argc, char *argv[]) |
442 | { |
354 | { |
443 | char *home = getenv ("HOME"); |
355 | u_setenv ("URLADER_EXEPATH", argv [0]); |
444 | |
356 | |
445 | // we assume fd 0 and 2 are "normal" |
|
|
446 | |
|
|
447 | if (!(pack_handle = u_open (argv [0]))) |
357 | pack_handle = u_open (argv [0]); |
|
|
358 | if (!u_valid (pack_handle)) |
448 | fatal ("unable to open executable pack"); |
359 | u_fatal ("unable to open executable pack"); |
449 | |
|
|
450 | exe_info (); |
|
|
451 | |
|
|
452 | if (!home) |
|
|
453 | { |
|
|
454 | struct passwd *pw; |
|
|
455 | |
|
|
456 | if ((pw = getpwuid (getuid ()))) |
|
|
457 | home = pw->pw_dir; |
|
|
458 | else |
|
|
459 | home = "/tmp"; |
|
|
460 | } |
|
|
461 | |
360 | |
462 | if (!getcwd (currdir, sizeof (currdir))) |
361 | if (!getcwd (currdir, sizeof (currdir))) |
463 | strcpy (currdir, "."); |
362 | strcpy (currdir, "."); |
464 | |
363 | |
465 | u_mkdir (home); |
364 | #if 0 |
466 | sprintf (datadir, "%s/.%s", home, URLADER); |
365 | /* intersperse hostname, for whatever reason */ |
467 | u_mkdir (datadir); |
|
|
468 | |
366 | |
469 | if (gethostname (tmppath, sizeof (tmppath))) |
367 | if (gethostname (tmppath, sizeof (tmppath))) |
470 | strcpy (tmppath, "default"); |
368 | strcpy (tmppath, "default"); |
471 | |
369 | |
472 | u_append (datadir, tmppath); |
370 | u_append (datadir, tmppath); |
|
|
371 | #endif |
473 | |
372 | |
474 | load (); |
373 | doit (argc - 1, argv + 1); |
475 | execute (); |
|
|
476 | |
374 | |
477 | return 0; |
375 | return 0; |
478 | } |
376 | } |
479 | |
377 | |
480 | #endif |
378 | #endif |