1 | #ifndef URLADER |
1 | #ifndef URLADER |
2 | # define URLADER "urlader" |
2 | # define URLADER "urlader" |
3 | #endif |
3 | #endif |
4 | #define URLADER_VERSION "1.0" |
4 | #define URLADER_VERSION "1.0" // a decimal number, not a version string |
5 | |
5 | |
6 | #include <stdio.h> |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <unistd.h> |
8 | #include <unistd.h> |
9 | #include <errno.h> |
9 | #include <errno.h> |
… | |
… | |
24 | #include <wininet.h> |
24 | #include <wininet.h> |
25 | |
25 | |
26 | static DWORD dword; |
26 | static DWORD dword; |
27 | |
27 | |
28 | #define u_handle HANDLE |
28 | #define u_handle HANDLE |
|
|
29 | #define u_valid(handle) (!!handle) |
|
|
30 | |
29 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
31 | #define u_setenv(name,value) SetEnvironmentVariable (name, value) |
30 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
32 | #define u_mkdir(path) !CreateDirectory (path, NULL) |
31 | #define u_chdir(path) !SetCurrentDirectory (path) |
33 | #define u_chdir(path) !SetCurrentDirectory (path) |
32 | #define u_rename(fr,to) !MoveFile (fr, to) |
34 | #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) |
35 | #define u_open(path) CreateFile (path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL) |
… | |
… | |
50 | #else |
52 | #else |
51 | #define MAX_PATH 4096 |
53 | #define MAX_PATH 4096 |
52 | #endif |
54 | #endif |
53 | |
55 | |
54 | #define u_handle int |
56 | #define u_handle int |
|
|
57 | #define u_valid(fd) ((fd) >= 0) |
|
|
58 | |
55 | #define u_setenv(name,value) setenv (name, value, 1) |
59 | #define u_setenv(name,value) setenv (name, value, 1) |
56 | #define u_mkdir(path) mkdir (path, 0777) |
60 | #define u_mkdir(path) mkdir (path, 0777) |
57 | #define u_chdir(path) chdir (path) |
61 | #define u_chdir(path) chdir (path) |
58 | #define u_rename(fr,to) rename (fr, to) |
62 | #define u_rename(fr,to) rename (fr, to) |
59 | #define u_open(path) open (path, O_RDONLY) + 1 |
63 | #define u_open(path) open (path, O_RDONLY) |
60 | #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) + 1 |
64 | #define u_creat(path,exec) open (path, O_WRONLY | O_CREAT | O_TRUNC, (exec) ? 0777 : 0666) |
61 | #define u_close(handle) close (handle - 1) |
65 | #define u_close(handle) close (handle) |
62 | #define u_append(path,add) strcat (strcat (path, "/"), add) |
66 | #define u_append(path,add) strcat (strcat (path, "/"), add) |
63 | #define u_write(handle,data,len) write ((handle) - 1, data, len) |
67 | #define u_write(handle,data,len) write (handle, data, len) |
64 | |
68 | |
65 | // on a mostly idle system, a sync at the end is certainly faster, hope for the best |
69 | // on a mostly idle system, a sync at the end is certainly faster, hope for the best |
66 | #define u_fsync(handle) |
70 | #define u_fsync(handle) |
67 | #define u_sync() sync () |
71 | #define u_sync() sync () |
68 | |
72 | |
… | |
… | |
289 | |
293 | |
290 | if (u_chdir (exe_dir)) |
294 | if (u_chdir (exe_dir)) |
291 | fatal ("unable to change to application data directory"); |
295 | fatal ("unable to change to application data directory"); |
292 | |
296 | |
293 | u_handle h = u_open ("override"); |
297 | u_handle h = u_open ("override"); |
294 | if (h) |
298 | if (u_valid (h)) |
295 | { |
299 | { |
296 | u_handle oh = pack_handle; |
300 | u_handle oh = pack_handle; |
297 | |
301 | |
298 | pack_handle = h; |
302 | pack_handle = h; |
299 | if (!pack_map ()) |
303 | if (!pack_map ()) |
… | |
… | |
357 | len = dlen; |
361 | len = dlen; |
358 | } |
362 | } |
359 | else |
363 | else |
360 | fatal ("unable to uncompress file data - pack corrupted?"); |
364 | fatal ("unable to uncompress file data - pack corrupted?"); |
361 | |
365 | |
362 | if (!h) |
366 | if (!u_valid (h)) |
363 | fatal ("unable to unpack file from packfile - disk full?"); |
367 | fatal ("unable to unpack file from packfile - disk full?"); |
364 | |
368 | |
365 | if (u_write (h, data, len) != len) |
369 | if (u_write (h, data, len) != len) |
366 | fatal ("unable to unpack file from packfile - disk full?"); |
370 | fatal ("unable to unpack file from packfile - disk full?"); |
367 | |
371 | |
… | |
… | |
398 | u_setenv ("URLADER_EXECDIR", execdir); |
402 | u_setenv ("URLADER_EXECDIR", execdir); |
399 | u_setenv ("URLADER_EXE_ID" , exe_id); |
403 | u_setenv ("URLADER_EXE_ID" , exe_id); |
400 | u_setenv ("URLADER_EXE_DIR", exe_dir); |
404 | u_setenv ("URLADER_EXE_DIR", exe_dir); |
401 | u_setenv ("URLADER_EXE_VER", exe_ver); |
405 | u_setenv ("URLADER_EXE_VER", exe_ver); |
402 | |
406 | |
|
|
407 | #if 0 |
403 | // yes, this is overkill |
408 | // yes, this is overkill |
404 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
409 | u_setenv ("SHLIB_PATH" , execdir); // hpux |
405 | u_setenv ("LIBPATH" , execdir); // aix |
410 | u_setenv ("LIBPATH" , execdir); // aix |
406 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
411 | u_setenv ("LD_LIBRARY_PATH" , execdir); // most elf systems |
407 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
412 | u_setenv ("LD_LIBRARY_PATH_32", execdir); // solaris |
408 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
413 | u_setenv ("LD_LIBRARY_PATH_64", execdir); // solaris |
409 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
414 | u_setenv ("LD_LIBRARYN32_PATH", execdir); // irix |
410 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
415 | u_setenv ("LD_LIBRARY64_PATH" , execdir); // irix |
411 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
416 | u_setenv ("DYLD_LIBRARY_PATH" , execdir); // os sucks from apple |
|
|
417 | #endif |
412 | } |
418 | } |
413 | |
419 | |
414 | static void |
420 | static void |
415 | execute (void) |
421 | execute (void) |
416 | { |
422 | { |
… | |
… | |
424 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
430 | WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) |
425 | { |
431 | { |
426 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
432 | if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) |
427 | fatal ("unable to find executable pack"); |
433 | fatal ("unable to find executable pack"); |
428 | |
434 | |
429 | if (!(pack_handle = u_open (tmppath))) |
435 | pack_handle = u_open (tmppath); |
|
|
436 | if (!u_valid (pack_handle)) |
430 | fatal ("unable to open executable pack"); |
437 | fatal ("unable to open executable pack"); |
431 | |
438 | |
432 | exe_info (); |
439 | exe_info (); |
433 | |
440 | |
434 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
441 | if (!GetCurrentDirectory (sizeof (currdir), currdir)) |
… | |
… | |
451 | int |
458 | int |
452 | main (int argc, char *argv[]) |
459 | main (int argc, char *argv[]) |
453 | { |
460 | { |
454 | char *home = getenv ("HOME"); |
461 | char *home = getenv ("HOME"); |
455 | |
462 | |
456 | // we assume fd 0 and 2 are "normal" |
|
|
457 | |
|
|
458 | if (!(pack_handle = u_open (argv [0]))) |
463 | pack_handle = u_open (argv [0]); |
|
|
464 | if (!u_valid (pack_handle)) |
459 | fatal ("unable to open executable pack"); |
465 | fatal ("unable to open executable pack"); |
460 | |
466 | |
461 | exe_info (); |
467 | exe_info (); |
462 | |
468 | |
463 | if (!home) |
469 | if (!home) |
… | |
… | |
475 | |
481 | |
476 | u_mkdir (home); |
482 | u_mkdir (home); |
477 | sprintf (datadir, "%s/.%s", home, URLADER); |
483 | sprintf (datadir, "%s/.%s", home, URLADER); |
478 | u_mkdir (datadir); |
484 | u_mkdir (datadir); |
479 | |
485 | |
|
|
486 | #if 0 |
480 | if (gethostname (tmppath, sizeof (tmppath))) |
487 | if (gethostname (tmppath, sizeof (tmppath))) |
481 | strcpy (tmppath, "default"); |
488 | strcpy (tmppath, "default"); |
482 | |
489 | |
483 | u_append (datadir, tmppath); |
490 | u_append (datadir, tmppath); |
|
|
491 | #endif |
484 | |
492 | |
485 | load (); |
493 | load (); |
486 | execute (); |
494 | execute (); |
487 | |
495 | |
488 | return 0; |
496 | return 0; |