diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h index 6e1d4a53..ef6df844 100644 --- a/WINGs/WINGs/WUtil.h +++ b/WINGs/WINGs/WUtil.h @@ -257,7 +257,7 @@ void wrelease(void *ptr); void* wretain(void *ptr); char *wstrdup(const char *str); -char* wstrndup(char *str, size_t len); +char* wstrndup(const char *str, size_t len); /* Concatenate str1 with str2 and return that in a newly malloc'ed string. * str1 and str2 can be any strings including static and constant strings. @@ -283,7 +283,7 @@ char* wtokenjoin(char **list, int count); void wtokenfree(char **tokens, int count); -char* wtrimspace(char *s); +char* wtrimspace(const char *s); WMRange wmkrange(int start, int count); diff --git a/WINGs/string.c b/WINGs/string.c index 956ad3a5..8cb0b031 100644 --- a/WINGs/string.c +++ b/WINGs/string.c @@ -145,22 +145,20 @@ void wtokenfree(char **tokens, int count) wfree(tokens); } -char *wtrimspace(char *s) +char *wtrimspace(const char *s) { char *t; - char *c; + + if (s == NULL) + return NULL; while (isspace(*s) && *s) s++; - t = s + strlen(s) - 1; + t = (char *)s + strlen(s) - 1; while (t > s && isspace(*t)) t--; - c = wmalloc(t - s + 2); - memcpy(c, s, t - s + 1); - c[t - s + 1] = 0; - - return c; + return wstrndup(s, t - s + 1); } char *wstrdup(const char *str) @@ -170,7 +168,7 @@ char *wstrdup(const char *str) return strcpy(wmalloc(strlen(str) + 1), str); } -char *wstrndup(char *str, size_t len) +char *wstrndup(const char *str, size_t len) { char *copy;