aboutsummaryrefslogtreecommitdiffstats
path: root/libntp/strdup.c
blob: 8af9ff81b39cb4787ce9db99f8752175b4bacb2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <config.h>

#include <ntp_assert.h>
#include <string.h>
#include "ntp_malloc.h"
#include "l_stdlib.h"

#define STRDUP_EMPTY_UNIT

#ifndef HAVE_STRDUP
# undef STRDUP_EMPTY_UNIT
char *strdup(const char *s);
char *
strdup(
	const char *s
	)
{
	size_t	octets;
	char *	cp;

	REQUIRE(s);
	octets = strlen(s) + 1;
	if ((cp = malloc(octets)) == NULL)
		return NULL;
	memcpy(cp, s, octets);

	return cp;
}
#endif

#ifndef HAVE_MEMCHR
# undef STRDUP_EMPTY_UNIT
void *memchr(const void *s, int c, size_t n)
{
	const unsigned char *p = s;
	while (n && *p != c) {
		--n;
		++p;
	}
	return n ? (char*)p : NULL;
}
#endif

#ifndef HAVE_STRNLEN
# undef STRDUP_EMPTY_UNIT
size_t strnlen(const char *s, size_t n)
{
	const char *e = memchr(s, 0, n);
	return e ? (size_t)(e - s) : n;
}
#endif

#ifdef STRDUP_EMPTY_UNIT
int strdup_c_nonempty_compilation_unit;
#endif