From: Wesley Zhao <zhaowei1102@thundersoft.com>
To: akpm@linux-foundation.org
Cc: andriy.shevchenko@linux.intel.com, keescook@chromium.org,
tglx@linutronix.de, kerneldev@karsmulder.nl,
nivedita@alum.mit.edu, joe@perches.com, gpiccoli@canonical.com,
aquini@redhat.com, gustavoars@kernel.org,
zhaowei1102@thundersoft.com, ojeda@kernel.org,
ndesaulniers@gooogle.com, linux-kernel@vger.kernel.org,
david@redhat.com, dan.j.williams@intel.com, guohanjun@huawei.com,
mchehab+huawei@kernel.org
Subject: [PATCH v2 1/2] lib/cmdline: add new function get_option_ull()
Date: Sat, 16 Jan 2021 02:41:12 -0800 [thread overview]
Message-ID: <1610793673-64008-2-git-send-email-zhaowei1102@thundersoft.com> (raw)
In-Reply-To: <1610793673-64008-1-git-send-email-zhaowei1102@thundersoft.com>
In the future we would pass the unsigned long long parameter
like(0x123456781234) in cmdline on the 64bit platform, so add a new
option parse function get_option_ull()
Signed-off-by: Wesley Zhao <zhaowei1102@thundersoft.com>
---
include/linux/kernel.h | 2 ++
lib/cmdline.c | 94 ++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 82 insertions(+), 14 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index f7902d8c1048..eb1f0b14a0c5 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -348,6 +348,8 @@ extern __scanf(2, 0)
int vsscanf(const char *, const char *, va_list);
extern int get_option(char **str, int *pint);
+extern int get_option_ll(char **str, long long *pll);
+extern int get_option_ull(char **str, unsigned long long *pull);
extern char *get_options(const char *str, int nints, int *ints);
extern unsigned long long memparse(const char *ptr, char **retptr);
extern bool parse_option_str(const char *str, const char *option);
diff --git a/lib/cmdline.c b/lib/cmdline.c
index b390dd03363b..d32b585d287d 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -31,6 +31,81 @@ static int get_range(char **str, int *pint, int n)
*pint++ = x;
return inc_counter;
}
+/**
+ * get_option_ull - Parse unsigned long long from an option string
+ * @str: option string
+ * @pull: (output) unsigned long long value parsed from @str
+ *
+ * Read an unsigned long long from an option string; if available accept a subsequent
+ * comma as well.
+ *
+ * Return values:
+ * 0 - no ull in string
+ * 1 - ull found, no subsequent comma
+ * 2 - ull found including a subsequent comma
+ * 3 - hyphen found to denote a range
+ */
+
+int get_option_ull(char **str, unsigned long long *pull)
+{
+ char *cur = *str;
+ unsigned long long value;
+
+ if (!cur || !(*cur))
+ return 0;
+ value = simple_strtoull(cur, str, 0);
+ if (pull)
+ *pull = value;
+ if (cur == *str)
+ return 0;
+ if (**str == ',') {
+ (*str)++;
+ return 2;
+ }
+ if (**str == '-')
+ return 3;
+
+ return 1;
+}
+EXPORT_SYMBOL(get_option_ull);
+
+/**
+ * get_option_ll - Parse long long from an option string
+ * @str: option string
+ * @pll: (output) long long value parsed from @str
+ *
+ * Read an long long from an option string; if available accept a subsequent
+ * comma as well.
+ *
+ * Return values:
+ * 0 - no ll in string
+ * 1 - ll found, no subsequent comma
+ * 2 - ll found including a subsequent comma
+ * 3 - hyphen found to denote a range
+ */
+
+int get_option_ll(char **str, long long *pll)
+{
+ char *cur = *str;
+ unsigned long long value;
+ int ret;
+
+ if (!cur || !(*cur))
+ return 0;
+ if (*cur == '-') {
+ cur = cur + 1;
+ ret = get_option_ull((char **)&cur, &value);
+ if (pll)
+ *pll = -value;
+ } else {
+ ret = get_option_ull(str, &value);
+ if (pll)
+ *pll = value;
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL(get_option_ll);
/**
* get_option - Parse integer from an option string
@@ -56,26 +131,17 @@ static int get_range(char **str, int *pint, int n)
int get_option(char **str, int *pint)
{
char *cur = *str;
- int value;
+ long long value;
+ int ret;
if (!cur || !(*cur))
return 0;
- if (*cur == '-')
- value = -simple_strtoull(++cur, str, 0);
- else
- value = simple_strtoull(cur, str, 0);
+ ret = get_option_ll(str, &value);
+
if (pint)
*pint = value;
- if (cur == *str)
- return 0;
- if (**str == ',') {
- (*str)++;
- return 2;
- }
- if (**str == '-')
- return 3;
- return 1;
+ return ret;
}
EXPORT_SYMBOL(get_option);
--
2.7.4
next prev parent reply other threads:[~2021-01-16 10:44 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-16 10:41 [PATCH v2 0/2] Make it possible to reserve memory on 64bit platform Wesley Zhao
2021-01-16 10:41 ` Wesley Zhao [this message]
2021-01-17 19:02 ` Dan Williams
[not found] ` <202101181151049299923@thundersoft.com>
2021-01-18 9:50 ` David Hildenbrand
[not found] ` <202101221536190259795@thundersoft.com>
2021-01-22 10:21 ` Andy Shevchenko
2021-01-18 10:42 ` Andy Shevchenko
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=1610793673-64008-2-git-send-email-zhaowei1102@thundersoft.com \
--to=zhaowei1102@thundersoft.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=aquini@redhat.com \
--cc=dan.j.williams@intel.com \
--cc=david@redhat.com \
--cc=gpiccoli@canonical.com \
--cc=guohanjun@huawei.com \
--cc=gustavoars@kernel.org \
--cc=joe@perches.com \
--cc=keescook@chromium.org \
--cc=kerneldev@karsmulder.nl \
--cc=linux-kernel@vger.kernel.org \
--cc=mchehab+huawei@kernel.org \
--cc=ndesaulniers@gooogle.com \
--cc=nivedita@alum.mit.edu \
--cc=ojeda@kernel.org \
--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;
as well as URLs for NNTP newsgroup(s).