diff options
author | Ori Bernstein <ori@eigenstate.org> | 2020-12-17 19:20:04 -0800 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2020-12-17 19:20:04 -0800 |
commit | 658c1b9f68065a4e9ac7f1e28b3eedbba36c3099 (patch) | |
tree | 7a5bdde36c6afd569f7da424221ba0e845cca260 /sys/src/ape/lib/ap/gen | |
parent | 646c502b151f667362c625b6e2440cf1db520475 (diff) |
libap: add strndup
strndup is part of POSIX.1, so APE should provide it.
This patch adds it, so need to patch it out of fewer
programs going forward.
Diffstat (limited to 'sys/src/ape/lib/ap/gen')
-rw-r--r-- | sys/src/ape/lib/ap/gen/strndup.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/sys/src/ape/lib/ap/gen/strndup.c b/sys/src/ape/lib/ap/gen/strndup.c new file mode 100644 index 000000000..b60e2da2b --- /dev/null +++ b/sys/src/ape/lib/ap/gen/strndup.c @@ -0,0 +1,20 @@ +#include <string.h> +#include <ctype.h> +#include <stdlib.h> + +char* +strndup(char *p, size_t max) +{ + int n; + char *np; + + n = strlen(p)+1; + if(n > max) + n = max+1; + np = malloc(n); + if(!np) + return nil; + memmove(np, p, n); + np[n-1] = 0; + return np; +} |