/* Implement the strndup function.
- Copyright (C) 2005 Free Software Foundation, Inc.
+ Copyright (C) 2005-2019 Free Software Foundation, Inc.
Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
This file is part of the libiberty library.
#include "ansidecl.h"
#include <stddef.h>
-extern size_t strlen (const char*);
+extern size_t strnlen (const char *s, size_t maxlen);
extern PTR malloc (size_t);
extern PTR memcpy (PTR, const PTR, size_t);
strndup (const char *s, size_t n)
{
char *result;
- size_t len = strlen (s);
+ size_t len = strnlen (s, n);
- if (n < len)
- len = n;
-
- result = malloc (len + 1);
+ result = (char *) malloc (len + 1);
if (!result)
return 0;
result[len] = '\0';
- return memcpy (result, s, len);
+ return (char *) memcpy (result, s, len);
}