mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/6] string: Introduce strtobool
Date: Fri, 29 Apr 2016 11:52:01 +0200	[thread overview]
Message-ID: <1461923525-6356-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1461923525-6356-1-git-send-email-s.hauer@pengutronix.de>

We have at least two places which convert a string to a boolean type,
so create a common function for this. strtobool treats

- any positive (nonzero) number as true
- "0" as false
- "true" (case insensitive) as true
- "false" (case insensitive) as false

Every other value results in an error and the input *val is not
modified. The caller is expected to initialize *val with the correct
default before calling strtobool.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/string.h |  1 +
 lib/string.c     | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/string.h b/include/string.h
index a833da1..0c557d6 100644
--- a/include/string.h
+++ b/include/string.h
@@ -4,5 +4,6 @@
 #include <linux/string.h>
 
 void *memdup(const void *, size_t);
+int strtobool(const char *str, int *val);
 
 #endif /* __STRING_H */
diff --git a/lib/string.c b/lib/string.c
index 6a39eb5..a3e9fd8 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -739,3 +739,46 @@ void *memdup(const void *orig, size_t size)
 	return buf;
 }
 EXPORT_SYMBOL(memdup);
+
+/**
+ * strtobool - convert a string to a boolean value
+ * @str - The string
+ * @val - The boolean value returned.
+ *
+ * This function treats
+ * - any positive (nonzero) number as true
+ * - "0" as false
+ * - "true" (case insensitive) as true
+ * - "false" (case insensitive) as false
+ *
+ * Every other value results in an error and the @val is not
+ * modified. The caller is expected to initialize @val with the
+ * correct default before calling strtobool.
+ *
+ * Returns 0 for success or negative error code if the variable does
+ * not exist or contains something this function does not recognize
+ * as true or false.
+ */
+int strtobool(const char *str, int *val)
+{
+	if (!str || !*str)
+		return -EINVAL;
+
+	if (simple_strtoul(str, NULL, 0) > 0) {
+		*val = true;
+		return 0;
+	}
+
+	if (!strcmp(str, "0") || !strcasecmp(str, "false")) {
+		*val = false;
+		return 0;
+	}
+
+	if (!strcasecmp(str, "true")) {
+		*val = true;
+		return 0;
+	}
+
+	return -EINVAL;
+}
+EXPORT_SYMBOL(strtobool);
-- 
2.8.0.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-04-29  9:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-29  9:51 Sascha Hauer
2016-04-29  9:52 ` [PATCH 1/6] globalvar: Allow to remove globalvars Sascha Hauer
2016-04-29  9:52 ` Sascha Hauer [this message]
2016-04-29  9:52 ` [PATCH 3/6] getenv_bool: use strtobool Sascha Hauer
2016-04-29  9:52 ` [PATCH 4/6] parameter: Use strtobool Sascha Hauer
2016-04-29  9:52 ` [PATCH 5/6] bootm: Optionally add a root= option to Kernel command line Sascha Hauer
2016-04-29  9:52 ` [PATCH 6/6] blspec: push appendroot handling to bootm Sascha Hauer

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=1461923525-6356-3-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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