From 7cc95a79a7a4ed8fb8fb3c5e1946a59c93ff335e Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 1 Aug 2015 11:48:59 -0500 Subject: Move strlower() from find to lib. --- lib/lib.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 05e377f8..81c989ab 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -291,6 +291,41 @@ int stridx(char *haystack, char needle) return off-haystack; } +char *strlower(char *s) +{ + char *try, *new; + + if (!CFG_TOYBOX_I18N) { + try = new = xstrdup(s); + for (; *s; s++) *(new++) = tolower(*s); + } else { + // I can't guarantee the string _won't_ expand during reencoding, so...? + try = new = xmalloc(strlen(s)*2+1); + + while (*s) { + wchar_t c; + int len = mbrtowc(&c, s, MB_CUR_MAX, 0); + + if (len < 1) *(new++) = *(s++); + else { + s += len; + // squash title case too + c = towlower(c); + + // if we had a valid utf8 sequence, convert it to lower case, and can't + // encode back to utf8, something is wrong with your libc. But just + // in case somebody finds an exploit... + len = wcrtomb(new, c, 0); + if (len < 1) error_exit("bad utf8 %x", (int)c); + new += len; + } + } + *new = 0; + } + + return try; +} + int unescape(char c) { char *from = "\\abefnrtv", *to = "\\\a\b\033\f\n\r\t\v"; -- cgit v1.2.3