Thread: Replacing (Ctrl-C) signal with (Ctrl-D)

  1. #1
    Registered User
    Join Date
    Sep 2003

    Replacing (Ctrl-C) signal with (Ctrl-D)

    I want to have my program ignore the (Ctrl-C) signal and instead should terminate on (Ctrl-D).
    How can i do that?

  2. #2
    Join Date
    Sep 2003
    Ctrl-C is usually SIGINT, so all you need to ignore it.
    struct sigaction sac;
    if (sigaction(SIGINT, NULL, &sac) == -1)
            perror("Couldn't get old handler");
    else {
            sac.sa_handler = SIG_IGN;
            if (sigaction(SIGINT, &sac, NULL) == -1)
                    perror("Couldn't ignore SIGINT");
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    And ctrl-D generates EOF on the input stream.
    Aren't you glad you learnt how to use fgets

    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        // do stuff
    if ( feof(stdin) ) {
        // user pressed ctrl-D
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 10:05 AM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  3. Signal question
    By fnoyan in forum Linux Programming
    Replies: 2
    Last Post: 06-01-2006, 05:46 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM