Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!wuarchive!uunet!ccicpg!cci632!cpp From: cpp@cci632.UUCP (Carl P. Petito) Newsgroups: comp.os.msdos.programmer Subject: Aborting program with ^C Keywords: signal, ^C Message-ID: <39616@cci632.UUCP> Date: 2 Sep 90 17:18:42 GMT Distribution: usa Organization: Computer Consoles Inc. An STC Company, Rochester NY Lines: 90 I have a program that I would like to be able to terminate from the keyboard. QC2.5 has an example using 'signal' that I used; however, it appears that the only time ^C is effective is when the program is doing i/o to the console. To confirm this, I modified the example program as shown below, and I found that if I hit ^C nothing happens until the string starts to print; I get the first two characters followed by ^C. What's worse, if output is redirected to a file, ^C doesn't do anything while the program is running. What am I doing wrong? Any pointers would be appreciated. By the the way I'm using MS-DOS 3.3. Carl Petito #include #include #include #include #include #include #include #include #include void ctrlchandler( void ); /* Prototypes */ void safeout( char *str ); int safein( void ); void main() { long i; /* Modify CTRL+C behavior. */ if( signal( SIGINT, ctrlchandler ) == SIG_ERR ) { fprintf( stderr, "Couldn't set SIGINT\n" ); abort(); } /* loop illustrates results. */ for (i = 0; i < 999999l; i++) ; printf("First loop done\n"); for (i = 0; i < 999999l; i++) ; printf("Second loop done\n"); } /* Handles SIGINT (CTRL+C) interrupt. */ void ctrlchandler() { int ch; /* Disallow CTRL+C during handler. */ signal( SIGINT, SIG_IGN ); safeout( "Abort processing? " ); ch = safein(); safeout( "\r\n" ); if( (ch == 'y') || (ch == 'Y') ) abort(); else /* The CTRL+C interrupt must be reset to our handler since by * default it is reset to the system handler. */ signal( SIGINT, ctrlchandler ); } /* Outputs a string using system level calls. */ void safeout( char *str ) { union REGS inregs, outregs; inregs.h.ah = 0x0e; while( *str ) { inregs.h.al = *str++; int86( 0x10, &inregs, &outregs ); } } /* Inputs a character using system level calls. */ int safein() { return (_bios_keybrd( _KEYBRD_READ ) & 0xff ); }