Does Windows "cmd.exe" argue arguments differently?

It seems to me that the way the cmd.exeWindows string argument string is different from the way the C-compiled exe does it.

To illustrate, it cmd /C "echo ok"prints "ok" correctly. But cmd "/C" "echo ok"leads to

'"echo ok' is not recognized as an internal or external command,
operable program or batch file.

For comparison, here is the C program, CommandArguments.c, which prints the arguments in turn:

int main(int argc, char *argv[])
{
    int i;
    for (i = 0; i < argc; ++i) {
        printf("%s\n", argv[i]);
    }
}

If I run CommandArguments.exe "/C" "echo ok", it prints correctly

CommandArguments.exe
/C
echo ok

I ask about this because I am implementing an API for porting CreateProcess. I quote and avoid all input arguments before moving on to CreateProcess. It works for most things, but not because cmdof the above problem.

, , cmd -? ? , -?

+4
3

, . C, .

API , , , Windows.

, , , .

, (cmd /?):

/C /K, , ("):

  • ,      :

    • /S
    • ,   special : & < > () @^ |
    •  
    • -   .
  • , - ,      , ,      ,      .

, , /S. , , [foo],

cmd /s /c "[foo]"
+3

! cmd.exe -! cmd.exe /? :

/c /k, cmd

, , /C !

C:\Users\Lukas>cmd "/C" "ECHO Hallo"
'"echo ok' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Lukas>cmd /CECHO Hallo
Hallo

C:\Users\Lukas>cmd "/CECHO Hallo"
Hallo"

C:\Users\Lukas>

, cmd.exe , /C ( ). , , () , cmd.exe - .

+2

, cmd /C /K .

? /C//K , cmd.

, :

cmd /S /C del /Q "any file.txt"

So, /Sand /Care arguments for cmd, but del, /Q... are not. Everything after /Cis stored together and is considered as a separate command line:

del /Q "any file.txt"

Arguments /Qand "any file.txt"apply to the team del.

+2
source

Source: https://habr.com/ru/post/1621573/


All Articles