12b15cb3dSCy Schubert #include <config.h>
22b15cb3dSCy Schubert
3276da39aSCy Schubert #include <ntp_assert.h>
4276da39aSCy Schubert #include <string.h>
5*767173ceSCy Schubert #include "ntp_malloc.h"
6*767173ceSCy Schubert #include "l_stdlib.h"
7*767173ceSCy Schubert
8*767173ceSCy Schubert #define STRDUP_EMPTY_UNIT
9ce265a54SOllivier Robert
102b15cb3dSCy Schubert #ifndef HAVE_STRDUP
11*767173ceSCy Schubert # undef STRDUP_EMPTY_UNIT
12ce265a54SOllivier Robert char *strdup(const char *s);
13224ba2bdSOllivier Robert char *
strdup(const char * s)14224ba2bdSOllivier Robert strdup(
15224ba2bdSOllivier Robert const char *s
16224ba2bdSOllivier Robert )
17224ba2bdSOllivier Robert {
182b15cb3dSCy Schubert size_t octets;
19224ba2bdSOllivier Robert char * cp;
20224ba2bdSOllivier Robert
21276da39aSCy Schubert REQUIRE(s);
22276da39aSCy Schubert octets = strlen(s) + 1;
23276da39aSCy Schubert if ((cp = malloc(octets)) == NULL)
24276da39aSCy Schubert return NULL;
252b15cb3dSCy Schubert memcpy(cp, s, octets);
262b15cb3dSCy Schubert
27276da39aSCy Schubert return cp;
28224ba2bdSOllivier Robert }
29*767173ceSCy Schubert #endif
30*767173ceSCy Schubert
31*767173ceSCy Schubert #ifndef HAVE_MEMCHR
32*767173ceSCy Schubert # undef STRDUP_EMPTY_UNIT
memchr(const void * s,int c,size_t n)33*767173ceSCy Schubert void *memchr(const void *s, int c, size_t n)
34*767173ceSCy Schubert {
35*767173ceSCy Schubert const unsigned char *p = s;
36*767173ceSCy Schubert while (n && *p != c) {
37*767173ceSCy Schubert --n;
38*767173ceSCy Schubert ++p;
39*767173ceSCy Schubert }
40*767173ceSCy Schubert return n ? (char*)p : NULL;
41*767173ceSCy Schubert }
42*767173ceSCy Schubert #endif
43*767173ceSCy Schubert
44*767173ceSCy Schubert #ifndef HAVE_STRNLEN
45*767173ceSCy Schubert # undef STRDUP_EMPTY_UNIT
strnlen(const char * s,size_t n)46*767173ceSCy Schubert size_t strnlen(const char *s, size_t n)
47*767173ceSCy Schubert {
48*767173ceSCy Schubert const char *e = memchr(s, 0, n);
49*767173ceSCy Schubert return e ? (size_t)(e - s) : n;
50*767173ceSCy Schubert }
51*767173ceSCy Schubert #endif
52*767173ceSCy Schubert
53*767173ceSCy Schubert #ifdef STRDUP_EMPTY_UNIT
542b15cb3dSCy Schubert int strdup_c_nonempty_compilation_unit;
55ce265a54SOllivier Robert #endif
56