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.
This commit is contained in:
Ori Bernstein 2020-12-17 19:20:04 -08:00
parent 646c502b15
commit 658c1b9f68
2 changed files with 21 additions and 0 deletions

View file

@ -19,6 +19,7 @@ extern int memcmp(const void *, const void *, size_t);
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);

View file

@ -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;
}