我想在程序中捕获Ctrl+ D信号并为其编写信号处理程序.我怎样才能做到这一点?我正在使用C系统并使用Linux系统.
Pas*_*uoq 78
正如其他人已经说过的那样,处理Control+ D,处理"文件结束".
Control+ D是用户和您看作stdin的伪文件之间的一段通信.它并不意味着具体的"文件结束",但更一般地说是"刷新我到目前为止输入的输入".read()
刷新意味着程序中对stdin的任何调用都会返回自上次刷新以来输入的输入长度.如果该行是非空的,则输入可用于您的程序,尽管用户尚未输入"return".如果该行为空,则read()
返回零,并将其解释为"文件结束".
因此,当使用Control+ D来结束程序时,它只能在一行的开头工作,或者如果你做两次(第一次刷新,第二次read()
返回零).
试试吧:
$ cat
foo
(type Control-D once)
foofoo (read has returned "foo")
(type Control-D again)
$
Run Code Online (Sandbox Code Playgroud)
Eti*_*mps 25
Ctrl+ D不是信号,它是EOF(文件结束).它关闭了stdin管道.如果read(STDIN)返回0,则意味着stdin关闭,这意味着Ctrl+ D被击中(假设管道的另一端有一个键盘).
sam*_*wry 14
一个简约的例子:
#include <unistd.h>
#include <stdio.h>
#include <termios.h>
#include <signal.h>
void sig_hnd(int sig){ (void)sig; printf("(VINTR)"); }
int main(){
setvbuf(stdout,NULL,_IONBF,0);
struct termios old_termios, new_termios;
tcgetattr(0,&old_termios);
signal( SIGINT, sig_hnd );
new_termios = old_termios;
new_termios.c_cc[VEOF] = 3; // ^C
new_termios.c_cc[VINTR] = 4; // ^D
tcsetattr(0,TCSANOW,&new_termios);
char line[256]; int len;
do{
len=read(0,line,256); line[len]='\0';
if( len <0 ) printf("(len: %i)",len);
if( len==0 ) printf("(VEOF)");
if( len >0 ){
if( line[len-1] == 10 ) printf("(line:'%.*s')\n",len-1,line);
if( line[len-1] != 10 ) printf("(partial line:'%s')",line);
}
}while( line[0] != 'q' );
tcsetattr(0,TCSANOW,&old_termios);
}
Run Code Online (Sandbox Code Playgroud)
程序将VEOF字符(从Ctrl-D)更改为Ctrl-C,将VINTR字符(从Ctrl-C)更改为Ctrl-D.如果按Ctrl-D,则终端驱动程序会将SIGINT发送到程序的信号处理程序.
注意:按下VINTR将擦除终端输入缓冲区,因此在按下VINTR键之前无法读取行中键入的字符.
无需处理信号。
您需要确保未在终端标志上设置ISIG,仅此而已。
这是使用select避免阻塞stdin的完整示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/select.h>
#define STDIN_FILENO 0
struct termios org_opts;
/** Select to check if stdin has pending input */
int pending_input(void) {
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
/** Input terminal mode; save old, setup new */
void setup_terminal(void) {
struct termios new_opts;
tcgetattr(STDIN_FILENO, &org_opts);
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ISIG | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
}
/** Shutdown terminal mode */
void reset_terminal(void) {
tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
}
/** Return next input or -1 if none */
int next_input(void) {
if (!pending_input())
return -1;
int rtn = fgetc(stdin);
printf("Found: %d\n", rtn);
return(rtn);
}
int main()
{
setup_terminal();
printf("Press Q to quit...\n");
for (;;) {
int key = next_input();
if (key != -1) {
if ((key == 113) || (key == 81)) {
printf("\nNormal exit\n");
break;
}
}
}
reset_terminal();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
doug-2:rust-sys-sterm doug$ cc junk.c
doug-2:rust-sys-sterm doug$ ./a.out
Press Q to quit...
Found: 4
Found: 3
Found: 27
Found: 26
Found: 113
Normal exit
Run Code Online (Sandbox Code Playgroud)
注意 3是控制C,4是控制D;26是控制z。113是“ q”。有关完整表格,请参见:http://en.wikipedia.org/wiki/ASCII#ASCII_control_characters。
归档时间: |
|
查看次数: |
74999 次 |
最近记录: |