-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
70 lines (57 loc) · 1.14 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <signal.h>
#define panic(emsg) fprintf(stderr, "[ERROR]: " emsg);\
return 1
enum {
USER_PORT = 1,
USER_LOG_FILE,
USER_IN_COUNT,
};
int port;
void terminator(int sig) {
close(port);
}
uint8_t main(int argc, char *argv[]) {
FILE *fout;
struct pollfd pfd;
int ready;
ssize_t size;
char buf[256];
if(argc < USER_IN_COUNT) {
panic("please specify the port\r\n");
}
signal(SIGINT, terminator);
fout = fopen(argv[USER_LOG_FILE], "w+");
if(fout == NULL) {
panic("Couldn't open outputfile\r\n");
}
port = open(argv[USER_PORT], O_RDONLY);
if(port == -1) {
panic("Couldn't open port\r\n");
}
pfd.fd = port;
pfd.events = POLLIN;
while(1) {
ready = poll(&pfd, 1, -1);
if(ready == -1)
break;
if(pfd.revents & POLLIN) {
size = read(pfd.fd, buf, sizeof(buf));
if (size == -1)
break;
fprintf(stderr, "\tread %zd bytes\n", size);
fprintf(fout, "%.*s", (int)size, buf);
} else {
if(close(pfd.fd) == -1) {
panic("Error closing");
}
}
}
close(pfd.fd);
panic("Polling error\r\n");
return 0;
}