|
19 | 19 | #include <conio.h>
|
20 | 20 | #include <stdint.h>
|
21 | 21 | #include <stdlib.h> /* exit */
|
| 22 | +#include <wchar.h> |
22 | 23 |
|
23 | 24 | int autoupdate(
|
24 | 25 | int argc,
|
@@ -492,6 +493,30 @@ int autoupdate(
|
492 | 493 | free(body_buffer);
|
493 | 494 | return 2;
|
494 | 495 | }
|
| 496 | + /* Windows paths can never be longer than this. */ |
| 497 | + const size_t exec_path_len = 32768; |
| 498 | + wchar_t exec_path[32768]; |
| 499 | + DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len); |
| 500 | + if (0 == utf16_len) { |
| 501 | + fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError()); |
| 502 | + free((void*)(tmpdir)); |
| 503 | + free(uncomp); |
| 504 | + free(body_buffer); |
| 505 | + return 2; |
| 506 | + } |
| 507 | + if (tmpdir[0] != exec_path[0]) { |
| 508 | + free((void*)(tmpdir)); |
| 509 | + tmpdir = wcsdup(exec_path); |
| 510 | + wchar_t *backslash = wcsrchr(tmpdir, L'\\'); |
| 511 | + if (NULL == backslash) { |
| 512 | + fprintf(stderr, "Auto-update Failed: Cannot find an approriate tmpdir with %S\n", tmpdir); |
| 513 | + free((void*)(tmpdir)); |
| 514 | + free(uncomp); |
| 515 | + free(body_buffer); |
| 516 | + return 2; |
| 517 | + } |
| 518 | + *backslash = 0; |
| 519 | + } |
495 | 520 | wchar_t *tmpf = autoupdate_tmpf(tmpdir, "exe");
|
496 | 521 | if (NULL == tmpf) {
|
497 | 522 | fprintf(stderr, "Auto-update Failed: no temporary file available\n");
|
@@ -524,17 +549,6 @@ int autoupdate(
|
524 | 549 | fclose(fp);
|
525 | 550 | free(uncomp);
|
526 | 551 | free(body_buffer);
|
527 |
| - /* Windows paths can never be longer than this. */ |
528 |
| - const size_t exec_path_len = 32768; |
529 |
| - wchar_t exec_path[32768]; |
530 |
| - DWORD utf16_len = GetModuleFileNameW(NULL, exec_path, exec_path_len); |
531 |
| - if (0 == utf16_len) { |
532 |
| - fprintf(stderr, "Auto-update Failed: GetModuleFileNameW failed with GetLastError=%d\n", GetLastError()); |
533 |
| - DeleteFileW(tmpf); |
534 |
| - free((void*)(tmpdir)); |
535 |
| - free((void*)(tmpf)); |
536 |
| - return 2; |
537 |
| - } |
538 | 552 | // Backup
|
539 | 553 | wchar_t *selftmpf = autoupdate_tmpf(tmpdir, "exe");
|
540 | 554 | if (NULL == selftmpf) {
|
|
0 commit comments