From 9aea93214a2ed4ab674e1809be1eb07631ad6939 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Thu, 19 Oct 2023 22:13:09 -0400 Subject: [PATCH] errno: add strerror function --- include/errno.h | 9 +-------- lib/errno.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 lib/errno.c diff --git a/include/errno.h b/include/errno.h index bf4b564..d000d02 100644 --- a/include/errno.h +++ b/include/errno.h @@ -1,5 +1,4 @@ /* SPDX-License-Identifier: GPL-2.0-only */ - #ifndef _ERRNO_H_ #define _ERRNO_H_ @@ -8,12 +7,6 @@ #define EINVAL 3 #define EBADF 4 -static const char *errno_str[] = { - [0] = "Success", - [ENOMEM] = "Out of memory", - [ENOENT] = "No such file or directory", - [EINVAL] = "Invalid argument", - [EBADF] = "Bad file descriptor", -}; +const char *strerror(int errnum); #endif /* _ERRNO_H_ */ diff --git a/lib/errno.c b/lib/errno.c new file mode 100644 index 0000000..8e3994d --- /dev/null +++ b/lib/errno.c @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +static const char *errno_str[] = { + [0] = "Success", + [ENOMEM] = "Out of memory", + [ENOENT] = "No such file or directory", + [EINVAL] = "Invalid argument", + [EBADF] = "Bad file descriptor", +}; + +inline const char *strerror(int errnum) +{ + if (errnum < 0 || (size_t)errnum >= ARRAY_SIZE(errno_str)) + return "Unknown error"; + + return errno_str[errnum]; +}