Handle strndup in freebsd in the same way it is handled on other

platforms that do not support it directly.
Review URL: http://codereview.chromium.org/18585

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1148 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
ager@chromium.org 2009-01-26 11:28:51 +00:00
parent 78de0cef11
commit 7a523e2f92
3 changed files with 21 additions and 5 deletions

View File

@ -195,7 +195,19 @@ char *OS::StrDup(const char* str) {
char* OS::StrNDup(const char* str, size_t n) {
return strndup(str, n);
// Stupid implementation of strndup since freebsd isn't born with
// one.
size_t len = strlen(str);
if (len <= n) {
return StrDup(str);
}
char* result = new char[n+1];
size_t i;
for (i = 0; i <= n; i++) {
result[i] = str[i];
}
result[i] = '\0';
return result;
}

View File

@ -199,12 +199,14 @@ char* OS::StrNDup(const char* str, size_t n) {
// Stupid implementation of strndup since macos isn't born with
// one.
size_t len = strlen(str);
if (len <= n)
if (len <= n) {
return StrDup(str);
}
char* result = new char[n+1];
size_t i;
for (i = 0; i <= n; i++)
for (i = 0; i <= n; i++) {
result[i] = str[i];
}
result[i] = '\0';
return result;
}

View File

@ -704,12 +704,14 @@ char* OS::StrNDup(const char* str, size_t n) {
// Stupid implementation of strndup since windows isn't born with
// one.
size_t len = strlen(str);
if (len <= n)
if (len <= n) {
return StrDup(str);
}
char* result = new char[n+1];
size_t i;
for (i = 0; i <= n; i++)
for (i = 0; i <= n; i++) {
result[i] = str[i];
}
result[i] = '\0';
return result;
}