diff options
Diffstat (limited to 'src/strerrno.c')
-rw-r--r-- | src/strerrno.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/strerrno.c b/src/strerrno.c new file mode 100644 index 0000000..39391d5 --- /dev/null +++ b/src/strerrno.c @@ -0,0 +1,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; +} |