diff options
author | Michael Brown <mcb30@etherboot.org> | 2007-07-08 22:03:12 +0100 |
---|---|---|
committer | Michael Brown <mcb30@etherboot.org> | 2007-07-08 22:03:12 +0100 |
commit | 7137c20ed1f486f31047a3a3dc77d90d0a5bb7dc (patch) | |
tree | 83869312d98ec8729d63fb73f96eebc06dbe21ae /src/core | |
parent | ed20fee0cfc5863b308d22abc17a7dbb4327c90f (diff) | |
download | ipxe-7137c20ed1f486f31047a3a3dc77d90d0a5bb7dc.tar.gz |
Add strcspn() and strndup()
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/string.c | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/core/string.c b/src/core/string.c index 353abd6f..87c50570 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -279,6 +279,31 @@ size_t strspn(const char *s, const char *accept) } #endif +#ifndef __HAVE_ARCH_STRCSPN +/** + * strcspn - Calculate the length of the initial substring of @s which only + * contain letters not in @reject + * @s: The string to be searched + * @accept: The string to search for + */ +size_t strcspn(const char *s, const char *reject) +{ + const char *p; + const char *r; + size_t count = 0; + + for (p = s; *p != '\0'; ++p) { + for (r = reject; *r != '\0'; ++r) { + if (*p == *r) + return count; + } + ++count; + } + + return count; +} +#endif + #ifndef __HAVE_ARCH_STRPBRK /** * strpbrk - Find the first occurrence of a set of characters @@ -541,9 +566,21 @@ void * memchr(const void *s, int c, size_t n) #endif -char * strdup(const char *s) { - char *new = malloc(strlen(s)+1); - if (new) - strcpy(new,s); +char * strndup(const char *s, size_t n) +{ + size_t len = strlen(s); + char *new; + + if (len>n) + len = n; + new = malloc(len+1); + if (new) { + new[len] = '\0'; + memcpy(new,s,len); + } return new; } + +char * strdup(const char *s) { + return strndup(s, ~((size_t)0)); +} |