forked from bsl/send-sds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
err.c
70 lines (56 loc) · 1.11 KB
/
err.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 <stdlib.h>
#include <stdarg.h>
#include "err.h"
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
struct err {
char *buf;
size_t buf_size;
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
err_t
err_create(size_t size)
{
err_t err;
err = malloc(sizeof(*err));
err->buf = malloc(size);
err->buf_size = size;
err->buf[0] = '\0';
return err;
}
void
err_destroy(err_t err)
{
if (err != NULL) {
if (err->buf != NULL) {
free(err->buf);
}
free(err);
}
}
void
err_set(
err_t err,
const char *filename,
const char *functionname,
const int linenum,
const char *format,
...
) {
char buf1[256]; /* XXX should not be fixed size */
va_list ap;
va_start(ap, format);
vsnprintf(buf1, sizeof(buf1), format, ap);
va_end(ap);
snprintf(
err->buf,
err->buf_size,
"%s, %s(), line %d: %s",
filename, functionname, linenum, buf1
);
}
const char *
err_get(err_t err)
{
return (const char *)(err->buf);
}