www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/16/15:29:05

From: alaric AT oasis DOT novia DOT net (Alaric Dailey)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: catching a CTRL C
Date: 16 Jan 1997 09:54:31 GMT
Organization: Novia Internetworking <> 28.8kbps dialup; 402/390-2NET
Lines: 50
Message-ID: <5bktsn$dps@nntp.novia.net>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 970112160124 DOT 7675f-100000 AT is>
NNTP-Posting-Host: oasis.novia.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019