blob: f38c44efecafe03dff9d770754ba6b67e67fcf20 (
plain) (
blame)
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
|
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef TEST
#include <assert.h>
#endif
static const char *const default_db_name = "fallible-default";
bool fallible_should_fail(const char *const filename, int lineno) {
static const char *db_name = NULL;
if (!db_name && !(db_name = getenv("FALLIBLE_FILE"))) {
db_name = default_db_name;
}
FILE *f = fopen(db_name, "a+");
if (!f) {
fprintf(stderr, "Failed to open '%s', exitting with error code 1.\n",
db_name);
exit(1);
}
int n = lineno;
int digits = 0;
while (n) {
digits++;
n /= 10;
}
char *current = malloc(strlen(filename) + 1 + digits + 1 + 1);
sprintf(current, "%s:%d\n", filename, lineno);
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, f) != -1) {
if (!strcmp(line, current)) {
free(current);
free(line);
fclose(f);
return false;
}
}
fprintf(f, "%s", current);
free(current);
free(line);
fclose(f);
return true;
}
|