basic/errno-list: add helper to print errno names

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-07-01 17:51:49 +02:00
parent 2d39c99fd8
commit 6020ca2c23
3 changed files with 39 additions and 1 deletions
+10 -1
View File
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errno-list.h"
@@ -33,7 +35,6 @@ const char* errno_to_name(int id) {
# include "errno-to-name.inc"
const char* errno_to_name(int id) {
if (id < 0)
id = -id;
@@ -43,3 +44,11 @@ const char* errno_to_name(int id) {
return errno_names[id];
}
#endif
const char* errno_name_full(int id, char buf[static ERRNO_NAME_BUF_LEN]) {
const char *a = errno_to_name(id);
if (a)
return a;
snprintf(buf, ERRNO_NAME_BUF_LEN, "%d", abs(id));
return buf;
}
+7
View File
@@ -9,3 +9,10 @@ int errno_from_name(const char *name) _pure_;
static inline bool errno_is_valid(int n) {
return n > 0 && n <= ERRNO_MAX;
}
#define ERRNO_NAME_BUF_LEN DECIMAL_STR_MAX(int)
/* Like errno_name, but always returns a string. */
const char* errno_name_full(int id, char buf[static ERRNO_NAME_BUF_LEN]);
/* A helper to print the errno "name" or number if name is not defined. */
#define ERRNO_NAME_FULL(errnum) errno_name_full(errnum, (char[ERRNO_NAME_BUF_LEN]){})