summaryrefslogtreecommitdiff
path: root/src/strerrno.c
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2024-02-06 21:54:04 +0900
committerRyo Nakamura <upa@haeena.net>2024-02-06 21:54:04 +0900
commit4f0669f8f86acb09f10ffb5af273f86d8d6ddd34 (patch)
tree65d5d0adfd90e31b77474993addeb065edfeb75c /src/strerrno.c
parent76892a69f95f7dcf47050800385bc610f8ccf5f3 (diff)
refactor error message-related functions
split message print fuctions (mpr_*), strerrno, and mscp_get/set_error into print.c/h and strerrno.c/h. ToDo: revise usages of priv_set_errv and pr_* functions.
Diffstat (limited to 'src/strerrno.c')
-rw-r--r--src/strerrno.c41
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;
+}