--- Urlader/urlader.c 2011/12/30 23:34:08 1.8 +++ Urlader/urlader.c 2012/01/02 09:23:03 1.9 @@ -1,3 +1,4 @@ +#include "urlib.h" #include "urlib.c" #include @@ -9,18 +10,10 @@ #include "liblzf/lzf_d.c" -#include "urlib.h" - /* some simple? dynamic memory management for paths might save ltos of ram */ static u_handle pack_handle; static u_handle lock_handle; static char tmppath[MAX_PATH]; -static char currdir[MAX_PATH]; -static char datadir[MAX_PATH]; // %AppData%/urlader -static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID -static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER -static char exe_id[MAX_PATH]; -static char exe_ver[MAX_PATH]; static int exe_argc; static const char *exe_argv[MAX_ARGC]; @@ -28,20 +21,6 @@ static unsigned int exe_argo; static void -fatal (const char *msg) -{ -#ifdef _WIN32 - MessageBox (0, msg, URLADER, 0); -#else - write (2, URLADER ": ", sizeof (URLADER ": ") - 1); - write (2, msg, strlen (msg)); - write (2, "\n", 1); -#endif - - _exit (1); -} - -static void tmpdir (const char *dir) { static int cnt; @@ -68,7 +47,7 @@ pid_t pid = fork (); if (pid < 0) - fatal ("fork failure"); + u_fatal ("fork failure"); if (!pid) { @@ -173,10 +152,10 @@ exe_info (void) { if (!pack_map ()) - fatal ("unable to locate packfile in executable - executable corrupted?"); + u_fatal ("unable to locate packfile in executable - executable corrupted?"); - if (pack_cur->type != U_T_META) - fatal ("unable to locate executable metadata - executable corrupted?"); + if (pack_cur->type != T_META) + u_fatal ("unable to locate executable metadata - executable corrupted?"); strcpy (exe_id, PACK_NAME); } @@ -184,14 +163,8 @@ static void load (void) { - u_mkdir (datadir); - - strcpy (exe_dir, datadir); - u_append (exe_dir, exe_id); - u_mkdir (exe_dir); - if (u_chdir (exe_dir)) - fatal ("unable to change to application data directory"); + u_fatal ("unable to change to application data directory"); u_handle h = u_open ("override"); if (u_valid (h)) @@ -208,17 +181,18 @@ u_close (oh); } - if (pack_cur->type != U_T_META) - fatal ("unable to locate override metadata"); + if (pack_cur->type != T_META) + u_fatal ("unable to locate override metadata"); strcpy (exe_ver, PACK_DATA); + u_set_exe_info (); pack_next (); for (;;) { - if (pack_cur->type == U_T_ENV) + if (pack_cur->type == T_ENV) u_setenv (PACK_NAME, PACK_DATA); - else if (pack_cur->type == U_T_ARG) + else if (pack_cur->type == T_ARG) { int len = u_16 (pack_cur->namelen) + 1; exe_argv [exe_argc++] = exe_args + exe_argo; @@ -232,14 +206,11 @@ } done_env_arg: - strcpy (execdir, exe_dir); - u_append (execdir, "i-"); - strcat (execdir, exe_ver); strcat (strcpy (tmppath, execdir), ".lck"); lock_handle = u_lock (tmppath, 0, 1); if (!lock_handle) - fatal ("unable to lock application instance"); + u_fatal ("unable to lock application instance"); if (access (execdir, F_OK)) { @@ -247,43 +218,43 @@ tmpdir (exe_dir); if (u_chdir (tmppath)) - fatal ("unable to change to new instance directory"); + u_fatal ("unable to change to new instance directory"); for (;;) { switch (pack_cur->type) { - case U_T_DIR: + case T_DIR: u_mkdir (PACK_NAME); break; - case U_T_FILE: + case T_FILE: { - u_handle h = u_creat (PACK_NAME, pack_cur->flags & U_F_EXEC); + u_handle h = u_creat (PACK_NAME, pack_cur->flags & F_EXEC); unsigned int dlen, len = u_32 (pack_cur->datalen); char *data = PACK_DATA; - if (pack_cur->flags & U_F_LZF) + if (pack_cur->flags & F_LZF) if (dlen = lzf_decompress (data, len, scratch, scratch_size)) { data = scratch; len = dlen; } else - fatal ("unable to uncompress file data - pack corrupted?"); + u_fatal ("unable to uncompress file data - pack corrupted?"); if (!u_valid (h)) - fatal ("unable to unpack file from packfile - disk full?"); + u_fatal ("unable to unpack file from packfile - disk full?"); if (u_write (h, data, len) != len) - fatal ("unable to unpack file from packfile - disk full?"); + u_fatal ("unable to unpack file from packfile - disk full?"); u_fsync (h); u_close (h); } break; - case U_T_NULL: + case T_NULL: goto done; } @@ -292,7 +263,7 @@ done: if (u_chdir (datadir)) - fatal ("unable to change to data directory"); + u_fatal ("unable to change to data directory"); u_sync (); @@ -304,14 +275,9 @@ u_close (pack_handle); if (u_chdir (execdir)) - fatal ("unable to change to application instance directory"); + u_fatal ("unable to change to application instance directory"); u_setenv ("URLADER_VERSION", URLADER_VERSION); - u_setenv ("URLADER_DATADIR", datadir); - u_setenv ("URLADER_EXECDIR", execdir); - u_setenv ("URLADER_EXE_ID" , exe_id); - u_setenv ("URLADER_EXE_DIR", exe_dir); - u_setenv ("URLADER_EXE_VER", exe_ver); #if 0 // yes, this is overkill @@ -333,32 +299,34 @@ systemv (exe_argv); } +static void +doit (void) +{ + u_set_datadir (); + u_mkdir (datadir); + + exe_info (); + u_set_exe_info (); + + load (); + execute (); +} + #ifdef _WIN32 int APIENTRY WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) { if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) - fatal ("unable to find executable pack"); + u_fatal ("unable to find executable pack"); pack_handle = u_open (tmppath); if (!u_valid (pack_handle)) - fatal ("unable to open executable pack"); - - exe_info (); + u_fatal ("unable to open executable pack"); if (!GetCurrentDirectory (sizeof (currdir), currdir)) strcpy (currdir, "."); - if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) - fatal ("unable to find application data directory"); - - u_mkdir (datadir); - u_append (datadir, URLADER); - - load (); - execute (); - return 0; } @@ -367,32 +335,13 @@ int main (int argc, char *argv[]) { - char *home = getenv ("HOME"); - pack_handle = u_open (argv [0]); if (!u_valid (pack_handle)) - fatal ("unable to open executable pack"); - - exe_info (); - - if (!home) - { - struct passwd *pw; - - if ((pw = getpwuid (getuid ()))) - home = pw->pw_dir; - else - home = "/tmp"; - } + u_fatal ("unable to open executable pack"); if (!getcwd (currdir, sizeof (currdir))) strcpy (currdir, "."); - u_mkdir (home); - //strcat (strcat (strcpy (datadir, home), "/."), URLADER); - sprintf (datadir, "%s/.%s", home, URLADER); - u_mkdir (datadir); - #if 0 if (gethostname (tmppath, sizeof (tmppath))) strcpy (tmppath, "default"); @@ -400,8 +349,7 @@ u_append (datadir, tmppath); #endif - load (); - execute (); + doit (); return 0; }