summaryrefslogtreecommitdiff
path: root/klibc/klibc/strndup.c
blob: 1b44e6f99ae697b201373e84cdc0a0372103dc7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * strndup.c
 */

#include <string.h>
#include <stdlib.h>

char *strndup(const char *s, size_t n)
{
	int l = n > strlen(s) ? strlen(s)+1 : n+1;
	char *d = malloc(l);

	if (d)
		memcpy(d, s, l);
	d[n] = '\0';
	return d;
}