summaryrefslogtreecommitdiff
path: root/lib/libc/string/strndup.c
blob: 18d1a718d8f21c54985b00eb92bce9d4f54eb4cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stddef.h" // for NULL

#include <stdlib.h> // for malloc
#include <string.h> // for memcpy, size_t, strndup

char *strndup(const char *s, size_t size)
{
	char *result = malloc(size + 1);

	if (result == NULL)
		return NULL;

	memcpy(result, s, size);
	result[size] = '\0';

	return result;
}