summaryrefslogtreecommitdiff
path: root/src/sys
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-01 14:56:15 -0400
committerIan Moffett <ian@osmora.org>2025-10-01 14:56:15 -0400
commit39db5071532630f38bc617e3b7d36dc3005ce3e1 (patch)
treeb1c759182226d70d736b76995bfd338e09617f4e /src/sys
parent7b7bce65bcec3d9aa0be7601d342226a1b534a79 (diff)
kern: ptrbox: Add string duplication
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys')
-rw-r--r--src/sys/include/lib/ptrbox.h12
-rw-r--r--src/sys/lib/ptrbox.c24
2 files changed, 36 insertions, 0 deletions
diff --git a/src/sys/include/lib/ptrbox.h b/src/sys/include/lib/ptrbox.h
index a84a760..025ac06 100644
--- a/src/sys/include/lib/ptrbox.h
+++ b/src/sys/include/lib/ptrbox.h
@@ -96,4 +96,16 @@ int ptrbox_terminate(struct ptrbox *box);
*/
void *ptrbox_alloc(size_t len, struct ptrbox *box);
+/*
+ * Duplicate a string and store it in a
+ * pointer box
+ *
+ * @s: String to duplicate
+ * @box: Box to do tracking
+ *
+ * Returns new heap string on success, otherwise NULL
+ * on failure
+ */
+char *ptrbox_strdup(const char *s, struct ptrbox *box);
+
#endif /* !_PTRBOX_H_ */
diff --git a/src/sys/lib/ptrbox.c b/src/sys/lib/ptrbox.c
index b8370e5..bc84145 100644
--- a/src/sys/lib/ptrbox.c
+++ b/src/sys/lib/ptrbox.c
@@ -116,3 +116,27 @@ ptrbox_init(struct ptrbox **box_res)
*box_res = box;
return 0;
}
+
+/*
+ * String duplication
+ */
+char *
+ptrbox_strdup(const char *s, struct ptrbox *box)
+{
+ size_t len;
+ char *s_new;
+
+ if (s == NULL || box == NULL) {
+ return NULL;
+ }
+
+ len = strlen(s);
+ s_new = ptrbox_alloc(len + 1, box);
+ if (s_new == NULL) {
+ return NULL;
+ }
+
+ memcpy(s_new, s, len);
+ s_new[len] = '\0';
+ return s_new;
+}