Web lists-archives.org

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




>  My console code behaves differently when running in a DOS box

Please don't say "DOS box" if you actually mean cmd.exe (or some other
shell) is a console window. (MS-)DOS has nothing to do with it.

>  and when running on Cygwin's bash.

Well, this is not the Cygwin mailing list. But if your program is not
a Cygwin program, but a native Win32 one, built using mingw, I guess
your question isn't off-topic.

Is this Cygwin bash running in rxvt or a console window? That might
explain the difference in behaviour.

>  Can anyone suggest a way to find out what a
>  program is running under?

Finding out information about the parent process is hard, but that
isn't really what you want.

Finding out if you are running in rxvt or a normal console (even if
there is a Cygwin bash running in that console as the parent process)
can be done by finding out if the standard input of the process is a
console device. Sample code below, seems to work. You can do other
variations on the theme.

--tml

#include <stdio.h>
#include <windows.h>

static char *
error_message (DWORD error)
{
  char *msg;

  FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
		 |FORMAT_MESSAGE_IGNORE_INSERTS
		 |FORMAT_MESSAGE_FROM_SYSTEM,
		 NULL, error, 0,
		 (char *) &msg, 0, NULL);

  return msg;
}

int
main (int argc, char **argv)
{
  HANDLE handle;
  DWORD type;

  handle = GetStdHandle (STD_INPUT_HANDLE);
  if (handle == INVALID_HANDLE_VALUE)
    {
      fprintf (stderr, "No standard input: %s\n", error_message
(GetLastError ()));
      exit (1);
    }

  type = GetFileType (handle);
  if (type == FILE_TYPE_UNKNOWN)
    {
      if (GetLastError () != NO_ERROR)
	fprintf (stderr, "Can't find out type of standard input: %s\n",
error_message (GetLastError ()));
      else
	fprintf (stderr, "Standard input is of unknown type\n");
      exit (1);
    }

  if (type == FILE_TYPE_CHAR)
    {
      DWORD mode;

      if (GetConsoleMode (handle, &mode))
	{
	  /* It truly is a console. Be silent, return success */
	  exit (0);
	}

      fprintf (stderr, "Standard input is not a console: %s\n",
error_message (GetLastError ()));
      exit (1);
    }

  fprintf (stderr, "Standard input is not a character device\n");
  exit (1);
}

-------------------------------------------------------------------------
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