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

This is a discussion on Replacing (Ctrl-C) signal with (Ctrl-D) within the Linux Programming forums, part of the Platform Specific Boards category; I want to have my program ignore the (Ctrl-C) signal and instead should terminate on (Ctrl-D). How can i do ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    38

    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
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    Ctrl-C is usually SIGINT, so all you need to ignore it.
    Code:
    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
    Location
    The edge of the known universe
    Posts
    32,824
    And ctrl-D generates EOF on the input stream.
    Aren't you glad you learnt how to use fgets

    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-07-2009, 11: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, 06: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, 03:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21