1 /* 2 Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> 3 */ 4 #ifndef _REENT_ONLY 5 6 #include <_ansi.h> 7 #include <stdlib.h> 8 #include <string.h> 9 10 char * strndup(const char * str,size_t n)11strndup (const char *str, 12 size_t n) 13 { 14 const char *ptr = str; 15 size_t len; 16 char *copy; 17 18 while (n-- > 0 && *ptr) 19 ptr++; 20 21 len = ptr - str; 22 23 copy = malloc (len + 1); 24 if (copy) 25 { 26 memcpy (copy, str, len); 27 copy[len] = '\0'; 28 } 29 return copy; 30 } 31 32 #endif /* !_REENT_ONLY */ 33