-
Notifications
You must be signed in to change notification settings - Fork 307
/
10.22.c
48 lines (35 loc) · 805 Bytes
/
10.22.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define BUFFSIZE 1024
static void sig_tstp(int);
int main(void)
{
int n;
char buf[BUFFSIZE];
if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
signal(SIGTSTP, sig_tstp);
}
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write error\n");
exit(-1);
}
}
if (n < 0) {
printf("read error\n");
exit(-1);
}
return 0;
}
static void sig_tstp(int signo)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
signal(SIGTSTP, SIG_DFL);
kill(getpid(), SIGTSTP);
signal(SIGTSTP, sig_tstp);
}