Subject: Does crt prepare arg[cv]?
From: Tak Ota <[email-masked]>
To: [email-masked]
Date: Tue, 03 Jul 2001 23:24:48 -0700 (PDT)

Hi,

While I was working on gdb modification I encountered a problem which
answer must be in the source but I could not find it.  Gdb is
irrelevant to this question.

The question is this.  Is it CRT routines that parses the command line
argument of CreateProcess and prepares argc, argv and envp?  If the
answer is yes I want to know how it parses the command line.

The problem is when the following program "print_arg.c" is run from
the program "create_process.c" the result argument is not what I
expect.

Running print_arg from bash manually results this.

bash-2.05$ print_arg 'a '\''b c'\'''
args = [d:\ota\project\hellogcc\print_arg.exe "a 'b c'"]
argc = 2
argv[0] = print_arg
argv[1] = a 'b c'

Running create_process results this.

bash-2.05$ create_process
args = [.\print_arg.exe 'a '\''b c'\''']
CreateProcess success
args = [.\print_arg.exe 'a '\''b c'\''']
argc = 3
argv[0] = ./print_arg
argv[1] = a b
argv[2] = c'

I am puzzled.  Could anyone help me solve this mystery?

-Tak

-------------------------- print_arg.c ------------------------------
#include <stdio.h>
#include <windows.h>

int main(int argc, char **argv)
{
  int i;
  fprintf(stderr, "args = [%s]\n", GetCommandLineA ());
  fprintf(stderr, "argc = %d\n", argc);
  for(i = 0; i < argc; i++) {
    fprintf(stderr, "argv[%d] = %s\n", i, argv[i]);
  }
}
-------------------------- print_arg.c ------------------------------

------------------------ create_process.c ---------------------------
#include <stdio.h>
#include <windows.h>

int main(int argc, char **argv)
{
  char *args = ".\\print_arg.exe 'a '\\''b c'\\'''";
  DWORD flags = 0;
  STARTUPINFO si;
  PROCESS_INFORMATION pi;

  memset (&si, 0, sizeof (si));
  si.cb = sizeof (si);
  fprintf(stderr, "args = [%s]\n", args);
  if(CreateProcess(0,
                   args, /* command line */
                   NULL, /* security */
                   NULL, /* thread */
                   TRUE, /* inherit handles */
                   flags, /* start flags */
                   NULL, /* env */
                   NULL, /* current directory */
                   &si,
                   &pi)) {
    fprintf(stderr, "CreateProcess success\n");
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
  } else {
    fprintf(stderr, "error: %d\n", GetLastError());
  }
  return 0;
}
------------------------ create_process.c ---------------------------

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/