shithub: riscv

Download patch

ref: 658c1b9f68065a4e9ac7f1e28b3eedbba36c3099
parent: 646c502b151f667362c625b6e2440cf1db520475
author: Ori Bernstein <[email protected]>
date: Thu Dec 17 14:20:04 EST 2020

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.

--- a/sys/include/ape/string.h
+++ b/sys/include/ape/string.h
@@ -19,6 +19,7 @@
 extern int strcmp(const char *, const char *);
 extern int strcoll(const char *, const char *);
 extern char *strdup(char*);
+extern char *strndup(char*, size_t);
 extern int strncmp(const char *, const char *, size_t);
 extern size_t strxfrm(char *, const char *, size_t);
 extern void *memchr(const void *, int, size_t);
--- /dev/null
+++ b/sys/src/ape/lib/ap/gen/strndup.c
@@ -1,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;
+}