From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: SIGINT handling & stdio Date: Thu, 12 Dec 1996 19:46:51 -0800 Organization: Three pounds of chaos and a pinch of salt Lines: 52 Message-ID: <32B0D1AB.40E4@cs.com> References: Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp211.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: Marc Feeley DJ-Gateway: from newsgroup comp.os.msdos.djgpp Marc Feeley wrote: > > I want to port a program that uses stdio and handles ctrl-c interrupts > with signal(SIGINT,...). The program below is a small example. It > seems that when getchar is called, a ctrl-c will NOT call the signal > handler; the program simply terminates. This is strange because if I > replace getchar with getkey then the program works fine. > Unfortunately I can't use getkey in my program because the input is > not necessarily from the console (it might be a redirection). What are you running this program under? In DOS with CWSDPMI it works perfectly for me. Well, not exactly _perfectly_, but at least it works as written. Some comments: > void user_signal_handler (void) { intr = 1; } This is defined wrong. ANSI signal handlers take an int as an argument. gcc gives me the following warning: sigint.c: In function `main': sigint.c:11: warning: passing arg 2 of `signal' from incompatible pointer type > void main (void) This is wrong, too. main() MUST return an int! It doesn't matter how small your program is; you cannot omit this. > { > int c; > signal (SIGINT, user_signal_handler); > while ((c=getchar()) != 'q') > { > if (intr) { printf ("interrupt\n"); intr = 0; } > printf ("got %d\n", c); > } > } -- John M. Aldrich * Anything that happens, happens. * Anything that, in happening, causes something else to happen, causes something else to happen. * Anything that, in happening, causes itself to happen again, happens again. * It doesn't necessarily do it in chronological order, though. --- Douglas Adams