aboutsummaryrefslogtreecommitdiff
path: root/lib/libutils/isoc/strndup.c
blob: dd2934e819b1a0dffaffec196afa9add2fc1b924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#include <stdlib.h>
#include <string.h>

char *strndup(const char *s, size_t n)
{
	size_t l = strnlen(s, n) + 1;
	char *p = malloc(l);

	if (p) {
		memcpy(p, s, l - 1);
		p[l - 1] = '\0';
	}
	return p;
}