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

Comparing Urlader/urlader.c (file contents):
Revision 1.11 by root, Thu Jan 5 06:00:13 2012 UTC vs.
Revision 1.12 by root, Fri Jan 6 07:56:36 2012 UTC

14static u_handle pack_handle; 14static u_handle pack_handle;
15static u_handle lock_handle; 15static u_handle lock_handle;
16static char tmppath[MAX_PATH]; 16static char tmppath[MAX_PATH];
17 17
18static int exe_argc; 18static int exe_argc;
19static const char *exe_argv[MAX_ARGC]; 19static u_dynbuf exe_argv;
20static char exe_args[MAX_ARGS]; /* actual arguments strings copied here */ 20static u_dynbuf exe_args; /* actual arguments strings copied here */
21static unsigned int exe_argo;
22 21
23static void 22static void
24tmpdir (const char *dir) 23tmpdir (const char *dir)
25{ 24{
26 static int cnt; 25 static int cnt;
72 #endif 71 #endif
73 systemv (argv); 72 systemv (argv);
74} 73}
75 74
76static char *pack_base, *pack_end; 75static char *pack_base, *pack_end;
76static struct u_pack_tail *pack_tail;
77static struct u_pack_hdr *pack_cur; 77static struct u_pack_hdr *pack_cur;
78static char *scratch; 78static char *scratch;
79static unsigned int scratch_size; 79static unsigned int scratch_size;
80 80
81#define PACK_NAME ((char *)(pack_cur + 1)) 81#define PACK_NAME ((char *)(pack_cur + 1))
125 125
126 addr = u_mmap (pack_handle, size); 126 addr = u_mmap (pack_handle, size);
127 if (!addr) 127 if (!addr)
128 return 0; 128 return 0;
129 129
130 pack_unmap (); 130 /*pack_unmap ();*/
131 131
132 pack_base = addr; 132 pack_base = addr;
133 pack_end = pack_base + size; 133 pack_end = pack_base + size;
134 134
135 struct u_pack_tail *tail;
136
137 tail = (void *)(pack_end - sizeof (*tail)); 135 pack_tail = (void *)(pack_end - sizeof (*pack_tail));
138 136
139 if (memcmp (tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1)) 137 if (memcmp (pack_tail->magic, TAIL_MAGIC, sizeof (TAIL_MAGIC) - 1))
140 return 0; 138 return 0;
141 139
142 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (tail->size)); 140 pack_cur = (struct u_pack_hdr *)(pack_end - u_32 (pack_tail->size));
143 141
142 if (pack_cur->type != T_META)
143 return 0;
144
144 scratch = u_malloc (scratch_size = u_32 (tail->max_uncompressed)); 145 scratch = u_malloc (scratch_size = u_32 (pack_tail->max_uncompressed));
145 if (!scratch) 146 if (!scratch)
146 return 0; 147 return 0;
147 148
148 return 1; 149 return 1;
149} 150}
150 151
151static void 152static void
152exe_info (void) 153add_arg (char *arg, unsigned int len)
153{ 154{
154 if (!pack_map ()) 155 char *addr = u_dynbuf_append (&exe_args, arg, len);
155 u_fatal ("unable to locate packfile in executable - executable corrupted?"); 156 u_dynbuf_append (&exe_argv, &addr, sizeof (addr));
156
157 if (pack_cur->type != T_META)
158 u_fatal ("unable to locate executable metadata - executable corrupted?");
159
160 strcpy (exe_id, PACK_NAME);
161} 157}
162 158
163static void 159static void
164load (void) 160load (void)
165{ 161{
162 strcpy (exe_id , PACK_NAME);
163 strcpy (exe_ver, PACK_DATA);
164 u_set_exe_info ();
165
166 if (u_chdir (exe_dir)) 166 if (u_chdir (exe_dir))
167 u_fatal ("unable to change to application data directory"); 167 u_fatal ("unable to change to application data directory");
168 168
169 u_handle h = u_open ("override"); 169 u_handle h = u_open ("override");
170 if (u_valid (h)) 170 if (u_valid (h))
171 { 171 {
172 u_handle oh = pack_handle; 172 u_handle oh = pack_handle;
173 173
174 pack_unmap ();
174 pack_handle = h; 175 pack_handle = h;
176
175 if (pack_map ()) 177 if (pack_map ()
178 && strcmp (exe_id , PACK_NAME) == 0
179 && strcmp (exe_ver, PACK_DATA) <= 0)
176 u_setenv ("URLADER_OVERRIDE", "override"); 180 u_setenv ("URLADER_OVERRIDE", "override");
177 else 181 else
178 { 182 {
183 pack_unmap ();
179 pack_handle = oh; 184 pack_handle = oh;
180 oh = h; 185 oh = h;
186 pack_map ();
181 } 187 }
182 188
183 u_close (oh); 189 u_close (oh);
184 } 190 }
185
186 if (pack_cur->type != T_META)
187 u_fatal ("unable to locate override metadata");
188 191
189 strcpy (exe_ver, PACK_DATA); 192 strcpy (exe_ver, PACK_DATA);
190 u_set_exe_info (); 193 u_set_exe_info ();
191 pack_next (); 194 pack_next ();
192 195
193 for (;;) 196 for (;;)
194 { 197 {
195 if (pack_cur->type == T_ENV) 198 if (pack_cur->type == T_ENV)
196 u_setenv (PACK_NAME, PACK_DATA); 199 u_setenv (PACK_NAME, PACK_DATA);
197 else if (pack_cur->type == T_ARG) 200 else if (pack_cur->type == T_ARG)
198 { 201 add_arg (PACK_NAME, u_16 (pack_cur->namelen) + 1);
199 int len = u_16 (pack_cur->namelen) + 1;
200 exe_argv [exe_argc++] = exe_args + exe_argo;
201 memcpy (exe_args + exe_argo, PACK_NAME, len);
202 exe_argo += len;
203 }
204 else 202 else
205 break; 203 break;
206 204
207 pack_next (); 205 pack_next ();
208 } 206 }
295} 293}
296 294
297static void 295static void
298execute (void) 296execute (void)
299{ 297{
300 exe_argv [exe_argc] = 0; 298 char *null = 0;
301 systemv (exe_argv); 299 u_dynbuf_append (&exe_argv, &null, sizeof (null));
300 systemv ((const char *const *)exe_argv.addr);
302} 301}
303 302
303// this argc/argv is without argv [0]
304static void 304static void
305doit (void) 305doit (int argc, char *argv[])
306{ 306{
307 int i;
308
309 u_setenv ("URLADER_CURRDIR", currdir);
310
307 u_set_datadir (); 311 u_set_datadir ();
308 u_mkdir (datadir); 312 u_mkdir (datadir);
309 313
310 exe_info (); 314 if (!pack_map ())
311 u_set_exe_info (); 315 u_fatal ("unable to map pack file - executable corrupted?");
312 316
313 load (); 317 load ();
318
319 while (argc--)
320 {
321 add_arg (*argv, strlen (*argv) + 1);
322 ++argv;
323 }
324
314 execute (); 325 execute ();
315} 326}
316 327
317#ifdef _WIN32 328#ifdef _WIN32
318 329
319int APIENTRY 330int APIENTRY
320WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show) 331WinMain (HINSTANCE hI, HINSTANCE hP, LPSTR argv, int command_show)
321{ 332{
322 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath))) 333 if (!GetModuleFileName (hI, tmppath, sizeof (tmppath)))
323 u_fatal ("unable to find executable pack"); 334 u_fatal ("unable to find executable pack");
335
336 u_setenv ("URLADER_EXEPATH", tmppath);
324 337
325 pack_handle = u_open (tmppath); 338 pack_handle = u_open (tmppath);
326 if (!u_valid (pack_handle)) 339 if (!u_valid (pack_handle))
327 u_fatal ("unable to open executable pack"); 340 u_fatal ("unable to open executable pack");
328 341
329 if (!GetCurrentDirectory (sizeof (currdir), currdir)) 342 if (!GetCurrentDirectory (sizeof (currdir), currdir))
330 strcpy (currdir, "."); 343 strcpy (currdir, ".");
331 344
332 doit (); 345 doit (1, &argv);
333 346
334 return 0; 347 return 0;
335} 348}
336 349
337#else 350#else
338 351
339int 352int
340main (int argc, char *argv[]) 353main (int argc, char *argv[])
341{ 354{
355 u_setenv ("URLADER_EXEPATH", argv [0]);
356
342 pack_handle = u_open (argv [0]); 357 pack_handle = u_open (argv [0]);
343 if (!u_valid (pack_handle)) 358 if (!u_valid (pack_handle))
344 u_fatal ("unable to open executable pack"); 359 u_fatal ("unable to open executable pack");
345 360
346 if (!getcwd (currdir, sizeof (currdir))) 361 if (!getcwd (currdir, sizeof (currdir)))
353 strcpy (tmppath, "default"); 368 strcpy (tmppath, "default");
354 369
355 u_append (datadir, tmppath); 370 u_append (datadir, tmppath);
356#endif 371#endif
357 372
358 doit (); 373 doit (argc - 1, argv + 1);
359 374
360 return 0; 375 return 0;
361} 376}
362 377
363#endif 378#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines