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