Mail Archives: djgpp/1997/01/16/15:29:05
ok everyone, I would like to thank you for all your help the code now
works under djgpp C and C++ here is the corrected code for anyone who
might want it the purpose is simple catch a Control-C or Control-Break and
redirect it to run your clean up function. This is good for reseting
graphics modes, cleaning up memory and many other things you might want to
cleanup, also you can make it so they won't inteefere with your code at
all so here it is.
#include <signal.h> // required for the ctrlbrk function
#include <stdio.h>
void ctrlbrk(void (*fptr)(int))
{
signal (SIGINT, fptr);
}
/*
* you can name this anything you want so long as it takes an int and
* returns a void. If you don't do anything with the int you will get
* a compiler warning
*/
void terminate (int signo)
{
// put whatever you want for cleanup code here
fprintf (stderr,"Exiting, due to signal %d caught\n",signo);
exit(0); //this must be here if you wan't it exit the program.
}
int main(void)
{
ctrlbrk(terminate); /* now that you have called it you can forget
* about it when a ^C or ^Break is hit
* your cleanup function will be called!
*/
while (1)
puts("Hit a Control-Break or Control-C to stop this loop!");
}
Thanx to everyone who helped, especially Eli! :-)
####################################################
We go left, right, left, right, left-
They're left in the right again.
We go right, left, right, left, right-
We've no rights left anymore.
-Martin Walkyier (Skyclad)
- Raw text -