This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: commanline argument parsing


grischka <gr1008@googlemail.com> writes:

> If I compile this snippet:
>
> #include <stdio.h>
> int main (int argc, char **argv)
> {
>     int i;
>     for (i = 0; i < argc; ++i)
>         printf("argv[%d] %s\n", i, argv[i]);
>     return 0;
> }
>
> with cygwin GCC and then run it from CMD prompt:
>
>     C:\cygwin\home\me> test \"stuff\"
>
> it prints this:
>
>     argv[0] test
>     argv[1] \stuff"
>
> Is that expected?  I'm aware that there is some conversion going on
> and that it's meant to work from a cygwin shell really, but still.

Yes, it's expected. The 1st `\' is not special to windows, so it get
printed, the 1st `"' start a quote and it's removed, the 2nd `\' is in a
quoted string, so it's removed but the 2nd `"' following it gets
printed. And you didn't end your quoted string properly.

You can also try test "x""y", it should print `argv[1] x"y'

>
> Could someone shed light upon the reasoning with this?

On Windows, you can only start a program with a string: `command args',
every program will do their own cmd line argument parsing. Most, but not
all, GUI apps will explicitly use the CRT (CommandLineToArgvW) to parse
it, since the WinMain is like (note the 3rd argument):

        int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)

and there is no ARGC and ARGV. On the other hand, all CLI apps do have
ARGC and ARGV, but it's only because windows already called
CommandLineToArgvW for you (I think).

On Posix system, there's no concept like a lpCmdLine, the shell
(bash/tcsh, whatever it is) will parse the `cmdline' and invoke execve.

On cygwin, I didn't read the code, but I guess execve need do the
reverse work of CommandLineToArgvW, that is, build a lpCmdLine from
ARGC/ARGV, and hand it to CreateProcess

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]