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 press开发者_C百科ed 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
?
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;
}
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.
As other have said, this kind of thing falls under the control of your terminal driver and is typically done using the curses library. That said, here is a hack that will probably get you on your way:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void siginthandler(int param) {
printf("User pressed Ctrl+C\n");
system("stty echo");
exit(1);
}
int main() {
system("stty -echo");
signal(SIGINT, siginthandler);
while(1);
return 0;
}
精彩评论