Mail Archives: djgpp/1996/12/13/09:57:37
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 <fighteer AT cs DOT com>
* 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
- Raw text -