-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_writer.c
42 lines (33 loc) · 1.03 KB
/
file_writer.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
#include "file_writer.h"
#define NUM_CHARS 64
int keepWriting = 1;
void writer_handler(int sig) {
keepWriting = 0;
syslog(LOG_INFO, "Writer stopped by signal %s", strsignal(sig));
}
void file_writer(int read_pipe, int file_desc) {
ssize_t bytesToWrite;
char str[NUM_CHARS];
int numWritten = 10;
struct sigaction int_handler;
int_handler.sa_handler = &writer_handler;
sigaction(SIGINT, &int_handler, NULL);
while (keepWriting) {
bytesToWrite = read(read_pipe, str, sizeof(char) * NUM_CHARS);
if (bytesToWrite == 0) {
syslog(LOG_INFO, "Read EOF from pipe");
break;
}
if (write(file_desc, str, bytesToWrite) == -1) {
syslog(LOG_CRIT, "write: %m");
break;
}
if (--numWritten == 0) {
write(file_desc, "\n", sizeof("\n"));
numWritten = 10;
}
}
write(file_desc, "\n\n", sizeof("\n\n"));
syslog(LOG_INFO, "Writer finished, closing read end of pipe");
close(read_pipe);
}