mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Robert P. J. Day" <rpjday@crashcourse.ca>
To: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Andrew Morton <akpm@osdl.org>
Subject: [PATCH] LIB: Allow memparse() to accept a NULL and ignorable second parm.
Date: Sat, 26 Apr 2008 08:10:55 -0400 (EDT)	[thread overview]
Message-ID: <alpine.LFD.1.10.0804260806480.13845@localhost.localdomain> (raw)


Extend memparse() to allow the caller to use a NULL second parameter,
which would represent no interest in returning the address of the end
of the parsed string.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

---

  in numerous cases, callers invoke memparse() to parse a
possibly-suffixed string (such as "64K" or "2G" or whatever) and
define a character pointer to accept the end pointer being returned by
memparse() even though they have no interest in it and promptly throw
it away.

  this (backward-compatible) enhancement allows callers to use NULL in
the cases where they just don't care about getting back that end
pointer.

  compile-tested on x86.

diff --git a/lib/cmdline.c b/lib/cmdline.c
index f596c08..34129cf 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -116,7 +116,7 @@ char *get_options(const char *str, int nints, int *ints)
 /**
  *	memparse - parse a string with mem suffixes into a number
  *	@ptr: Where parse begins
- *	@retptr: (output) Pointer to next char after parse completes
+ *	@retptr: (output) Optional pointer to next char after parse completes
  *
  *	Parses a string into a number.  The number stored at @ptr is
  *	potentially suffixed with %K (for kilobytes, or 1024 bytes),
@@ -128,9 +128,11 @@ char *get_options(const char *str, int nints, int *ints)

 unsigned long long memparse (char *ptr, char **retptr)
 {
-	unsigned long long ret = simple_strtoull (ptr, retptr, 0);
+	char *endptr;	/* local pointer to end of parsed string */

-	switch (**retptr) {
+	unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
+
+	switch (*endptr) {
 	case 'G':
 	case 'g':
 		ret <<= 10;
@@ -140,10 +142,15 @@ unsigned long long memparse (char *ptr, char **retptr)
 	case 'K':
 	case 'k':
 		ret <<= 10;
-		(*retptr)++;
+		endptr++;
 	default:
 		break;
 	}
+
+	if (retptr) {
+		*retptr = endptr;
+	}
+
 	return ret;
 }

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry:
    Have classroom, will lecture.

http://crashcourse.ca                          Waterloo, Ontario, CANADA
========================================================================

                 reply	other threads:[~2008-04-26 12:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=alpine.LFD.1.10.0804260806480.13845@localhost.localdomain \
    --to=rpjday@crashcourse.ca \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®