Showing the changes to src/core/util_string.cpp (1 of 2 files). Show all
Unified · Split
| @@ -494,6 +494,28 @@ std::string globTildePath( const std::string & path ) · 493 unmodified lines | ||
| 494 | 494 | // path expansion using homemade duct tape on Windows |
| 495 | 495 | //----------------------------------------------------------------------------- |
| 496 | 496 | #ifdef __PLATFORM_WIN32__ |
| 497 | + | |
| 498 | +//----------------------------------------------------------------------------- | |
| 499 | +// name: getUserNameWindows() | |
| 500 | +// desc: get the user name on windows | |
| 501 | +//----------------------------------------------------------------------------- | |
| 502 | +std::string getUserNameWindows() | |
| 503 | +{ | |
| 504 | + // allocate buffer | |
| 505 | + DWORD buflen = 128; | |
| 506 | + char * buf = new char[buflen]; | |
| 507 | + // get user name | |
| 508 | + BOOL retval = GetUserNameA( buf, &buflen ); | |
| 509 | + // result to return | |
| 510 | + string result; | |
| 511 | + // check if succeeded | |
| 512 | + if( retval ) result = buf; | |
| 513 | + // clean up | |
| 514 | + SAFE_DELETE_ARRAY( buf ); | |
| 515 | + // return | |
| 516 | + return result; | |
| 517 | +} | |
| 518 | + | |
| 497 | 519 | //----------------------------------------------------------------------------- |
| 498 | 520 | // name: expandFilePathWindows() |
| 499 | 521 | // desc: expand file path (windows edition) |
| @@ -512,13 +534,28 @@ std::string expandFilePathWindows( const string & path ) · 12 unmodified lines | ||
| 512 | 534 | tokenize( thePath, tokens, "%" ); |
| 513 | 535 | // every other one is an environment variable |
| 514 | 536 | t_CKBOOL expand = (thePath.length() && thePath[0] == '%'); |
| 515 | − // loop | |
| 516 | − for( t_CKINT i = 0; i < tokens.size(); i++ ) | |
| 537 | + // loop; flip 'expand' | |
| 538 | + // HACK: assume path does not contain immediately consecutive env-vars | |
| 539 | + for( t_CKINT i = 0; i < tokens.size(); i++, expand = !expand ) | |
| 517 | 540 | { |
| 518 | 541 | // connstruct expansion |
| 519 | 542 | if( !expand ) exp += tokens[i]; |
| 520 | 543 | else |
| 521 | 544 | { |
| 545 | + // check for special vars | |
| 546 | + if( tolower( tokens[i] ) == "username" ) | |
| 547 | + { | |
| 548 | + // this is supposedly safer than querying the env-var, which is easier to spoof | |
| 549 | + string u = getUserNameWindows(); | |
| 550 | + // if got something | |
| 551 | + if( u != "" ) | |
| 552 | + { | |
| 553 | + // append | |
| 554 | + exp += u; | |
| 555 | + // move to next token | |
| 556 | + continue; | |
| 557 | + } | |
| 558 | + } | |
| 522 | 559 | // get environment variable |
| 523 | 560 | char * v = getenv( tokens[i].c_str() ); |
| 524 | 561 | // if got something back |
| @@ -533,8 +570,6 @@ std::string expandFilePathWindows( const string & path ) · 8 unmodified lines | ||
| 533 | 570 | break; |
| 534 | 571 | } |
| 535 | 572 | } |
| 536 | − // flip | HACK: path does not contain immediately consecutive env-vars | |
| 537 | − expand = !expand; | |
| 538 | 573 | } |
| 539 | 574 | |
| 540 | 575 | // if exp still empty, no change |
| @@ -610,6 +645,7 @@ std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) · 69 unmodified lines | ||
| 610 | 645 | // no expansion in Emscripten (webchuck) or Android or iOS |
| 611 | 646 | return fp; |
| 612 | 647 | #elif defined(__PLATFORM_WIN32__) |
| 648 | + // 1.5.0.4 (ge) added | |
| 613 | 649 | return expandFilePathWindows( fp ); |
| 614 | 650 | #else |
| 615 | 651 | // expand ~ to full path |