Web lists-archives.org

Re: [Mingw-users] How can I tell whether my prog is running on cmd.exe or bash?




Earnie Boyd wrote:

> Now, that's where MSYS comes in handy.  I insisted that cygpath was not 
> necessary and MSYS converts the PATH to something useful for a native 
> windows app.  The only thing that comes close is a -W switch to the pwd 
> command.

Sounds good - this is a real pain in Cygwin. Even my Tcl code has to
'exec uname' and special-case Cygwin. I'll give it a go.

> But what does /bin/sh convert to in windows?  You'll come up short 
> unless /bin is on the root of the current drive.  How do you determine 
> where /bin exists?

cygpath :)

I never call CreateProcess(NULL, "/bin/sh -c ...", ...); I don't think 
that would ever find anything to run. If it did, "/bin/sh" almost 
certainly wouldn't be anything I'd want to execute.

This is what I'm actually doing (attached below), for anyone else who 
comes this way; this is tested and working for:

1 - myprog.exe running from a DOS box, no knowledge of Cygwin at all

2 - myprog.exe running from a DOS box, with C:\cygwin\bin added to PATH

3 - myprog.exe running from the basic Cygwin terminal

'execReturnStdOut' is a routine which calls _popen and returns the 
stdout from the child program. There's a simple example in MSDN in the 
'Unix code migration guide' 
(http://msdn2.microsoft.com/en-us/library/ms811896.aspx#ucmgch09_topic10), 
although it needs cleaning up to be usable. _popen(..,"r") redirects 
only stdout, and you also need to capture or dump stderr so that the 
user doesn't see it; this is what the redirection is for. '_popen' uses 
cmd.exe; this works when myprog.exe is run from from Cygwin because the 
new process inherits the Cygwin path. 'execReturnStdOut' returns 0 iff 
the child returned 0 and there was some output from the child.

After 'getWinshell' returns, I always use the return string as the start 
of the second parameter to CreateProcess. It's also necessary to adjust 
the rest of the command line for quotes and slashes.

-Paul

=======================================================

/**
  * Return the (DOS) path to the best shell we can find, together with a 
switch
  * to allow that shell to execute a command. If we can't find a sane 
shell, we
  * return "cmd.exe /c" in 'retval'; if we can find anything better, we 
return
  * that instead ("C:/cygwin/bin/sh -c", for instance).
  *
  * Currently, only a /bin/sh provided by Cygwin counts as sane; we 
revert to
  * cmd.exe if we can't find this. Note that the search is carried out by
  * cmd.exe. We don't have to be running on a Cygwin shell to return 
Cygwin's
  * /bin/sh; it just has to be in the path. This will happen if we're on
  * Cygwin, because the cmd.exe spawned by _popen will pick up Cygwin's 
PATH.
  *
  * @param retval The initial part of a shell exec command string
  * @return       true if we found a sane shell, false if we're using 
cmd.exe
  */
static bool getWinShell(string &retval) {
    char buffer[BUFLEN];
    retval = "cmd.exe /c";                 // set a default return

    // if we can't exec uname, return DOS
    if(execReturnStdOut("uname 2>&1", buffer, BUFLEN))
       return false;

    // if we can't find a case-insensitive 'cygwin' in the uname output, 
return
    // DOS. Windows doesn't appear to have 'strcasecmp'
    for(char *cptr = buffer; *cptr; cptr++)
       *cptr = toupper(*cptr);
    if(strstr(buffer, "CYGWIN") == NULL)
       return false;

    // if we can't exec 'cygpath -m /bin/sh', return DOS. the '-m' gives us
    // forward slashes
    if(execReturnStdOut("cygpath -m /bin/sh 2>&1", buffer, BUFLEN))
       return false;

    // return whatever 'cygpath' returned, without the trailing NL, but 
with a
    // '-c' switch
    for(size_t i=0; i<strlen(buffer); ++i) {
       if(buffer[i] == '\n') {
          buffer[i] = '\0';
          break;
       }
    }
    retval = buffer;
    retval += " -c";
    return true;
}  // getWinShell()

=============================================================

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users