-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.c
58 lines (45 loc) · 1.06 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "tests.h"
extern test_fn tests[];
static int stdin_backup = -1;
void stdin_file_create (char* buf) {
size_t len = strlen(buf);
stdin_backup = dup(0);
if (-1 == stdin_backup) {
abort();
}
int file = open("/tmp/input", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);
if (-1 == file) {
abort();
}
if ((size_t) write(file, buf, len) != len) {
abort();
}
if (lseek(file, 0, SEEK_SET) != 0) {
abort();
}
dup2(file, 0);
close(file);
}
void stdin_file_destroy (void) {
if (stdin_backup != -1) {
close(0);
dup2(stdin_backup, 0);
close(stdin_backup);
stdin_backup = -1;
}
}
int main (int argc, char** argv) {
(void) argc;
(void) argv;
for (unsigned int i = 0; tests[i]; ++i) {
printf("Running test #%u\n", i + 1);
if (tests[i]() != TEST_SUCCESS) {
abort();
}
}
}