Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a SIGINT from Ctrl+C

alright, so i'm using a sighandler to interpret some signal, for this purpose it is Ctrl+C, so when Ctrl+C is typed some action will be taken, and everything is fine and dandy, but what I really need is for this to happen without ^C appearing in the input/output

for example let's say i have this code

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void siginthandler(int param)
{
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}

the output would be

^CUser pressed Ctrl+C

How could I get this to simply be

User pressed Ctrl+C?

like image 834
josh simmonns Avatar asked Nov 17 '10 03:11

josh simmonns


2 Answers

You can achieve this effect using the curses library's "noecho()" call -- but you must be willing to accept (and/or use) curses terminal handling too ...

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include <curses.h>

void siginthandler(int param)
{
  endwin();
  printf("User pressed Ctrl+C\n");
  exit(1);
}

int main()
{
  initscr();
      noecho();
  signal(SIGINT, siginthandler);
  while(1);
  return 0;
}
like image 115
Kamal Avatar answered Oct 23 '22 09:10

Kamal


That is most likely not your program doing that. It's likely to be the terminal drivers, in which case you should look into stty with the ctlecho or echoctl options.

You can use stty -a to get all the current settings for your terminal device. For a programmatic interface to these items, termios is the way to go.

like image 42
paxdiablo Avatar answered Oct 23 '22 08:10

paxdiablo