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