ch10-catchint.c

来自「linux编程精髓 源代码」· C语言 代码 · 共 45 行

C
45
字号
/* ch10-catchint.c --- catch a SIGINT, at least once. */#include <signal.h>#include <string.h>#include <unistd.h>/* handler --- simple signal handler. */void handler(int signum){	char buf[200], *cp;	int offset;	/* Jump through hoops to avoid fprintf(). */	strcpy(buf, "handler: caught signal ");	cp = buf + strlen(buf);	/* cp points at terminating '\0' */	if (signum > 100)	/* unlikely */		offset = 3;	else if (signum > 10)		offset = 2;	else		offset = 1;	cp += offset;	*cp-- = '\0';		/* terminate string */	while (signum > 0) {	/* work backwards, filling in digits */		*cp-- = (signum % 10) + '0';		signum /= 10;	}	strcat(buf, "\n");	(void) write(2, buf, strlen(buf));}/* main --- set up signal handling and go into infinite loop */int main(void){	(void) signal(SIGINT, handler);	for (;;)		pause();	/* wait for a signal, see later in the chapter */	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?