basic/errno-list: add helper to print errno names
This commit is contained in:
+10
-1
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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]){})
|
||||
|
||||
Reference in New Issue
Block a user