aboutsummaryrefslogtreecommitdiff
path: root/payloads/libpayload/libc
diff options
context:
space:
mode:
authorThomas Heijligen <src@posteo.de>2022-11-21 17:21:47 +0100
committerFelix Held <felix-coreboot@felixheld.de>2023-02-16 17:54:51 +0000
commit3d91563c98296ea9947cfab4077f8d8cd1a78835 (patch)
tree19ab02d20a512f560bb0dc1221dcc797d1e03775 /payloads/libpayload/libc
parente68ddc71ef4b01c746203fd0e72d99917c022244 (diff)
libpayload/string: add strndup() function
Change-Id: Ie509e49f21fb537692704ac6527efa09649164e3 Signed-off-by: Thomas Heijligen <src@posteo.de> Reviewed-on: https://review.coreboot.org/c/coreboot/+/70115 Reviewed-by: Jakub Czapiga <jacz@semihalf.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'payloads/libpayload/libc')
-rw-r--r--payloads/libpayload/libc/string.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c
index 46c3c019bd..b3e1c8df0d 100644
--- a/payloads/libpayload/libc/string.c
+++ b/payloads/libpayload/libc/string.c
@@ -322,6 +322,25 @@ char *strdup(const char *s)
}
/**
+ * Duplicate a string with a max length of size
+ *
+ * @param s The string to duplicate.
+ * @param size The max length of the string
+ * @return A pointer to the copy of the original string.
+ */
+char *strndup(const char *s, size_t size)
+{
+ size_t n = strnlen(s, size);
+ char *p = malloc(n + 1);
+
+ if (p != NULL) {
+ strncpy(p, s, n);
+ p[n] = 0;
+ }
+ return p;
+}
+
+/**
* Find a substring within a string.
*
* @param h The haystack string.