-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigcat.c
52 lines (41 loc) · 999 Bytes
/
sigcat.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
49
50
51
#include "sigcat.h"
#include <csse2310a3.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/** Current output stream for the program. */
FILE* outputStream;
int main() {
char* input;
outputStream = stdout;
set_handlers();
do {
input = read_line(stdin);
if (input == NULL) {
break;
}
fprintf(outputStream, "%s\n", input);
fflush(outputStream);
free(input);
} while (!feof(stdin));
return 0;
}
void set_handlers() {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sa.sa_flags = SA_RESTART;
for (int signum = 1; signum <= 31; signum++) {
sigaction(signum, &sa, 0);
}
}
void handler(int signum) {
fprintf(outputStream, "sigcat received %s\n", strsignal(signum));
fflush(outputStream);
if (signum == 10) {
outputStream = stdout;
} else if (signum == 12) {
outputStream = stderr;
}
}