-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorHandling.c
More file actions
71 lines (58 loc) · 1.61 KB
/
errorHandling.c
File metadata and controls
71 lines (58 loc) · 1.61 KB
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
71
#include <stdbool.h>
#include <errno.h>
#include <malloc.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include "errorHandling.h"
const int DEFAULT_ERR_STR_SIZE = 1024;
struct result errorFromConstString(const char *e) {
struct result err;
err.ok = false;
err.error = (char *) e;
err.free = false;
return err;
}
struct result okResult() {
struct result ok;
ok.ok = true;
ok.error = NULL;
ok.free = false;
return ok;
}
struct result errorFromErrno() {
int e = errno;
struct result err;
err.ok = false;
err.free = true;
size_t errSize = sizeof(char) * DEFAULT_ERR_STR_SIZE;
err.error = malloc(errSize);
// Double the buffer until it fits the error.
while (true) {
// Check to see if malloc or realloc failed
if (err.error == NULL) {
return errorFromConstString("Out of memory");
}
//Retrieve error string.
int r = strerror_r(e, err.error, errSize);
//Earlier versions of glib would return the error in errno and return -1.
r = (r == -1 ? errno : r);
if (r == 0) {
return err;
} else if (r == EINVAL) {
e = r;
} else if (r == ERANGE) {
errSize *= 2;
err.error = realloc(err.error, errSize);
}
}
}
void cleanupError(struct result err) {
if (err.free) free(err.error);
}
void exitIfError(struct result result) {
if (result.ok) return;
fprintf(stderr, "%s\n", result.error);
cleanupError(result);
exit(1);
}