-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.c
More file actions
88 lines (71 loc) · 1.46 KB
/
common.c
File metadata and controls
88 lines (71 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: MIT */
#include "common.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
int is_debug_enabled = 0;
static char *progname = NULL;
static int use_prefix = 0;
static int use_func = 0;
void
progname_set(char *name)
{
progname = name;
}
const char *
progname_get(void)
{
return progname;
}
void
enable_debug(void)
{
is_debug_enabled = 1;
}
static void
vaerr(const char *prefix, const char *func, const char *errstr, va_list ap)
{
if (progname != NULL)
fprintf(stderr, "%s: ", progname);
if (use_prefix && prefix != NULL)
fprintf(stderr, "%s: ", prefix);
if (use_func && func != NULL)
fprintf(stderr, "%s: ", func);
vfprintf(stderr, errstr, ap);
int len = strlen(errstr);
if (len > 0) {
char last = errstr[len - 1];
if (last == ':')
fprintf(stderr, " %s\n", strerror(errno));
else if (last != '\n' && last != '\r')
fprintf(stderr, "\n");
}
}
void
verr(const char *prefix, const char *func, const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vaerr(prefix, func, errstr, ap);
va_end(ap);
}
void
vdie(const char *prefix, const char *func, const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vaerr(prefix, func, errstr, ap);
va_end(ap);
abort();
}
void *
safe_calloc(size_t nmemb, size_t size)
{
void *p = calloc(nmemb, size);
if (p == NULL)
die("calloc failed:");
return p;
}