History

windows chugin path enhancements

Ge Wang committed 3 years ago

Commit f5a227e1 parent bb784ef2 files · +58 −11Browse files

Showing the changes to src/core/util_string.cpp (1 of 2 files). Show all

Unified · Split

src/core/util_string.cpp+40−4
@@ -494,6 +494,28 @@ std::string globTildePath( const std::string & path ) · 493 unmodified lines
494494 // path expansion using homemade duct tape on Windows
495495 //-----------------------------------------------------------------------------
496496 #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+
497519 //-----------------------------------------------------------------------------
498520 // name: expandFilePathWindows()
499521 // desc: expand file path (windows edition)
@@ -512,13 +534,28 @@ std::string expandFilePathWindows( const string & path ) · 12 unmodified lines
512534 tokenize( thePath, tokens, "%" );
513535 // every other one is an environment variable
514536 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 )
517540 {
518541 // connstruct expansion
519542 if( !expand ) exp += tokens[i];
520543 else
521544 {
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+ }
522559 // get environment variable
523560 char * v = getenv( tokens[i].c_str() );
524561 // if got something back
@@ -533,8 +570,6 @@ std::string expandFilePathWindows( const string & path ) · 8 unmodified lines
533570 break;
534571 }
535572 }
536− // flip | HACK: path does not contain immediately consecutive env-vars
537− expand = !expand;
538573 }
539574
540575 // if exp still empty, no change
@@ -610,6 +645,7 @@ std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) · 69 unmodified lines
610645 // no expansion in Emscripten (webchuck) or Android or iOS
611646 return fp;
612647 #elif defined(__PLATFORM_WIN32__)
648+ // 1.5.0.4 (ge) added
613649 return expandFilePathWindows( fp );
614650 #else
615651 // expand ~ to full path

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t