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]

Ctrl-C and non-cygwin programs


I'm experiencing strange behaviour with Ctrl-C with non-cygwin programs started from Cygwin bash.

Take the following program:

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

static BOOL WINAPI handler(DWORD dwCtrlType)
{
    switch (dwCtrlType) {
    case CTRL_C_EVENT:
    case CTRL_BREAK_EVENT:
        printf("caught!\n");
        return TRUE;
    }
    return FALSE;
}

int main(int argc, char *argv[])
{
    if ( !SetConsoleCtrlHandler(handler, TRUE) ) {
        exit(1);
    }
    while (1) {
        Sleep(1000);
        printf("tick\n");
        fflush(stdout);
    }
}
============================

Compile it as follows:

 $ gcc -mno-cygwin ctrlc.c -o ctrlc

(or use MinGW gcc directly, or use MS VC++, in other words generate a plain Win32 non-cygwin executable).

Then run the program, hit Ctrl-C and see what happens.  The behaviour differs depending on the environment:

  * In a Cygwin shell started from cygwin.bat, with the CYGWIN
    environment variable empty: correct behaviour, Ctrl-C is caught
    and handled.

  * In a Cgwin shell started from cygwin.bat, with CYGWIN=tty:
    Ctrl-C apparently just kills the child process, the event is
    not caught.

  * In an xterm (Cygwin), with Cygwin bash: again, the child process
    is just killed.

  * In a CMD.EXE shell, Ctrl-C is caught and handled.

I would like the behaviour to be that the program always gets to handle the Ctrl-C event and doesn't get killed, which seems reasonable.  The application I'm interested in (GHC, http://www.haskell.org/ghc) needs to be able to catch Ctrl-C and clean up, and I'd like this to work when I run GHC from a Cygwin bash shell or an xterm.  GHC is not a Cygwin app, it's built against MinGW.

There have been various reports of similar problems in the past on this mailing list, and apparently fixes have been checked in, but the current Cygwin still has the above behaviour.  Here are some previous threads:

http://www.cygwin.com/ml/cygwin/2003-11/msg00192.html
http://www.cygwin.com/ml/cygwin/2002-01/msg00347.html

I'll be happy to help with any further diagnosis.  I also tried the latest cygwin1.dll snapshot (20061127), but that didn't help.

Thanks!

Simon

Attachment: cygcheck.out
Description: cygcheck.out

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

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