delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2009/06/30/22:33:04

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,SPF_PASS
X-Spam-Check-By: sourceware.org
From: Haojun Bao <baohaojun AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: commanline argument parsing
References: <4A4A7F68 DOT 3040306 AT gmail DOT com>
Date: Wed, 01 Jul 2009 10:32:42 +0800
In-Reply-To: <4A4A7F68.3040306@gmail.com> (grischka's message of "Tue, 30 Jun 2009 23:11:04 +0200")
Message-ID: <83zlbp6sl1.fsf@gmail.com>
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (cygwin)
MIME-Version: 1.0
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

grischka <gr1008 AT googlemail DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019