ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Urlader/urlader.c
(Generate patch)

Comparing Urlader/urlader.c (file contents):
Revision 1.7 by root, Fri Dec 30 14:51:45 2011 UTC vs.
Revision 1.15 by root, Mon Jun 10 06:46:32 2013 UTC

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" // a decimal number, not a version string 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 */
5 36
6#define MAX_ARGC 32 37#include "urlib.h"
7#define MAX_ARGS 256 38#include "urlib.c"
8 39
9#include <stdio.h> 40#include <stdio.h>
10#include <stdlib.h> 41#include <stdlib.h>
11#include <unistd.h> 42#include <unistd.h>
12#include <errno.h> 43#include <errno.h>
13#include <string.h> 44#include <string.h>
14#include <time.h> 45#include <time.h>
15#include <fcntl.h>
16 46
17#include "liblzf/lzf_d.c" 47#include "liblzf/lzf_d.c"
18
19#define TAIL_MAGIC "SCHMORPPACK0"
20
21#ifdef _WIN32
22
23 #include <windows.h>
24 //#include <winbase.h>
25 #include <shlobj.h>
26 #include <shlwapi.h>
27 #include <wininet.h>
28
29 static DWORD dword;
30
31 #define u_handle HANDLE
32 #define u_invalid_handle 0
33 #define u_valid(handle) (!!handle)
34
35 #define u_setenv(name,value) SetEnvironmentVariable (name, value)
36 #define u_mkdir(path) !CreateDirectory (path, NULL)
37 #define u_chdir(path) !SetCurrentDirectory (path)
38 #define u_rename(fr,to) !MoveFile (fr, to)
39 #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
40 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
41 #define u_creat(path,exec) CreateFile (path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL)
42 #define u_close(handle) CloseHandle (handle)
43 #define u_append(path,add) PathAppend (path, add)
44 #define u_write(handle,data,len) (WriteFile (handle, data, len, &dword, 0) ? dword : -1)
45
46 #define u_fsync(handle) FlushFileBuffers (handle)
47 #define u_sync()
48
49 #define u_lockfile(path) CreateFile (path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)
50 #define u_cloexec(handle)
51
52#else
53
54 #define _GNU_SOURCE 1
55 #define _BSD_SOURCE 1
56 // the above increases our chances of getting MAP_ANONYMOUS
57
58 #include <sys/mman.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <unistd.h>
62 #include <pwd.h>
63
64 #if defined (MAP_ANON) && !defined (MAP_ANONYMOUS)
65 #define MAP_ANONYMOUS MAP_ANON
66 #endif
67
68 #ifdef PATH_MAX
69 #define MAX_PATH (PATH_MAX < 4096 ? 4096 : PATH_MAX)
70 #else
71 #define MAX_PATH 4096
72 #endif
73
74 #define u_handle int
75 #define u_invalid_handle -1
76 #define u_valid(fd) ((fd) >= 0)
77
78 #define u_setenv(name,value) setenv (name, value, 1)
79 #define u_mkdir(path) mkdir (path, 0777)
80 #define u_chdir(path) chdir (path)
81 #define u_rename(fr,to) rename (fr, to)
82 #define u_open(path) open (path, O_RDONLY)
83 #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666)
84 #define u_close(handle) close (handle)
85 #define u_append(path,add) strcat (strcat (path, "/"), add)
86 #define u_write(handle,data,len) write (handle, data, len)
87
88 // on a mostly idle system, a sync at the end is certainly faster, hope for the best
89 #define u_fsync(handle)
90 #define u_sync() sync ()
91
92 #define u_lockfile(path) open (path, O_RDWR | O_CREAT, 0666)
93 #define u_cloexec(handle) fcntl (handle, F_SETFD, FD_CLOEXEC)
94
95#endif
96
97#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
98#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
99 48
100/* some simple? dynamic memory management for paths might save ltos of ram */ 49/* some simple? dynamic memory management for paths might save ltos of ram */
101static u_handle pack_handle; 50static u_handle pack_handle;
102static u_handle lock_handle; 51static u_handle lock_handle;
103static char tmppath[MAX_PATH]; 52static char tmppath[MAX_PATH];
104static char currdir[MAX_PATH];
105static char datadir[MAX_PATH]; // %AppData%/urlader
106static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
107static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
108static char exe_id[MAX_PATH];
109static char exe_ver[MAX_PATH];
110 53
111static int exe_argc; 54static int exe_argc;
112static const char *exe_argv[MAX_ARGC]; 55static u_dynbuf exe_argv;
113static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 56static u_dynbuf exe_args; /* actual arguments strings copied here */
114static unsigned int exe_argo;
115
116static void
117fatal (const char *msg)
118{
119#ifdef _WIN32
120 MessageBox (0, msg, URLADER, 0);
121#else
122 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
123 write (2, msg, strlen (msg));
124 write (2, "\n", 1);
125#endif
126
127 _exit (1);
128}
129
130static void *
131u_malloc (unsigned int size)
132{
133 void *addr;
134
135#ifdef _WIN32
136 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
137
138 if (!handle)
139 return 0;
140
141 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
142
143 CloseHandle (handle);
144#elif defined (MAP_ANONYMOUS)
145 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
146
147 if (addr == (void *)-1)
148 addr = 0;
149#else
150 addr = malloc (size);
151#endif
152
153 return addr;
154}
155
156static void
157u_free (void *addr, unsigned int size)
158{
159#ifdef _WIN32
160 UnmapViewOfFile (addr);
161#elif defined (MAP_ANONYMOUS)
162 munmap (addr, size);
163#else
164 free (addr);
165#endif
166}
167
168static void *
169u_mmap (u_handle h, unsigned int size)
170{
171 void *addr;
172
173#ifdef _WIN32
174 HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL);
175
176 if (!handle)
177 return 0;
178
179 addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size);
180
181 CloseHandle (handle);
182#else
183 addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0);
184
185 if (addr == (void *)-1)
186 addr = 0;
187#endif
188
189 return addr;
190}
191
192static void
193u_munmap (void *addr, unsigned int len)
194{
195#ifdef _WIN32
196 UnmapViewOfFile (addr);
197#else
198 munmap (addr, len);
199#endif
200}
201 57
202static void 58static void
203tmpdir (const char *dir) 59tmpdir (const char *dir)
204{ 60{
205 static int cnt; 61 static int cnt;
215 if (!u_mkdir (tmppath)) 71 if (!u_mkdir (tmppath))
216 return; 72 return;
217 } 73 }
218} 74}
219 75
220static u_handle
221u_lock (const char *path, int excl, int dowait)
222{
223 u_handle h;
224
225 h = u_lockfile (path);
226 if (!u_valid (h))
227 return h;
228
229 u_cloexec (h);
230
231 for (;;)
232 {
233 int success;
234
235 // acquire the lock
236#ifdef _WIN32
237 OVERLAPPED ov = { 0 };
238
239 success = LockFileEx (h,
240 (excl ? LOCKFILE_EXCLUSIVE_LOCK : 0)
241 | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
242 0,
243 1, 0,
244 &ov);
245#else
246 struct flock lck = { 0 };
247
248 lck.l_type = excl ? F_WRLCK : F_RDLCK;
249 lck.l_whence = SEEK_SET;
250 lck.l_len = 1;
251
252 success = !fcntl (h, dowait ? F_SETLKW : F_SETLK, &lck);
253#endif
254
255 if (!success)
256 break;
257
258 // we have the lock, now verify that the lockfile still exists
259
260#ifdef _WIN32
261 // apparently, we have to open the file to get its info :(
262 BY_HANDLE_FILE_INFORMATION s1, s2;
263 u_handle h2 = u_lockfile (path);
264 if (!u_valid (h))
265 break;
266
267 success = GetFileInformationByHandle (h, &s1)
268 && GetFileInformationByHandle (h2, &s2);
269
270 u_close (h2);
271
272 if (!success)
273 break;
274
275 success = s1.dwVolumeSerialNumber == s2.dwVolumeSerialNumber
276 && s1.nFileIndexHigh == s2.nFileIndexHigh
277 && s1.nFileIndexLow == s2.nFileIndexLow;
278#else
279 struct stat s1, s2;
280
281 if (fstat (h, &s1) || stat (path, &s2))
282 break;
283
284 success = s1.st_dev == s2.st_dev
285 && s1.st_ino == s2.st_ino;
286#endif
287
288 if (success)
289 return h; // lock successfully acquired
290
291 // files differ, close and retry - should be very rare
292 u_close (h);
293 }
294
295 // failure
296 u_close (h);
297 return u_invalid_handle;
298}
299
300static void 76static void
301systemv (const char *const argv[]) 77systemv (const char *const argv[])
302{ 78{
303#ifdef _WIN32 79#ifdef _WIN32
304 _spawnv (P_WAIT, argv [0], argv); 80 _spawnv (P_WAIT, argv [0], argv);
305#else 81#else
306 pid_t pid = fork (); 82 pid_t pid = fork ();
307 83
308 if (pid < 0) 84 if (pid < 0)
309 fatal ("fork failure"); 85 u_fatal ("fork failure");
310 86
311 if (!pid) 87 if (!pid)
312 { 88 {
313 execv (argv [0], (void *)argv); 89 execv (argv [0], (void *)argv);
314 _exit (124); 90 _exit (124);
330 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 106 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
331 #endif 107 #endif
332 systemv (argv); 108 systemv (argv);
333} 109}
334 110
335enum
336{
337 T_NULL, // 5
338 T_META, // 1 : exe_id, exe_ver
339 T_ENV, // 2 : name, value
340 T_ARG, // 3 : arg
341 T_DIR, // 4+: path
342 T_FILE, // 4+: path, data
343 T_NUM
344};
345
346enum
347{
348 F_EXEC = 1,
349 F_LZF = 2,
350 F_NULL = 0
351};
352
353struct hdr
354{
355 unsigned char type;
356 unsigned char flags;
357 unsigned char namelen[2];
358 unsigned char datalen[4];
359};
360
361struct tail {
362 unsigned char max_filesize[4];
363 unsigned char size[4];
364 char magic[12];
365};
366
367static char *pack_base, *pack_end; 111static char *pack_base, *pack_end;
112static struct u_pack_tail *pack_tail;
368static struct hdr *pack_cur; 113static struct u_pack_hdr *pack_cur;
369static char *scratch; 114static char *scratch;
370static unsigned int scratch_size; 115static unsigned int scratch_size;
371 116
372#define PACK_NAME ((char *)(pack_cur + 1)) 117#define PACK_NAME ((char *)(pack_cur + 1))
373#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 118#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
376static void 121static void
377pack_next (void) 122pack_next (void)
378{ 123{
379 unsigned int d = u_32 (pack_cur->datalen); 124 unsigned int d = u_32 (pack_cur->datalen);
380 125
381 pack_cur = (struct hdr *)(PACK_DATA + d); 126 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
382} 127}
383 128
384static void 129static void
385pack_unmap (void) 130pack_unmap (void)
386{ 131{
416 161
417 addr = u_mmap (pack_handle, size); 162 addr = u_mmap (pack_handle, size);
418 if (!addr) 163 if (!addr)
419 return 0; 164 return 0;
420 165
421 pack_unmap (); 166 /*pack_unmap ();*/
422 167
423 pack_base = addr; 168 pack_base = addr;
424 pack_end = pack_base + size; 169 pack_end = pack_base + size;
425 170
426 struct tail *tail;
427
428 tail = (void *)(pack_end - sizeof (*tail)); 171 pack_tail = (void *)(pack_end - sizeof (*pack_tail));
429 172
430 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 173 if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
431 return 0; 174 return 0;
432 175
433 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 176 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size));
434 177
178 if (pack_cur->type != T_META)
179 return 0;
180
435 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 181 scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed));
436 if (!scratch) 182 if (!scratch)
437 return 0; 183 return 0;
438 184
439 return 1; 185 return 1;
440} 186}
441 187
442static void 188static void
443exe_info (void) 189add_arg (char *arg, unsigned int len)
444{ 190{
445 if (!pack_map ()) 191 char *addr = u_dynbuf_append (&exe_args, arg, len);
446 fatal ("unable to locate packfile in executable - executable corrupted?"); 192 u_dynbuf_append (&exe_argv, &addr, sizeof (addr));
447
448 if (pack_cur->type != T_META)
449 fatal ("unable to locate executable metadata - executable corrupted?");
450
451 strcpy (exe_id, PACK_NAME);
452} 193}
453 194
454static void 195static void
455load (void) 196load (void)
456{ 197{
457 u_mkdir (datadir); 198 strcpy (exe_id , PACK_NAME);
458 199 strcpy (exe_ver, PACK_DATA);
459 strcpy (exe_dir, datadir); 200 u_set_exe_info ();
460 u_append (exe_dir, exe_id);
461 u_mkdir (exe_dir);
462 201
463 if (u_chdir (exe_dir)) 202 if (u_chdir (exe_dir))
464 fatal ("unable to change to application data directory"); 203 u_fatal ("unable to change to application data directory");
465 204
466 u_handle h = u_open ("override"); 205 u_handle h = u_open ("override");
467 if (u_valid (h)) 206 if (u_valid (h))
468 { 207 {
469 u_handle oh = pack_handle; 208 u_handle oh = pack_handle;
470 209
210 pack_unmap ();
471 pack_handle = h; 211 pack_handle = h;
212
472 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
473 { 218 {
219 pack_unmap ();
474 pack_handle = oh; 220 pack_handle = oh;
475 oh = h; 221 oh = h;
222 pack_map ();
476 } 223 }
477 224
478 u_close (oh); 225 u_close (oh);
479 } 226 }
480 227
481 if (pack_cur->type != T_META)
482 fatal ("unable to locate override metadata");
483
484 strcpy (exe_ver, PACK_DATA); 228 strcpy (exe_ver, PACK_DATA);
229 u_set_exe_info ();
485 pack_next (); 230 pack_next ();
486 231
487 for (;;) 232 for (;;)
488 { 233 {
489 if (pack_cur->type == T_ENV) 234 if (pack_cur->type == T_ENV)
490 u_setenv (PACK_NAME, PACK_DATA); 235 u_setenv (PACK_NAME, PACK_DATA);
491 else if (pack_cur->type == T_ARG) 236 else if (pack_cur->type == T_ARG)
492 { 237 add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1);
493 int len = u_16 (pack_cur->namelen) + 1;
494 exe_argv [exe_argc++] = exe_args + exe_argo;
495 memcpy (exe_args + exe_argo, PACK_NAME, len);
496 exe_argo += len;
497 }
498 else 238 else
499 break; 239 break;
500 240
501 pack_next (); 241 pack_next ();
502 } 242 }
503 243
504done_env_arg: 244done_env_arg:
505 strcpy (execdir, exe_dir);
506 u_append (execdir, "i-");
507 strcat (execdir, exe_ver);
508 245
509 strcat (strcpy (tmppath, execdir), ".lck"); 246 strcat (strcpy (tmppath, execdir), ".lck");
510 lock_handle = u_lock (tmppath, 0, 1); 247 lock_handle = u_lock (tmppath, 0, 1);
511 if (!lock_handle) 248 if (!lock_handle)
512 fatal ("unable to lock application instance"); 249 u_fatal ("unable to lock application instance");
513 250
514 if (access (execdir, F_OK)) 251 if (access (execdir, F_OK))
515 { 252 {
516 // does not exist yet, so unpack and move 253 // does not exist yet, so unpack and move
517 tmpdir (exe_dir); 254 tmpdir (exe_dir);
518 255
519 if (u_chdir (tmppath)) 256 if (u_chdir (tmppath))
520 fatal ("unable to change to new instance directory"); 257 u_fatal ("unable to change to new instance directory");
521 258
522 for (;;) 259 for (;;)
523 { 260 {
524 switch (pack_cur->type) 261 switch (pack_cur->type)
525 { 262 {
538 { 275 {
539 data = scratch; 276 data = scratch;
540 len = dlen; 277 len = dlen;
541 } 278 }
542 else 279 else
543 fatal ("unable to uncompress file data - pack corrupted?"); 280 u_fatal ("unable to uncompress file data - pack corrupted?");
544 281
545 if (!u_valid (h)) 282 if (!u_valid (h))
546 fatal ("unable to unpack file from packfile - disk full?"); 283 u_fatal ("unable to unpack file from packfile - disk full?");
547 284
548 if (u_write (h, data, len) != len) 285 if (u_write (h, data, len) != len)
549 fatal ("unable to unpack file from packfile - disk full?"); 286 u_fatal ("unable to unpack file from packfile - disk full?");
550 287
551 u_fsync (h); 288 u_fsync (h);
552 u_close (h); 289 u_close (h);
553 } 290 }
554 break; 291 break;
560 pack_next (); 297 pack_next ();
561 } 298 }
562 299
563done: 300done:
564 if (u_chdir (datadir)) 301 if (u_chdir (datadir))
565 fatal ("unable to change to data directory"); 302 u_fatal ("unable to change to data directory");
566 303
567 u_sync (); 304 u_sync ();
568 305
569 if (u_rename (tmppath, execdir)) 306 if (u_rename (tmppath, execdir))
570 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
572 309
573 pack_unmap (); 310 pack_unmap ();
574 u_close (pack_handle); 311 u_close (pack_handle);
575 312
576 if (u_chdir (execdir)) 313 if (u_chdir (execdir))
577 fatal ("unable to change to application instance directory"); 314 u_fatal ("unable to change to application instance directory");
578 315
579 u_setenv ("URLADER_VERSION", URLADER_VERSION); 316 u_setenv ("URLADER_VERSION", URLADER_VERSION);
580 u_setenv ("URLADER_DATADIR", datadir);
581 u_setenv ("URLADER_EXECDIR", execdir);
582 u_setenv ("URLADER_EXE_ID" , exe_id);
583 u_setenv ("URLADER_EXE_DIR", exe_dir);
584 u_setenv ("URLADER_EXE_VER", exe_ver);
585 317
586#if 0 318#if 0
587 // yes, this is overkill 319 // yes, this is overkill
588 u_setenv ("SHLIB_PATH" , execdir); // hpux 320 u_setenv ("SHLIB_PATH" , execdir); // hpux
589 u_setenv ("LIBPATH" , execdir); // aix 321 u_setenv ("LIBPATH" , execdir); // aix
597} 329}
598 330
599static void 331static void
600execute (void) 332execute (void)
601{ 333{
602 exe_argv [exe_argc] = 0; 334 char *null = 0;
603 systemv (exe_argv); 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]
340static void
341doit (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 ();
604} 362}
605 363
606#ifdef _WIN32 364#ifdef _WIN32
607 365
608int APIENTRY 366int APIENTRY
609WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 367WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
610{ 368{
611 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 369 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
612 fatal ("unable to find executable pack"); 370 u_fatal ("unable to find executable pack");
371
372 u_setenv ("URLADER_EXEPATH", tmppath);
613 373
614 pack_handle = u_open (tmppath); 374 pack_handle = u_open (tmppath);
615 if (!u_valid (pack_handle)) 375 if (!u_valid (pack_handle))
616 fatal ("unable to open executable pack"); 376 u_fatal ("unable to open executable pack");
617
618 exe_info ();
619 377
620 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 378 if (!GetCurrentDirectory (sizeof (currdir), currdir))
621 strcpy (currdir, "."); 379 strcpy (currdir, ".");
622 380
623 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 381 doit (1, &argv);
624 fatal ("unable to find application data directory");
625
626 u_mkdir (datadir);
627 u_append (datadir, URLADER);
628
629 load ();
630 execute ();
631 382
632 return 0; 383 return 0;
633} 384}
634 385
635#else 386#else
636 387
637int 388int
638main (int argc, char *argv[]) 389main (int argc, char *argv[])
639{ 390{
640 char *home = getenv ("HOME");
641
642 pack_handle = u_open (argv [0]);
643 if (!u_valid (pack_handle))
644 fatal ("unable to open executable pack");
645
646 exe_info ();
647
648 if (!home)
649 {
650 struct passwd *pw;
651
652 if ((pw = getpwuid (getuid ())))
653 home = pw->pw_dir;
654 else
655 home = "/tmp";
656 }
657
658 if (!getcwd (currdir, sizeof (currdir))) 391 if (!getcwd (currdir, sizeof (currdir)))
659 strcpy (currdir, "."); 392 strcpy (currdir, ".");
660 393
661 u_mkdir (home); 394 {
662 //strcat (strcat (strcpy (datadir, home), "/."), URLADER); 395 const char *exe_path = 0;
663 sprintf (datadir, "%s/.%s", home, URLADER); 396
664 u_mkdir (datadir); 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 }
665 443
666#if 0 444#if 0
445 /* intersperse hostname, for whatever reason */
446
667 if (gethostname (tmppath, sizeof (tmppath))) 447 if (gethostname (tmppath, sizeof (tmppath)))
668 strcpy (tmppath, "default"); 448 strcpy (tmppath, "default");
669 449
670 u_append (datadir, tmppath); 450 u_append (datadir, tmppath);
671#endif 451#endif
672 452
673 load (); 453 doit (argc - 1, argv + 1);
674 execute ();
675 454
676 return 0; 455 return 0;
677} 456}
678 457
679#endif 458#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines