2

Usually one could use ^ to escape a character in the command-line. But I couldn't make it to work.

Here is a test program, CommandArguments.exe, which prints the arguments it got.

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

The program in question, LineBreakCommandArguments.exe, is as follow:

void main(void)
{
    system("CommandArguments ^\n"); // ^\n does not really pass a line break...
}

The output of LineBreakCommandArguments.exe:

>LineBreakCommandArguments.exe
0: CommandArguments
  67   111   109   109   97   110   100   65   114   103   117   109   101   110   116   115

As shown above, CommandArguments got only one argument. The line-break character was missing.

So the question is, how should I modify LineBreakCommandArguments, such that CommandArguments will get the line-break character as an argument?

Note that I still would like to use the system function, since I want the ability to use shell commands like dir/cd, which are not available when using CreateProcess.

Update:

The reason of asking is that I'm implementing an API for a programming language (haxe). The API allows users to pass arbitrary command to the shell. I hope people wouldn't need to use line breaks in any argument, but who knows... Anyway, since an api for a programming lang is the basis for potentially a lots of programs, I want it to cover as many cases as possible. If it is not feasible to support line breaks in argument, I will document it as is.

Andy Li
  • 5,485
  • 5
  • 32
  • 45

1 Answers1

5

OK, so I've done some testing and it does work when you use CreateProcess. That tells us that the C runtime's command line parser isn't doing anything with the linefeed, which means the problem must be related to the way cmd.exe ("the shell") parses command lines.

You're not going to find an official source on that. The best you could hope for is to find someone who has decompiled and reverse-engineered it. As far as I can tell experimentally, the shell's parser unconditionally stops processing the command line as soon as it sees a line feed. That is also the conclusion reached in this answer.

Update:

In response to the update in your question, I would recommend in your scenario that you don't treat line breaks as a special case. Assuming that the API function in question will be documented to pass commands to the shell rather than running them directly, programmers shouldn't expect to be able to pass line feeds. Of course you may prefer to document the fact explicitly, that's entirely your call. :-)

I do recommend that you provide some method to run a command directly rather than via the shell, as there are edge cases where going via the shell is inconvenient. That might be either a different function or an option.

Harry Johnston
  • 33,445
  • 6
  • 56
  • 142