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

Comparing Urlader/urlader.c (file contents):
Revision 1.6 by root, Fri Dec 30 12:21:28 2011 UTC vs.
Revision 1.14 by root, Mon Feb 6 21:57:48 2012 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 // acquire shared in addition to excl, then unlock excl
52 #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1); u_lock (U_UNLOCK, 1);
53
54#else
55
56 #define _GNU_SOURCE 1
57 #define _BSD_SOURCE 1
58 // the above increases our chances of getting MAP_ANONYMOUS
59
60 #include <sys/mman.h>
61 #include <sys/types.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 #define u_lock_write_to_read(wait) u_lock (U_LOCK_READ, 1);
95
96#endif
97
98#define u_16(ptr) (((ptr)[0] << 8) | (ptr)[1])
99#define u_32(ptr) (((ptr)[0] << 24) | ((ptr)[1] << 16) | ((ptr)[2] << 8) | (ptr)[3])
100 48
101/* 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 */
102static u_handle pack_handle; 50static u_handle pack_handle;
103static u_handle lock_handle; 51static u_handle lock_handle;
104static char tmppath[MAX_PATH]; 52static char tmppath[MAX_PATH];
105static char currdir[MAX_PATH];
106static char datadir[MAX_PATH]; // %AppData%/urlader
107static char exe_dir[MAX_PATH]; // %AppData%/urlader/EXE_ID
108static char execdir[MAX_PATH]; // %AppData%/urlader/EXE_ID/EXE_VER
109static char exe_id[MAX_PATH];
110static char exe_ver[MAX_PATH];
111 53
112static int exe_argc; 54static int exe_argc;
113static const char *exe_argv[MAX_ARGC]; 55static u_dynbuf exe_argv;
114static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 56static u_dynbuf exe_args; /* actual arguments strings copied here */
115static unsigned int exe_argo;
116
117static void
118fatal (const char *msg)
119{
120#ifdef _WIN32
121 MessageBox (0, msg, URLADER, 0);
122#else
123 write (2, URLADER ": ", sizeof (URLADER ": ") - 1);
124 write (2, msg, strlen (msg));
125 write (2, "\n", 1);
126#endif
127
128 _exit (1);
129}
130
131static void *
132u_malloc (unsigned int size)
133{
134 void *addr;
135
136#ifdef _WIN32
137 HANDLE handle = CreateFileMapping (0, 0, PAGE_READWRITE, 0, size, NULL);
138
139 if (!handle)
140 return 0;
141
142 addr = MapViewOfFile (handle, FILE_MAP_WRITE, 0, 0, size);
143
144 CloseHandle (handle);
145#elif defined (MAP_ANONYMOUS)
146 addr = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
147
148 if (addr == (void *)-1)
149 addr = 0;
150#else
151 addr = malloc (size);
152#endif
153
154 return addr;
155}
156
157static void
158u_free (void *addr, unsigned int size)
159{
160#ifdef _WIN32
161 UnmapViewOfFile (addr);
162#elif defined (MAP_ANONYMOUS)
163 munmap (addr, size);
164#else
165 free (addr);
166#endif
167}
168
169static void *
170u_mmap (u_handle h, unsigned int size)
171{
172 void *addr;
173
174#ifdef _WIN32
175 HANDLE handle = CreateFileMapping (h, 0, PAGE_READONLY, 0, size, NULL);
176
177 if (!handle)
178 return 0;
179
180 addr = MapViewOfFile (handle, FILE_MAP_READ, 0, 0, size);
181
182 CloseHandle (handle);
183#else
184 addr = mmap (0, size, PROT_READ, MAP_SHARED, h, 0);
185
186 if (addr == (void *)-1)
187 addr = 0;
188#endif
189
190 return addr;
191}
192
193static void
194u_munmap (void *addr, unsigned int len)
195{
196#ifdef _WIN32
197 UnmapViewOfFile (addr);
198#else
199 munmap (addr, len);
200#endif
201}
202 57
203static void 58static void
204tmpdir (const char *dir) 59tmpdir (const char *dir)
205{ 60{
206 static int cnt; 61 static int cnt;
216 if (!u_mkdir (tmppath)) 71 if (!u_mkdir (tmppath))
217 return; 72 return;
218 } 73 }
219} 74}
220 75
221enum
222{
223 U_UNLOCK,
224 U_LOCK_READ,
225 U_LOCK_WRITE
226};
227
228static int
229u_lock (int mode, int dowait)
230{
231#ifdef _WIN32
232 if (mode == U_UNLOCK)
233 return !UnlockFile (lock_handle, 0, 0, 1, 0);
234 else
235 {
236 OVERLAPPED ov = { 0 };
237
238 return !LockFileEx (lock_handle,
239 0
240 | (mode == U_LOCK_WRITE ? LOCKFILE_EXCLUSIVE_LOCK : 0)
241 | (dowait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
242 0,
243 1, 0,
244 &ov);
245 }
246#else
247 static short mode2lck [] = { F_UNLCK, F_RDLCK, F_WRLCK };
248 struct flock lck;
249 lck.l_type = mode2lck [mode];
250 lck.l_whence = SEEK_SET;
251 lck.l_start = 0;
252 lck.l_len = 1;
253
254 return fcntl (lock_handle, dowait ? F_SETLKW : F_SETLK, &lck);
255#endif
256}
257
258static void 76static void
259systemv (const char *const argv[]) 77systemv (const char *const argv[])
260{ 78{
261#ifdef _WIN32 79#ifdef _WIN32
262 _spawnv (P_WAIT, argv [0], argv); 80 _spawnv (P_WAIT, argv [0], argv);
263#else 81#else
264 pid_t pid = fork (); 82 pid_t pid = fork ();
265 83
266 if (pid < 0) 84 if (pid < 0)
267 fatal ("fork failure"); 85 u_fatal ("fork failure");
268 86
269 if (!pid) 87 if (!pid)
270 { 88 {
271 execv (argv [0], (void *)argv); 89 execv (argv [0], (void *)argv);
272 _exit (124); 90 _exit (124);
288 const char *argv[] = { "/bin/rm", "-rf", path, 0 }; 106 const char *argv[] = { "/bin/rm", "-rf", path, 0 };
289 #endif 107 #endif
290 systemv (argv); 108 systemv (argv);
291} 109}
292 110
293enum
294{
295 T_NULL, // 5
296 T_META, // 1 : exe_id, exe_ver
297 T_ENV, // 2 : name, value
298 T_ARG, // 3 : arg
299 T_DIR, // 4+: path
300 T_FILE, // 4+: path, data
301 T_NUM
302};
303
304enum
305{
306 F_EXEC = 1,
307 F_LZF = 2,
308 F_NULL = 0
309};
310
311struct hdr
312{
313 unsigned char type;
314 unsigned char flags;
315 unsigned char namelen[2];
316 unsigned char datalen[4];
317};
318
319struct tail {
320 unsigned char max_filesize[4];
321 unsigned char size[4];
322 char magic[12];
323};
324
325static char *pack_base, *pack_end; 111static char *pack_base, *pack_end;
112static struct u_pack_tail *pack_tail;
326static struct hdr *pack_cur; 113static struct u_pack_hdr *pack_cur;
327static char *scratch; 114static char *scratch;
328static unsigned int scratch_size; 115static unsigned int scratch_size;
329 116
330#define PACK_NAME ((char *)(pack_cur + 1)) 117#define PACK_NAME ((char *)(pack_cur + 1))
331#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1) 118#define PACK_DATA (PACK_NAME + u_16 (pack_cur->namelen) + 1)
334static void 121static void
335pack_next (void) 122pack_next (void)
336{ 123{
337 unsigned int d = u_32 (pack_cur->datalen); 124 unsigned int d = u_32 (pack_cur->datalen);
338 125
339 pack_cur = (struct hdr *)(PACK_DATA + d); 126 pack_cur = (struct u_pack_hdr *)(PACK_DATA + d);
340} 127}
341 128
342static void 129static void
343pack_unmap (void) 130pack_unmap (void)
344{ 131{
374 161
375 addr = u_mmap (pack_handle, size); 162 addr = u_mmap (pack_handle, size);
376 if (!addr) 163 if (!addr)
377 return 0; 164 return 0;
378 165
379 pack_unmap (); 166 /*pack_unmap ();*/
380 167
381 pack_base = addr; 168 pack_base = addr;
382 pack_end = pack_base + size; 169 pack_end = pack_base + size;
383 170
384 struct tail *tail;
385
386 tail = (void *)(pack_end - sizeof (*tail)); 171 pack_tail = (void *)(pack_end - sizeof (*pack_tail));
387 172
388 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 173 if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
389 return 0; 174 return 0;
390 175
391 pack_cur = (struct hdr *)(pack_end - u_32 (tail->size)); 176 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size));
392 177
178 if (pack_cur->type != T_META)
179 return 0;
180
393 scratch = u_malloc (scratch_size = u_32 (tail->max_filesize)); 181 scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed));
394 if (!scratch) 182 if (!scratch)
395 return 0; 183 return 0;
396 184
397 return 1; 185 return 1;
398} 186}
399 187
400static void 188static void
401exe_info (void) 189add_arg (char *arg, unsigned int len)
402{ 190{
403 if (!pack_map ()) 191 char *addr = u_dynbuf_append (&exe_args, arg, len);
404 fatal ("unable to locate packfile in executable - executable corrupted?"); 192 u_dynbuf_append (&exe_argv, &addr, sizeof (addr));
405
406 if (pack_cur->type != T_META)
407 fatal ("unable to locate executable metadata - executable corrupted?");
408
409 strcpy (exe_id, PACK_NAME);
410} 193}
411 194
412static void 195static void
413load (void) 196load (void)
414{ 197{
415 u_mkdir (datadir); 198 strcpy (exe_id , PACK_NAME);
416 199 strcpy (exe_ver, PACK_DATA);
417 strcpy (exe_dir, datadir); 200 u_set_exe_info ();
418 u_append (exe_dir, exe_id);
419 u_mkdir (exe_dir);
420 201
421 if (u_chdir (exe_dir)) 202 if (u_chdir (exe_dir))
422 fatal ("unable to change to application data directory"); 203 u_fatal ("unable to change to application data directory");
423 204
424 u_handle h = u_open ("override"); 205 u_handle h = u_open ("override");
425 if (u_valid (h)) 206 if (u_valid (h))
426 { 207 {
427 u_handle oh = pack_handle; 208 u_handle oh = pack_handle;
428 209
210 pack_unmap ();
429 pack_handle = h; 211 pack_handle = h;
212
430 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
431 { 218 {
219 pack_unmap ();
432 pack_handle = oh; 220 pack_handle = oh;
433 oh = h; 221 oh = h;
222 pack_map ();
434 } 223 }
435 224
436 u_close (oh); 225 u_close (oh);
437 } 226 }
438 227
439 if (pack_cur->type != T_META)
440 fatal ("unable to locate override metadata");
441
442 strcpy (exe_ver, PACK_DATA); 228 strcpy (exe_ver, PACK_DATA);
229 u_set_exe_info ();
443 pack_next (); 230 pack_next ();
444 231
445 for (;;) 232 for (;;)
446 { 233 {
447 if (pack_cur->type == T_ENV) 234 if (pack_cur->type == T_ENV)
448 u_setenv (PACK_NAME, PACK_DATA); 235 u_setenv (PACK_NAME, PACK_DATA);
449 else if (pack_cur->type == T_ARG) 236 else if (pack_cur->type == T_ARG)
450 { 237 add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1);
451 int len = u_16 (pack_cur->namelen) + 1;
452 exe_argv [exe_argc++] = exe_args + exe_argo;
453 memcpy (exe_args + exe_argo, PACK_NAME, len);
454 exe_argo += len;
455 }
456 else 238 else
457 break; 239 break;
458 240
459 pack_next (); 241 pack_next ();
460 } 242 }
461 243
462done_env_arg: 244done_env_arg:
463 strcpy (execdir, exe_dir);
464 u_append (execdir, "i-");
465 strcat (execdir, exe_ver);
466 245
467 strcat (strcpy (tmppath, execdir), ".lck"); 246 strcat (strcpy (tmppath, execdir), ".lck");
468 lock_handle = u_lockfile (tmppath); 247 lock_handle = u_lock (tmppath, 0, 1);
469 if (!lock_handle) 248 if (!lock_handle)
470 fatal ("unable to create lock file"); 249 u_fatal ("unable to lock application instance");
471
472 u_cloexec (lock_handle);
473
474 // all users that uee an instance acquire a shared lock
475 // acquiring an exclusive lock is enough to delete the dir
476 // deleeting the lockfile is another topic altogether
477 u_lock (U_LOCK_READ, 1);
478 250
479 if (access (execdir, F_OK)) 251 if (access (execdir, F_OK))
480 { 252 {
481 // does not exist yet, so unpack and move 253 // does not exist yet, so unpack and move
482 tmpdir (exe_dir); 254 tmpdir (exe_dir);
483 255
484 if (u_chdir (tmppath)) 256 if (u_chdir (tmppath))
485 fatal ("unable to change to new instance directory"); 257 u_fatal ("unable to change to new instance directory");
486 258
487 for (;;) 259 for (;;)
488 { 260 {
489 switch (pack_cur->type) 261 switch (pack_cur->type)
490 { 262 {
503 { 275 {
504 data = scratch; 276 data = scratch;
505 len = dlen; 277 len = dlen;
506 } 278 }
507 else 279 else
508 fatal ("unable to uncompress file data - pack corrupted?"); 280 u_fatal ("unable to uncompress file data - pack corrupted?");
509 281
510 if (!u_valid (h)) 282 if (!u_valid (h))
511 fatal ("unable to unpack file from packfile - disk full?"); 283 u_fatal ("unable to unpack file from packfile - disk full?");
512 284
513 if (u_write (h, data, len) != len) 285 if (u_write (h, data, len) != len)
514 fatal ("unable to unpack file from packfile - disk full?"); 286 u_fatal ("unable to unpack file from packfile - disk full?");
515 287
516 u_fsync (h); 288 u_fsync (h);
517 u_close (h); 289 u_close (h);
518 } 290 }
519 break; 291 break;
525 pack_next (); 297 pack_next ();
526 } 298 }
527 299
528done: 300done:
529 if (u_chdir (datadir)) 301 if (u_chdir (datadir))
530 fatal ("unable to change to data directory"); 302 u_fatal ("unable to change to data directory");
531 303
532 u_sync (); 304 u_sync ();
533 305
534 if (u_rename (tmppath, execdir)) 306 if (u_rename (tmppath, execdir))
535 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
537 309
538 pack_unmap (); 310 pack_unmap ();
539 u_close (pack_handle); 311 u_close (pack_handle);
540 312
541 if (u_chdir (execdir)) 313 if (u_chdir (execdir))
542 fatal ("unable to change to application instance directory"); 314 u_fatal ("unable to change to application instance directory");
543 315
544 u_setenv ("URLADER_VERSION", URLADER_VERSION); 316 u_setenv ("URLADER_VERSION", URLADER_VERSION);
545 u_setenv ("URLADER_DATADIR", datadir);
546 u_setenv ("URLADER_EXECDIR", execdir);
547 u_setenv ("URLADER_EXE_ID" , exe_id);
548 u_setenv ("URLADER_EXE_DIR", exe_dir);
549 u_setenv ("URLADER_EXE_VER", exe_ver);
550 317
551#if 0 318#if 0
552 // yes, this is overkill 319 // yes, this is overkill
553 u_setenv ("SHLIB_PATH" , execdir); // hpux 320 u_setenv ("SHLIB_PATH" , execdir); // hpux
554 u_setenv ("LIBPATH" , execdir); // aix 321 u_setenv ("LIBPATH" , execdir); // aix
562} 329}
563 330
564static void 331static void
565execute (void) 332execute (void)
566{ 333{
567 exe_argv [exe_argc] = 0; 334 char *null = 0;
568 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 ();
569} 362}
570 363
571#ifdef _WIN32 364#ifdef _WIN32
572 365
573int APIENTRY 366int APIENTRY
574WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 367WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
575{ 368{
576 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 369 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
577 fatal ("unable to find executable pack"); 370 u_fatal ("unable to find executable pack");
371
372 u_setenv ("URLADER_EXEPATH", tmppath);
578 373
579 pack_handle = u_open (tmppath); 374 pack_handle = u_open (tmppath);
580 if (!u_valid (pack_handle)) 375 if (!u_valid (pack_handle))
581 fatal ("unable to open executable pack"); 376 u_fatal ("unable to open executable pack");
582
583 exe_info ();
584 377
585 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 378 if (!GetCurrentDirectory (sizeof (currdir), currdir))
586 strcpy (currdir, "."); 379 strcpy (currdir, ".");
587 380
588 if (SHGetFolderPath (0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, datadir) != S_OK) 381 doit (1, &argv);
589 fatal ("unable to find application data directory");
590
591 u_mkdir (datadir);
592 u_append (datadir, URLADER);
593
594 load ();
595 execute ();
596 382
597 return 0; 383 return 0;
598} 384}
599 385
600#else 386#else
601 387
602int 388int
603main (int argc, char *argv[]) 389main (int argc, char *argv[])
604{ 390{
605 char *home = getenv ("HOME");
606
607 pack_handle = u_open (argv [0]);
608 if (!u_valid (pack_handle))
609 fatal ("unable to open executable pack");
610
611 exe_info ();
612
613 if (!home)
614 {
615 struct passwd *pw;
616
617 if ((pw = getpwuid (getuid ())))
618 home = pw->pw_dir;
619 else
620 home = "/tmp";
621 }
622
623 if (!getcwd (currdir, sizeof (currdir))) 391 if (!getcwd (currdir, sizeof (currdir)))
624 strcpy (currdir, "."); 392 strcpy (currdir, ".");
625 393
626 u_mkdir (home); 394 {
627 //strcat (strcat (strcpy (datadir, home), "/."), URLADER); 395 const char *exe_path = 0;
628 sprintf (datadir, "%s/.%s", home, URLADER); 396
629 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 }
630 443
631#if 0 444#if 0
445 /* intersperse hostname, for whatever reason */
446
632 if (gethostname (tmppath, sizeof (tmppath))) 447 if (gethostname (tmppath, sizeof (tmppath)))
633 strcpy (tmppath, "default"); 448 strcpy (tmppath, "default");
634 449
635 u_append (datadir, tmppath); 450 u_append (datadir, tmppath);
636#endif 451#endif
637 452
638 load (); 453 doit (argc - 1, argv + 1);
639 execute ();
640 454
641 return 0; 455 return 0;
642} 456}
643 457
644#endif 458#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines