-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror.c
More file actions
58 lines (45 loc) · 1.46 KB
/
error.c
File metadata and controls
58 lines (45 loc) · 1.46 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
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include "conf.h"
#include "error.h"
/* error string handling */
void error_reset(error_t *error) {
memset(error->strerror, 0, sizeof(error->strerror));
}
char *error_get(error_t *error) {
return error->strerror;
}
void error_set(error_t *error, char *str) {
strncpy(error->strerror, str, sizeof(error->strerror) - 1);
error->strerror[sizeof(error->strerror) - 1] = '\0';
}
void error_printf(error_t *error, const char *format, ...) {
va_list ap;
va_start(ap, format);
vsnprintf(error->strerror, sizeof(error->strerror), format, ap);
va_end(ap);
}
void error_set_strerror(error_t *error, char *str) {
snprintf(error->strerror, sizeof(error->strerror), "%s: %s", str, strerror(errno));
}
void error_printf_strerror(error_t *error, const char *format, ...) {
va_list ap;
va_start(ap, format);
vsnprintf(error->strerror, sizeof(error->strerror), format, ap);
error_append(error, strerror(errno));
va_end(ap);
}
void error_append(error_t *error, char *str) {
unsigned char buf[4096];
strncpy(buf, error->strerror, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
snprintf(error->strerror, sizeof(error->strerror), "%s: %s", buf, str);
}
void error_prepend(error_t *error, char *str) {
unsigned char buf[4096];
strncpy(buf, error->strerror, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
snprintf(error->strerror, sizeof(error->strerror), "%s: %s", str, buf);
}