diff options
Diffstat (limited to 'src/user/knob/block.c')
-rw-r--r-- | src/user/knob/block.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/user/knob/block.c b/src/user/knob/block.c index 9f3f353..8b9971f 100644 --- a/src/user/knob/block.c +++ b/src/user/knob/block.c @@ -1,4 +1,5 @@ #include <knob/panic.h> +#include <knob/block.h> #include <knob/heap.h> #include <stdbool.h> #include <stdint.h> @@ -28,13 +29,22 @@ uint32_t strcpy(char *to, const char *from) { } char *strdup(const char *from) { - const char *end = from; - while (*(end++)) - ; - char *buf = get_block(end - from); - if (!buf) - PANIC("get_block returned 0 in strdup"); - blockcpy(buf, from, end - from); + const uint32_t len = strlen(from) + 1; + char *buf = get_block(len); + blockcpy(buf, from, len); + return buf; +} + +char *strndup(const char *from, uint32_t n) { + uint32_t len = strlen(from) + 1; + if (n < len) { + char *buf = get_block(n + 1); + blockcpy(buf, from, n); + buf[n] = '\0'; + return buf; + } + char *buf = get_block(len); + blockcpy(buf, from, len); return buf; } |