@@ -154,4 +154,12 @@ static inline const char *kbasename(const char *path)
return tail ? tail + 1 : path;
}
+#ifdef CONFIG_STRERROR
+extern const char *strerror(int error);
+#endif
+
+#ifdef CONFIG_STRERROR_NAME
+extern const char *strerror_name(int error);
+#endif
+
#endif /* _LINUX_STRING_H_ */
@@ -27,6 +27,10 @@
#include <linux/bug.h>
#include <linux/errno.h>
+#if defined(CONFIG_STRERROR) || defined(CONFIG_STRERROR_NAME)
+# include <generated/error_strings.h>
+#endif
+
#ifndef __HAVE_ARCH_STRNICMP
/**
* strnicmp - Case insensitive, length-limited string comparison
@@ -824,3 +828,59 @@ void *memchr_inv(const void *start, int c, size_t bytes)
return check_bytes8(start, value, bytes % 8);
}
EXPORT_SYMBOL(memchr_inv);
+
+#if defined(CONFIG_STRERROR) || defined(CONFIG_STRERROR_NAME)
+static const char *_strerror(int error, unsigned index)
+{
+ unsigned uerror = error < 0 ? -error : error;
+ const struct error_strings *es;
+ const struct error_strings *es_end = &error_strings[
+ sizeof(error_strings) / sizeof(*error_strings)];
+ const char *ret;
+ unsigned i;
+
+ for (es = error_strings; es != es_end; ++es) {
+ if (uerror >= es->first && uerror <= es->last)
+ break;
+ }
+
+ if (es == es_end)
+ return NULL;
+
+ for (i = es->first, ret = es->desc[index]; i < uerror; ++ret)
+ i += !*ret;
+
+ BUG_ON(i > es->last);
+ BUG_ON(i != uerror);
+
+ return *ret ? ret : NULL;
+}
+#endif /* defined(CONFIG_STRERROR) || defined(CONFIG_STRERROR_NAME) */
+
+#ifdef CONFIG_STRERROR
+/**
+ * strerror - Translates an error code into a description.
+ * @error: The error to describe (may be positive or negative)
+ *
+ * Returns a pointer to the error description or NULL if none is found.
+ */
+const char *strerror(int error)
+{
+ return _strerror(error, 1);
+}
+EXPORT_SYMBOL(strerror);
+#endif
+
+#ifdef CONFIG_STRERROR_NAME
+/**
+ * strerror_name - Translates an error code into a name
+ * @error: The error to describe (may be positive or negative)
+ *
+ * Returns a pointer to the error name or NULL if none is found.
+ */
+const char *strerror_name(int error)
+{
+ return _strerror(error, 0);
+}
+EXPORT_SYMBOL(strerror_name);
+#endif
Signed-off-by: Daniel Santos <daniel.santos@pobox.com> --- include/linux/string.h | 8 +++++++ lib/string.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)