From: danielfsantos@att.net
To: linux-kbuild <linux-kbuild@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
Michal Marek <mmarek@suse.cz>,
Andrew Morton <akpm@linux-foundation.org>,
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
David Howells <dhowells@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Michael Kerrisk <mtk.manpages@gmail.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
George Spelvin <linux@horizon.com>
Cc: Daniel Santos <daniel.santos@pobox.com>
Subject: [PATCH 4/5] lib: Add strerror and strerror_name functions
Date: Tue, 17 Sep 2013 18:08:36 -0500 [thread overview]
Message-ID: <1379459317-13046-5-git-send-email-daniel.santos@pobox.com> (raw)
In-Reply-To: <1379459317-13046-1-git-send-email-daniel.santos@pobox.com>
Signed-off-by: Daniel Santos <daniel.santos@pobox.com>
---
include/linux/string.h | 8 +++++++
lib/string.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 68 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index ac889c5..76ce2ff 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -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_ */
diff --git a/lib/string.c b/lib/string.c
index e5878de..7fd2704 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -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
--
1.8.1.5
next prev parent reply other threads:[~2013-09-17 23:08 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-17 23:08 [PATCH 0/5] Preliminary: Add error names & descrptions to printks danielfsantos
2013-09-17 23:08 ` [PATCH 1/5] scripts: Add mkstrerror.sh danielfsantos
2013-09-17 23:08 ` [PATCH 2/5] lib: Add .config options for error strings in printks danielfsantos
2013-09-17 23:18 ` Daniel Santos
2013-09-17 23:08 ` [PATCH 3/5] Makefile: Generate error_strings.h danielfsantos
2013-09-17 23:08 ` danielfsantos [this message]
2013-09-17 23:08 ` [PATCH 5/5] lib: Add error string support to printks danielfsantos
2013-09-18 5:36 ` Joe Perches
2013-09-18 11:04 ` David Howells
2013-09-19 1:27 ` Daniel Santos
2013-09-20 1:07 ` Joe Perches
2013-09-20 5:21 ` Daniel Santos
2013-09-20 11:45 ` Joe Perches
2013-09-23 19:40 ` Daniel Santos
2013-09-20 13:07 ` David Howells
2013-09-23 20:17 ` Daniel Santos
2013-09-18 11:08 ` [PATCH 0/5] Preliminary: Add error names & descrptions " David Howells
2013-09-18 22:47 ` Daniel Santos
2013-09-18 11:38 ` [PATCH 1/5] scripts: Add mkstrerror.sh David Howells
2013-09-18 22:43 ` Daniel Santos
2013-09-19 22:35 ` Daniel Santos
2013-09-19 22:53 ` Daniel Santos
2013-09-18 11:55 ` David Howells
2013-09-18 22:27 ` Daniel Santos
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1379459317-13046-5-git-send-email-daniel.santos@pobox.com \
--to=danielfsantos@att.net \
--cc=akpm@linux-foundation.org \
--cc=daniel.santos@pobox.com \
--cc=dave.hansen@linux.intel.com \
--cc=dhowells@redhat.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@horizon.com \
--cc=mmarek@suse.cz \
--cc=mtk.manpages@gmail.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®