@@ -1187,9 +1187,8 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
11871187{
11881188#if defined(PHP_WIN32 )
11891189 int num_args ;
1190- wchar_t * * argv_wide ;
1190+ wchar_t * * argv_wide = NULL ;
11911191 char * * argv_save = argv ;
1192- BOOL using_wide_argv = 0 ;
11931192#endif
11941193
11951194 int c ;
@@ -1341,7 +1340,26 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
13411340 we can access the internal charset information from PHP. */
13421341 argv_wide = CommandLineToArgvW (GetCommandLineW (), & num_args );
13431342 PHP_WIN32_CP_W_TO_ANY_ARRAY (argv_wide , num_args , argv , argc )
1344- using_wide_argv = 1 ;
1343+ } else if (!php_win32_cp_use_unicode ()) {
1344+ /* Custom arguments are UTF-8, regardless of PHP's configured encoding. */
1345+ char * * converted_argv = calloc ((size_t ) argc + 1 , sizeof (char * ));
1346+ if (!converted_argv ) {
1347+ exit_status = 1 ;
1348+ goto out ;
1349+ }
1350+ argv = converted_argv ;
1351+ for (int i = 0 ; i < argc ; i ++ ) {
1352+ wchar_t * arg = php_win32_cp_utf8_to_w (argv_save [i ]);
1353+ if (arg ) {
1354+ argv [i ] = php_win32_cp_w_to_any (arg );
1355+ free (arg );
1356+ }
1357+ if (!argv [i ]) {
1358+ fprintf (stderr , "Could not convert command line argument %d.\n" , i );
1359+ exit_status = 1 ;
1360+ goto out ;
1361+ }
1362+ }
13451363 }
13461364
13471365 SetConsoleCtrlHandler (php_cli_win32_ctrl_handler , TRUE);
@@ -1381,8 +1399,10 @@ PHP_CLI_API int do_php_cli(int argc, char *argv[])
13811399#if defined(PHP_WIN32 )
13821400 (void )php_win32_cp_cli_restore ();
13831401
1384- if (using_wide_argv ) {
1402+ if (argv != argv_save ) {
13851403 PHP_WIN32_CP_FREE_ARRAY (argv , argc );
1404+ }
1405+ if (argv_wide ) {
13861406 LocalFree (argv_wide );
13871407 }
13881408 argv = argv_save ;
0 commit comments