From: Marc Kleine-Budde <mkl@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v3 2/4] xfuncs: import xstrndup() from busybox
Date: Wed, 17 Jun 2015 22:47:01 +0200 [thread overview]
Message-ID: <1434574023-20556-3-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1434574023-20556-1-git-send-email-mkl@pengutronix.de>
This function is needed for the fixed length string feature in the state
framework.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
include/xfuncs.h | 1 +
lib/xfuncs.c | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/include/xfuncs.h b/include/xfuncs.h
index 8efc99dbc455..940a1d67ed1c 100644
--- a/include/xfuncs.h
+++ b/include/xfuncs.h
@@ -7,6 +7,7 @@ void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
void *xzalloc(size_t size);
char *xstrdup(const char *s);
+char *xstrndup(const char *s, size_t size);
void* xmemalign(size_t alignment, size_t bytes);
void* xmemdup(const void *orig, size_t size);
diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index 0e78b670a5d4..f0219c43a5ec 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -63,6 +63,28 @@ char *xstrdup(const char *s)
}
EXPORT_SYMBOL(xstrdup);
+char *xstrndup(const char *s, size_t n)
+{
+ int m;
+ char *t;
+
+ /* We can just xmalloc(n+1) and strncpy into it, */
+ /* but think about xstrndup("abc", 10000) wastage! */
+ m = n;
+ t = (char*) s;
+ while (m) {
+ if (!*t) break;
+ m--;
+ t++;
+ }
+ n -= m;
+ t = xmalloc(n + 1);
+ t[n] = '\0';
+
+ return memcpy(t, s, n);
+}
+EXPORT_SYMBOL(xstrndup);
+
void* xmemalign(size_t alignment, size_t bytes)
{
void *p = memalign(alignment, bytes);
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2015-06-17 20:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-17 20:46 [PATCH v3 0/4] state: add support for fixed size strings Marc Kleine-Budde
2015-06-17 20:47 ` [PATCH v3 1/4] parameter: allow setting of string parameters of zero length Marc Kleine-Budde
2015-06-17 20:47 ` Marc Kleine-Budde [this message]
2015-06-17 20:47 ` [PATCH v3 3/4] state: struct variable_type::import: remove const from node argument Marc Kleine-Budde
2015-06-17 20:47 ` [PATCH v3 4/4] state: add support for fixed length strings Marc Kleine-Budde
2015-06-18 8:59 ` [PATCH v3 0/4] state: add support for fixed size strings 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=1434574023-20556-3-git-send-email-mkl@pengutronix.de \
--to=mkl@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