/* Implement the strndup function.
Copyright 2005 Free Software Foundation, Inc.
Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
*/
/*
@deftypefn Extension char* strndup (const char *@var{s}, size_t @var{n})
Returns a pointer to a copy of @var{s} with at most @var{n} characters
in memory obtained from @code{malloc}, or @code{NULL} if insufficient
memory was available. The result is always NUL terminated.
@end deftypefn
*/
#include "stdhdr.h"
FUNC(char*) strndup(const char *s, size_t n) {
char *result;
size_t len = strlen (s);
if (n < len)
len = n;
result = (char *) malloc (len + 1);
if (!result)
return 0;
result[len] = '\0';
return (char *) memcpy (result, s, len);
}
Detected encoding: ASCII (7 bit) | 2
|