1d54cfbdroberto#include <config.h>
2f771469roberto
35ef283fcy#include <ntp_assert.h>
45ef283fcy#include <string.h>
570964a8cy#include "ntp_malloc.h"
670964a8cy#include "l_stdlib.h"
740b8e41roberto
870964a8cy#define STRDUP_EMPTY_UNIT
940b8e41roberto
1070964a8cy#ifndef HAVE_STRDUP
1170964a8cy# undef STRDUP_EMPTY_UNIT
12f771469robertochar *strdup(const char *s);
1340b8e41robertochar *
1440b8e41robertostrdup(
1540b8e41roberto	const char *s
1640b8e41roberto	)
1740b8e41roberto{
18d54cfbdroberto	size_t	octets;
19d54cfbdroberto	char *	cp;
20d54cfbdroberto
215ef283fcy	REQUIRE(s);
225ef283fcy	octets = strlen(s) + 1;
235ef283fcy	if ((cp = malloc(octets)) == NULL)
245ef283fcy		return NULL;
255ef283fcy	memcpy(cp, s, octets);
2640b8e41roberto
275ef283fcy	return cp;
2840b8e41roberto}
2970964a8cy#endif
3070964a8cy
3170964a8cy#ifndef HAVE_MEMCHR
3270964a8cy# undef STRDUP_EMPTY_UNIT
3370964a8cyvoid *memchr(const void *s, int c, size_t n)
3470964a8cy{
3570964a8cy	const unsigned char *p = s;
3670964a8cy	while (n && *p != c) {
3770964a8cy		--n;
3870964a8cy		++p;
3970964a8cy	}
4070964a8cy	return n ? (char*)p : NULL;
4170964a8cy}
4270964a8cy#endif
4370964a8cy
4470964a8cy#ifndef HAVE_STRNLEN
4570964a8cy# undef STRDUP_EMPTY_UNIT
4670964a8cysize_t strnlen(const char *s, size_t n)
4770964a8cy{
4870964a8cy	const char *e = memchr(s, 0, n);
4970964a8cy	return e ? (size_t)(e - s) : n;
5070964a8cy}
5170964a8cy#endif
5270964a8cy
5370964a8cy#ifdef STRDUP_EMPTY_UNIT
54d54cfbdrobertoint strdup_c_nonempty_compilation_unit;
55f771469roberto#endif
56