strdup.c revision 362716
1#include <config.h> 2 3#include <ntp_assert.h> 4#include <string.h> 5#include "ntp_malloc.h" 6#include "l_stdlib.h" 7 8#define STRDUP_EMPTY_UNIT 9 10#ifndef HAVE_STRDUP 11# undef STRDUP_EMPTY_UNIT 12char *strdup(const char *s); 13char * 14strdup( 15 const char *s 16 ) 17{ 18 size_t octets; 19 char * cp; 20 21 REQUIRE(s); 22 octets = strlen(s) + 1; 23 if ((cp = malloc(octets)) == NULL) 24 return NULL; 25 memcpy(cp, s, octets); 26 27 return cp; 28} 29#endif 30 31#ifndef HAVE_MEMCHR 32# undef STRDUP_EMPTY_UNIT 33void *memchr(const void *s, int c, size_t n) 34{ 35 const unsigned char *p = s; 36 while (n && *p != c) { 37 --n; 38 ++p; 39 } 40 return n ? (char*)p : NULL; 41} 42#endif 43 44#ifndef HAVE_STRNLEN 45# undef STRDUP_EMPTY_UNIT 46size_t strnlen(const char *s, size_t n) 47{ 48 const char *e = memchr(s, 0, n); 49 return e ? (size_t)(e - s) : n; 50} 51#endif 52 53#ifdef STRDUP_EMPTY_UNIT 54int strdup_c_nonempty_compilation_unit; 55#endif 56