2013-12-04 21:33:17 +00:00
|
|
|
#include <config.h>
|
2002-10-29 19:58:12 +00:00
|
|
|
|
2015-07-01 03:12:13 +00:00
|
|
|
#include <ntp_assert.h>
|
|
|
|
#include <string.h>
|
2020-06-24 00:20:45 +00:00
|
|
|
#include "ntp_malloc.h"
|
|
|
|
#include "l_stdlib.h"
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2020-06-24 00:20:45 +00:00
|
|
|
#define STRDUP_EMPTY_UNIT
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2020-06-24 00:20:45 +00:00
|
|
|
#ifndef HAVE_STRDUP
|
|
|
|
# undef STRDUP_EMPTY_UNIT
|
2002-10-29 19:58:12 +00:00
|
|
|
char *strdup(const char *s);
|
2001-08-29 14:35:15 +00:00
|
|
|
char *
|
|
|
|
strdup(
|
|
|
|
const char *s
|
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
size_t octets;
|
|
|
|
char * cp;
|
|
|
|
|
2015-07-01 03:12:13 +00:00
|
|
|
REQUIRE(s);
|
|
|
|
octets = strlen(s) + 1;
|
|
|
|
if ((cp = malloc(octets)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
memcpy(cp, s, octets);
|
2001-08-29 14:35:15 +00:00
|
|
|
|
2015-07-01 03:12:13 +00:00
|
|
|
return cp;
|
2001-08-29 14:35:15 +00:00
|
|
|
}
|
2020-06-24 00:20:45 +00:00
|
|
|
#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
|
2013-12-04 21:33:17 +00:00
|
|
|
int strdup_c_nonempty_compilation_unit;
|
2002-10-29 19:58:12 +00:00
|
|
|
#endif
|