1 | #ifndef URLADER |
1 | /* |
2 | # define URLADER "urlader" |
2 | * Copyright (c) 2012 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | #endif |
3 | * |
4 | #define URLADER_VERSION "1.0" |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
5 | * tion, are permitted provided that the following conditions are met: |
|
|
6 | * |
|
|
7 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
8 | * this list of conditions and the following disclaimer. |
|
|
9 | * |
|
|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
11 | * notice, this list of conditions and the following disclaimer in the |
|
|
12 | * documentation and/or other materials provided with the distribution. |
|
|
13 | * |
|
|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
18 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
23 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
24 | * |
|
|
25 | * Alternatively, the contents of this file may be used under the terms of |
|
|
26 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
27 | * in which case the provisions of the GPL are applicable instead of |
|
|
28 | * the above. If you wish to allow the use of your version of this file |
|
|
29 | * only under the terms of the GPL and not to allow others to use your |
|
|
30 | * version of this file under the BSD license, indicate your decision |
|
|
31 | * by deleting the provisions above and replace them with the notice |
|
|
32 | * and other provisions required by the GPL. If you do not delete the |
|
|
33 | * provisions above, a recipient may use your version of this file under |
|
|
34 | * either the BSD or the GPL. |
|
|
35 | */ |
|
|
36 | |
|
|
37 | #include "urlib.h" |
|
|
38 | #include "urlib.c" |
5 | |
39 | |
6 | #include <stdio.h> |
40 | #include <stdio.h> |
7 | #include <stdlib.h> |
41 | #include <stdlib.h> |
8 | #include <unistd.h> |
42 | #include <unistd.h> |
9 | #include <errno.h> |
43 | #include <errno.h> |
10 | #include <string.h> |
44 | #include <string.h> |
11 | #include <time.h> |
45 | #include <time.h> |
12 | #include <fcntl.h> |
|
|
13 | |
46 | |
14 | #include "liblzf/lzf_d.c" |
47 | #include "liblzf/lzf_d.c" |
15 | |
48 | |
16 | #define TAIL_MAGIC "SCHMORPPACK0" |
49 | /* 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; |
50 | static u_handle pack_handle; |
|
|
51 | static u_handle lock_handle; |
68 | static char tmppath[MAX_PATH]; |
52 | 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 | |
53 | |
76 | static int exe_argc; |
54 | static int exe_argc; |
77 | static const char *exe_argv[32]; |
55 | static u_dynbuf exe_argv; |
78 | |
56 | 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 | |
57 | |
91 | static void |
58 | static void |
92 | tmpdir (const char *dir) |
59 | tmpdir (const char *dir) |
93 | { |
60 | { |
94 | static int cnt; |
61 | static int cnt; |
… | |
… | |
105 | return; |
72 | return; |
106 | } |
73 | } |
107 | } |
74 | } |
108 | |
75 | |
109 | static void |
76 | static void |
110 | systemv (const char *const argv[], int dowait) |
77 | systemv (const char *const argv[]) |
111 | { |
78 | { |
112 | #ifdef _WIN32 |
79 | #ifdef _WIN32 |
113 | _spawnv (P_WAIT, argv [0], argv); |
80 | _spawnv (P_WAIT, argv [0], argv); |
114 | #else |
81 | #else |
115 | pid_t pid = dowait ? fork () : 0; |
82 | pid_t pid = fork (); |
116 | |
83 | |
117 | if (pid < 0) |
84 | if (pid < 0) |
118 | fatal ("fork failure"); |
85 | u_fatal ("fork failure"); |
119 | |
86 | |
120 | if (!pid) |
87 | if (!pid) |
121 | { |
88 | { |
122 | execv (argv [0], (void *)argv); |
89 | execv (argv [0], (void *)argv); |
123 | _exit (124); |
90 | _exit (124); |
… | |
… | |
136 | const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; |
103 | const char *argv[] = { getenv ("COMSPEC"), "/c", "rd", "/s", "/q", buf, 0 }; |
137 | sprintf (buf, "\"%s\"", path); |
104 | sprintf (buf, "\"%s\"", path); |
138 | #else |
105 | #else |
139 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
106 | const char *argv[] = { "/bin/rm", "-rf", path, 0 }; |
140 | #endif |
107 | #endif |
141 | systemv (argv, 1); |
108 | systemv (argv); |
142 | } |
109 | } |
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 | |
110 | |
176 | static char *pack_base, *pack_end; |
111 | static char *pack_base, *pack_end; |
|
|
112 | static struct u_pack_tail *pack_tail; |
177 | static struct hdr *pack_cur; |
113 | static struct u_pack_hdr *pack_cur; |
178 | static char *scratch; |
114 | static char *scratch; |
179 | static unsigned int scratch_len; |
115 | static unsigned int scratch_size; |
180 | |
116 | |
181 | #define PACK_NAME ((char *)(pack_cur + 1)) |
117 | #define PACK_NAME ((char *)(pack_cur + 1)) |
182 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
118 | #define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) |
183 | #define PACK_VALID pack_cur->type |
119 | #define PACK_VALID pack_cur->type |
184 | |
120 | |
185 | static void |
121 | static void |
186 | pack_next (void) |
122 | pack_next (void) |
187 | { |
123 | { |
188 | unsigned int d = u_32 (pack_cur->datalen); |
124 | unsigned int d = u_32 (pack_cur->datalen); |
189 | |
125 | |
190 | pack_cur = (struct hdr *)(PACK_DATA + d); |
126 | pack_cur = (struct u_pack_hdr *)(PACK_DATA + d); |
191 | } |
127 | } |
192 | |
128 | |
193 | static void |
129 | static void |
194 | pack_unmap (void) |
130 | pack_unmap (void) |
195 | { |
131 | { |
196 | if (!pack_base) |
132 | if (pack_base) |
197 | return; |
133 | { |
198 | |
|
|
199 | free (scratch); |
|
|
200 | |
|
|
201 | #ifdef _WIN32 |
|
|
202 | UnmapViewOfFile (pack_base); |
|
|
203 | #else |
|
|
204 | munmap (pack_base, pack_end - pack_base); |
134 | u_munmap (pack_base, pack_end - pack_base); |
205 | #endif |
135 | pack_base = 0; |
|
|
136 | } |
|
|
137 | |
|
|
138 | if (scratch) |
|
|
139 | { |
|
|
140 | u_free (scratch, scratch_size); |
|
|
141 | scratch = 0; |
|
|
142 | } |
206 | } |
143 | } |
207 | |
144 | |
208 | static int |
145 | static int |
209 | pack_map (void) |
146 | pack_map (void) |
210 | { |
147 | { |
|
|
148 | char *addr; |
|
|
149 | unsigned int size; |
|
|
150 | |
211 | #ifdef _WIN32 |
151 | #ifdef _WIN32 |
212 | BY_HANDLE_FILE_INFORMATION fi; |
152 | BY_HANDLE_FILE_INFORMATION fi; |
213 | |
153 | |
214 | if (!GetFileInformationByHandle (pack_handle, &fi)) |
154 | if (!GetFileInformationByHandle (pack_handle, &fi)) |
215 | return 0; |
155 | return 0; |
216 | |
156 | |
217 | if (fi.nFileSizeHigh) // too big |
157 | size = fi.nFileSizeLow; |
|
|
158 | #else |
|
|
159 | size = lseek (pack_handle, 0, SEEK_END); |
|
|
160 | #endif |
|
|
161 | |
|
|
162 | addr = u_mmap (pack_handle, size); |
|
|
163 | if (!addr) |
218 | return 0; |
164 | return 0; |
219 | |
165 | |
220 | HANDLE handle = CreateFileMapping (pack_handle, 0, PAGE_READONLY, 0, fi.nFileSizeLow, NULL); |
166 | /*pack_unmap ();*/ |
221 | |
167 | |
222 | if (!handle) |
168 | pack_base = addr; |
|
|
169 | pack_end = pack_base + size; |
|
|
170 | |
|
|
171 | pack_tail = (void *)(pack_end - sizeof (*pack_tail)); |
|
|
172 | |
|
|
173 | if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
223 | return 0; |
174 | return 0; |
224 | |
175 | |
225 | pack_unmap (); |
176 | pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size)); |
226 | |
177 | |
227 | pack_base = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, fi.nFileSizeLow); |
178 | if (pack_cur->type != T_META) |
228 | |
|
|
229 | CloseHandle (handle); |
|
|
230 | |
|
|
231 | pack_end = pack_base + fi.nFileSizeLow; |
|
|
232 | #else |
|
|
233 | off_t len = lseek (pack_handle - 1, 0, SEEK_END); |
|
|
234 | |
|
|
235 | pack_unmap (); |
|
|
236 | |
|
|
237 | pack_base = mmap (0, len, PROT_READ, MAP_SHARED, pack_handle - 1, 0); |
|
|
238 | if (pack_base == (void *)-1) |
|
|
239 | pack_base = 0; |
|
|
240 | |
|
|
241 | pack_end = pack_base + len; |
|
|
242 | #endif |
|
|
243 | |
|
|
244 | if (!pack_base) |
|
|
245 | return 0; |
179 | return 0; |
246 | |
180 | |
247 | struct tail *tail; |
181 | scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed)); |
248 | |
182 | if (!scratch) |
249 | tail = (void *)(pack_end - sizeof (*tail)); |
|
|
250 | |
|
|
251 | if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) |
|
|
252 | return 0; |
183 | return 0; |
253 | |
184 | |
254 | pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); |
|
|
255 | |
|
|
256 | free (scratch); |
|
|
257 | scratch = malloc (scratch_len = u_32 (tail->max_filesize)); |
|
|
258 | |
|
|
259 | return 1; |
185 | return 1; |
260 | } |
186 | } |
261 | |
187 | |
262 | static void |
188 | static void |
263 | exe_info (void) |
189 | add_arg (char *arg, unsigned int len) |
264 | { |
190 | { |
265 | if (!pack_map ()) |
191 | char *addr = u_dynbuf_append (&exe_args, arg, len); |
266 | fatal ("unable to locate packfile in executable - executable corrupted?"); |
192 | 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 | } |
193 | } |
273 | |
194 | |
274 | static void |
195 | static void |
275 | load (void) |
196 | load (void) |
276 | { |
197 | { |
277 | u_mkdir (datadir); |
198 | strcpy (exe_id , PACK_NAME); |
278 | |
199 | strcpy (exe_ver, PACK_DATA); |
279 | strcpy (exe_dir, datadir); |
200 | u_set_exe_info (); |
280 | u_append (exe_dir, exe_id); |
|
|
281 | u_mkdir (exe_dir); |
|
|
282 | |
201 | |
283 | if (u_chdir (exe_dir)) |
202 | if (u_chdir (exe_dir)) |
284 | fatal ("unable to change to application data directory"); |
203 | u_fatal ("unable to change to application data directory"); |
285 | |
204 | |
286 | u_handle h = u_open ("override"); |
205 | u_handle h = u_open ("override"); |
287 | if (h) |
206 | if (u_valid (h)) |
288 | { |
207 | { |
289 | u_handle oh = pack_handle; |
208 | u_handle oh = pack_handle; |
290 | |
209 | |
|
|
210 | pack_unmap (); |
291 | pack_handle = h; |
211 | pack_handle = h; |
|
|
212 | |
292 | if (!pack_map ()) |
213 | if (pack_map () |
|
|
214 | && strcmp (exe_id , PACK_NAME) == 0 |
|
|
215 | && strcmp (exe_ver, PACK_DATA) <= 0) |
|
|
216 | u_setenv ("URLADER_OVERRIDE", "override"); |
|
|
217 | else |
293 | { |
218 | { |
|
|
219 | pack_unmap (); |
294 | pack_handle = oh; |
220 | pack_handle = oh; |
295 | oh = h; |
221 | oh = h; |
|
|
222 | pack_map (); |
296 | } |
223 | } |
297 | |
224 | |
298 | u_close (oh); |
225 | u_close (oh); |
299 | } |
226 | } |
300 | |
227 | |
301 | if (pack_cur->type != T_META) |
|
|
302 | fatal ("unable to locate override metadata"); |
|
|
303 | |
|
|
304 | strcpy (exe_ver, PACK_DATA); |
228 | strcpy (exe_ver, PACK_DATA); |
|
|
229 | u_set_exe_info (); |
305 | pack_next (); |
230 | pack_next (); |
306 | |
231 | |
|
|
232 | for (;;) |
|
|
233 | { |
|
|
234 | if (pack_cur->type == T_ENV) |
|
|
235 | u_setenv (PACK_NAME, PACK_DATA); |
307 | while (pack_cur->type == T_ARG) |
236 | else if (pack_cur->type == T_ARG) |
308 | { |
237 | add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1); |
309 | exe_argv [exe_argc++] = strdup (PACK_NAME); |
238 | else |
|
|
239 | break; |
|
|
240 | |
310 | pack_next (); |
241 | pack_next (); |
311 | } |
242 | } |
|
|
243 | |
|
|
244 | done_env_arg: |
312 | |
245 | |
313 | while (pack_cur->type == T_ENV) |
246 | strcat (strcpy (tmppath, execdir), ".lck"); |
314 | { |
247 | lock_handle = u_lock (tmppath, 0, 1); |
315 | u_setenv (PACK_NAME, PACK_DATA); |
248 | if (!lock_handle) |
316 | pack_next (); |
249 | 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 | |
250 | |
323 | if (access (execdir, F_OK)) |
251 | if (access (execdir, F_OK)) |
324 | { |
252 | { |
325 | // does not exist yet, so unpack and move |
253 | // does not exist yet, so unpack and move |
326 | tmpdir (exe_dir); |
254 | tmpdir (exe_dir); |
327 | |
255 | |
328 | if (u_chdir (tmppath)) |
256 | if (u_chdir (tmppath)) |
329 | fatal ("unable to change to new instance directory"); |
257 | u_fatal ("unable to change to new instance directory"); |
330 | |
258 | |
331 | for (;;) |
259 | for (;;) |
332 | { |
260 | { |
333 | switch (pack_cur->type) |
261 | switch (pack_cur->type) |
334 | { |
262 | { |
… | |
… | |
341 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); |
269 | u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); |
342 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
270 | unsigned int dlen, len = u_32 (pack_cur->datalen); |
343 | char *data = PACK_DATA; |
271 | char *data = PACK_DATA; |
344 | |
272 | |
345 | if (pack_cur->flags & F_LZF) |
273 | if (pack_cur->flags & F_LZF) |
346 | if (dlen = lzf_decompress (data, len, scratch, scratch_len)) |
274 | if (dlen = lzf_decompress (data, len, scratch, scratch_size)) |
347 | { |
275 | { |
348 | data = scratch; |
276 | data = scratch; |
349 | len = dlen; |
277 | len = dlen; |
350 | } |
278 | } |
351 | else |
279 | else |
352 | fatal ("unable to uncompress file data - pack corrupted?"); |
280 | u_fatal ("unable to uncompress file data - pack corrupted?"); |
353 | |
281 | |
354 | if (!h) |
282 | if (!u_valid (h)) |
355 | fatal ("unable to unpack file from packfile - disk full?"); |
283 | u_fatal ("unable to unpack file from packfile - disk full?"); |
356 | |
284 | |
357 | if (u_write (h, data, len) != len) |
285 | if (u_write (h, data, len) != len) |
358 | fatal ("unable to unpack file from packfile - disk full?"); |
286 | u_fatal ("unable to unpack file from packfile - disk full?"); |
359 | |
287 | |
|
|
288 | u_fsync (h); |
360 | u_close (h); |
289 | u_close (h); |
361 | } |
290 | } |
362 | break; |
291 | break; |
363 | |
292 | |
364 | case T_NULL: |
293 | case T_NULL: |
… | |
… | |
368 | pack_next (); |
297 | pack_next (); |
369 | } |
298 | } |
370 | |
299 | |
371 | done: |
300 | done: |
372 | if (u_chdir (datadir)) |
301 | if (u_chdir (datadir)) |
373 | fatal ("unable to change to data directory"); |
302 | u_fatal ("unable to change to data directory"); |
|
|
303 | |
|
|
304 | u_sync (); |
374 | |
305 | |
375 | if (u_rename (tmppath, execdir)) |
306 | if (u_rename (tmppath, execdir)) |
376 | deltree (tmppath); // if move fails, delete new, assume other process created it independently |
307 | deltree (tmppath); // if move fails, delete new, assume other process created it independently |
377 | } |
308 | } |
378 | |
309 | |
379 | pack_unmap (); |
310 | pack_unmap (); |
380 | u_close (pack_handle); |
311 | u_close (pack_handle); |
381 | |
312 | |
382 | if (u_chdir (execdir)) |
313 | if (u_chdir (execdir)) |
383 | fatal ("unable to change to application instance directory"); |
314 | u_fatal ("unable to change to application instance directory"); |
384 | |
315 | |
385 | u_setenv ("URLADER_VERSION", URLADER_VERSION); |
316 | 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 | |
317 | |
|
|
318 | #if 0 |
392 | // yes, this is overkill |
319 | // yes, this is overkill |
393 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
320 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
394 | u_setenv ("LIBPATH" , execdir); // aix |
321 | u_setenv ("LIBPATH" , execdir); // aix |
395 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
322 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
396 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
323 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
397 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
324 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
398 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
325 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
399 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
326 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
400 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
327 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
|
|
328 | #endif |
401 | } |
329 | } |
402 | |
330 | |
403 | static void |
331 | static void |
404 | execute (void) |
332 | execute (void) |
405 | { |
333 | { |
406 | exe_argv [exe_argc] = 0; |
334 | char *null = 0; |
407 | systemv (exe_argv, 0); |
335 | u_dynbuf_append (&exe_argv, &null, sizeof (null)); |
|
|
336 | systemv ((const char *const *)exe_argv.addr); |
|
|
337 | } |
|
|
338 | |
|
|
339 | // this argc/argv is without argv [0] |
|
|
340 | static void |
|
|
341 | doit (int argc, char *argv[]) |
|
|
342 | { |
|
|
343 | int i; |
|
|
344 | |
|
|
345 | u_setenv ("URLADER_CURRDIR", currdir); |
|
|
346 | |
|
|
347 | u_set_datadir (); |
|
|
348 | u_mkdir (datadir); |
|
|
349 | |
|
|
350 | if (!pack_map ()) |
|
|
351 | u_fatal ("unable to map pack file - executable corrupted?"); |
|
|
352 | |
|
|
353 | load (); |
|
|
354 | |
|
|
355 | while (argc--) |
|
|
356 | { |
|
|
357 | add_arg (*argv, strlen (*argv) + 1); |
|
|
358 | ++argv; |
|
|
359 | } |
|
|
360 | |
|
|
361 | execute (); |
408 | } |
362 | } |
409 | |
363 | |
410 | #ifdef _WIN32 |
364 | #ifdef _WIN32 |
411 | |
365 | |
412 | int APIENTRY |
366 | int APIENTRY |
413 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
367 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
414 | { |
368 | { |
415 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
369 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
416 | fatal ("unable to find executable pack"); |
370 | u_fatal ("unable to find executable pack"); |
417 | |
371 | |
|
|
372 | u_setenv ("URLADER_EXEPATH", tmppath); |
|
|
373 | |
418 | if (!(pack_handle = u_open (tmppath))) |
374 | pack_handle = u_open (tmppath); |
|
|
375 | if (!u_valid (pack_handle)) |
419 | fatal ("unable to open executable pack"); |
376 | u_fatal ("unable to open executable pack"); |
420 | |
|
|
421 | exe_info (); |
|
|
422 | |
377 | |
423 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
378 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
424 | strcpy (currdir, "."); |
379 | strcpy (currdir, "."); |
425 | |
380 | |
426 | if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) |
381 | 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 | |
382 | |
435 | return 0; |
383 | return 0; |
436 | } |
384 | } |
437 | |
385 | |
438 | #else |
386 | #else |
439 | |
387 | |
440 | int |
388 | int |
441 | main (int argc, char *argv[]) |
389 | main (int argc, char *argv[]) |
442 | { |
390 | { |
443 | char *home = getenv ("HOME"); |
|
|
444 | |
|
|
445 | // we assume fd 0 and 2 are "normal" |
|
|
446 | |
|
|
447 | if (!(pack_handle = u_open (argv [0]))) |
|
|
448 | 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 | |
|
|
462 | if (!getcwd (currdir, sizeof (currdir))) |
391 | if (!getcwd (currdir, sizeof (currdir))) |
463 | strcpy (currdir, "."); |
392 | strcpy (currdir, "."); |
464 | |
393 | |
465 | u_mkdir (home); |
394 | { |
466 | sprintf (datadir, "%s/.%s", home, URLADER); |
395 | const char *exe_path = 0; |
467 | u_mkdir (datadir); |
396 | |
|
|
397 | if (strchr (argv [0], '/')) |
|
|
398 | exe_path = argv [0]; |
|
|
399 | else |
|
|
400 | { |
|
|
401 | const char *p, *path = getenv ("PATH"); |
|
|
402 | |
|
|
403 | if (!path) |
|
|
404 | u_fatal ("unable to find executable, try running with full path."); |
|
|
405 | |
|
|
406 | for (p = path; ; ) |
|
|
407 | { |
|
|
408 | const char *e = p; |
|
|
409 | int l; |
|
|
410 | |
|
|
411 | while (*e && *e != ':') |
|
|
412 | ++e; |
|
|
413 | |
|
|
414 | l = e - p; |
|
|
415 | memcpy (tmppath, p, l); |
|
|
416 | |
|
|
417 | if (!l) |
|
|
418 | tmppath [l++] = '.'; |
|
|
419 | |
|
|
420 | tmppath [l++] = '/'; |
|
|
421 | |
|
|
422 | strcpy (tmppath + l, argv [0]); |
|
|
423 | |
|
|
424 | if (!access (tmppath, X_OK)) |
|
|
425 | break; |
|
|
426 | |
|
|
427 | p = e; |
|
|
428 | if (!*p) |
|
|
429 | u_fatal ("unable to find executable, try running with full path."); |
|
|
430 | |
|
|
431 | ++p; |
|
|
432 | } |
|
|
433 | |
|
|
434 | exe_path = tmppath; |
|
|
435 | } |
|
|
436 | |
|
|
437 | pack_handle = u_open (exe_path); |
|
|
438 | if (!u_valid (pack_handle)) |
|
|
439 | u_fatal ("unable to open executable for reading - permissions problem?"); |
|
|
440 | |
|
|
441 | u_setenv ("URLADER_EXEPATH", exe_path); |
|
|
442 | } |
|
|
443 | |
|
|
444 | #if 0 |
|
|
445 | /* intersperse hostname, for whatever reason */ |
468 | |
446 | |
469 | if (gethostname (tmppath, sizeof (tmppath))) |
447 | if (gethostname (tmppath, sizeof (tmppath))) |
470 | strcpy (tmppath, "default"); |
448 | strcpy (tmppath, "default"); |
471 | |
449 | |
472 | u_append (datadir, tmppath); |
450 | u_append (datadir, tmppath); |
|
|
451 | #endif |
473 | |
452 | |
474 | load (); |
453 | doit (argc - 1, argv + 1); |
475 | execute (); |
|
|
476 | |
454 | |
477 | return 0; |
455 | return 0; |
478 | } |
456 | } |
479 | |
457 | |
480 | #endif |
458 | #endif |