Ctrl C catcher

Ctrl C catcher

Post by Kaz Kylhe » Sun, 18 Mar 2001 16:46:21




>I'm porting a program from WIN32 to linux and came across the function
>'SetConsoleCtrlHandler'.  This function 'catches' to Ctrl C button and
>instead of breaking the program, it gets rerouted to a function. (--> very
>handy if you are doing tests and want to finish the current test before
>exiting)

>My question now is: how can you catch the Ctrl C being pressed, so that it
>gets rerouted to a function instead of ending the program?

Ther is an ANSI C method for doing this; namely set up a signal handler
for the SIGINT signal. It's not guaranteed that the SIGINT signal is
generated in response to the interrupt key, but it does work on UNIX
platforms as well as MS Visual C++.  For utmost portability, have your
signal handler store a value to a static object of type volatile
sig_atomic_t. Your mainline can inspect the value of that object from
time to time to discover whether the interrupt key has been pressed.
 
 
 

Ctrl C catcher

Post by Arthur H. Gol » Sat, 17 Mar 2001 18:57:27



> I'm porting a program from WIN32 to linux and came across the function
> 'SetConsoleCtrlHandler'.  This function 'catches' to Ctrl C button and
> instead of breaking the program, it gets rerouted to a function. (--> very
> handy if you are doing tests and want to finish the current test before
> exiting)

> My question now is: how can you catch the Ctrl C being pressed, so that it
> gets rerouted to a function instead of ending the program?

> Thanks
> Ludwig

man sigaction
man signal

You want to set a signal handler for SIGINT.

HTH,
--ag
--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account
for more info)

--
Clone Bernie!

 
 
 

Ctrl C catcher

Post by Ludwi » Sat, 17 Mar 2001 18:49:14


I'm porting a program from WIN32 to linux and came across the function
'SetConsoleCtrlHandler'.  This function 'catches' to Ctrl C button and
instead of breaking the program, it gets rerouted to a function. (--> very
handy if you are doing tests and want to finish the current test before
exiting)

My question now is: how can you catch the Ctrl C being pressed, so that it
gets rerouted to a function instead of ending the program?

Thanks
Ludwig

 
 
 

1. Problems with signal (SIGFPE, Catcher);


This question should clearly have been directed to comp.unix.programmer.
I have sent followups there.

(This code lacks a return value from main().)

The signal is not being caught because it is not being delivered.
IEEE floating point implementations support the notion that 1 divided
by 0 gives the result `plus infinity'.  Independent of this, IEEE 754
says that the system should provide some way for you to trap divide
by zero errors.  Many systems do this with a `trap enable mask'.  IEEE
defines five exceptions:

        invalid operand
        overflow
        underflow
        divide by zero
        inexact result

Each floating point operation sets a `current exceptions' and adds any
new ones to the set of `accrued exceptions'.  If the current exception(s)
is/are enabled under the trap enable mask, the system should cause a
trap.  On a Unix box, such a trap should translate to a SIGFPE.

If your system conforms to the standard, it will provide some way to
set up all of these things.  This is, however, far out of the realm
of comp.lang.c; it even depends heavily on which Unix system you have.
The question cannot be answered without more information.
--
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)

2. "sensed a recovered error"

3. Why don't ctrl c and ctrl z work on OS5???

4. 5.0.2 enterprise and IBM Token-ring problem

5. disable Ctrl Z and Ctrl C

6. telnet session terminates after a certain time idle

7. Trapping CTRL-C and CTRL-Z

8. enlightenment user menus

9. Help with Term (ctrl-Z, and ctrl-c don't work!)

10. How to catch keys like CTRL+LEFT, CTRL+HOME etc. in Linux?

11. blocking CTRL-Z and CTRL-C

12. Why does CTRL-C on CTRL-Z kill my shell and log me out?

13. Disabling CTRL-C and CTRL-Z from apps