summaryrefslogtreecommitdiff
path: root/src/strerrno.c
blob: 39391d55e5722b54f00b8bf2ab0e6d34756c7cb3 (plain)
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
/* SPDX-License-Identifier: GPL-3.0-only */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include <strerrno.h>

#define STRERRNO_TLS_BUFSIZ	128
__thread char tls_strerrno_buf[STRERRNO_TLS_BUFSIZ];

const char *strerrno(void)
{
	snprintf(tls_strerrno_buf, sizeof(tls_strerrno_buf), "%s", "strerror_r error");
	strerror_r(errno, tls_strerrno_buf, sizeof(tls_strerrno_buf));
	return tls_strerrno_buf;
}

#define PRIV_ERR_BUFSIZ	(1 << 12)
static char priv_err_buf[PRIV_ERR_BUFSIZ], internal[PRIV_ERR_BUFSIZ];

void priv_set_err(const char *fmt, ...)
{
	va_list va;

	/* arguments may contains priv_err_buf. Thus, we build the
	 * string in a internal buffer, and then copy it to
	 * priv_err_buf. */
        memset(internal, 0, sizeof(internal));
        va_start(va, fmt);
        vsnprintf(internal, sizeof(internal), fmt, va);
        va_end(va);	

	snprintf(priv_err_buf, sizeof(priv_err_buf), "%s", internal);
}

const char *priv_get_err()
{
	return priv_err_buf;
}