Search code examples
linuxfreepascallazarus

How to handle SIGINT in a program using CRT unit?


I have a console application (program) which uses the CRT unit. Under Linux, when I press CTRL+C, nothing happens. However, I want to handle this SIGINT signal as one would expect (by quitting the program immediately). I found some notice that using CRT re-defines STDIN and STDOUT stuff, so that the normal SIG handlers don't work. Currently, I tried fpSignal(SIGINT, SignalHandler(@DoSig));, but still pressing CTRL+C doesn't do anything. When I send a SIGINT to the process manually (via htop), the handler kicks in. But not with CTRL+C. Not using CRT unit is not an option, unfortunately.

Any help is greatly appreciated :)


Solution

  • The reason is that CRT unit will redirect standard input/output handles, so you have to check for SIGINT "by hand", signal handler will not work. You should check for key pressed with KeyPressed function and then check with ReadKey if SIGINT (eg: Ctrl-C) has been sent.