diff options
author | Rob Landley <rob@landley.net> | 2015-08-01 11:48:59 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2015-08-01 11:48:59 -0500 |
commit | 7cc95a79a7a4ed8fb8fb3c5e1946a59c93ff335e (patch) | |
tree | 938eb6c27ee5be9f2d5892ad0236024dd75c8f2d | |
parent | ce4188fd1228c8037d1f059cf24a066941c26501 (diff) | |
download | toybox-7cc95a79a7a4ed8fb8fb3c5e1946a59c93ff335e.tar.gz toybox-7cc95a79a7a4ed8fb8fb3c5e1946a59c93ff335e.tar.bz2 toybox-7cc95a79a7a4ed8fb8fb3c5e1946a59c93ff335e.zip |
Move strlower() from find to lib.
-rw-r--r-- | lib/lib.c | 35 | ||||
-rw-r--r-- | lib/lib.h | 1 | ||||
-rw-r--r-- | toys/posix/find.c | 35 |
3 files changed, 36 insertions, 35 deletions
@@ -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"; @@ -160,6 +160,7 @@ long xstrtol(char *str, char **end, int base); long atolx(char *c); long atolx_range(char *numstr, long low, long high); int stridx(char *haystack, char needle); +char *strlower(char *s); int unescape(char c); int strstart(char **a, char *b); off_t fdlength(int fd); diff --git a/toys/posix/find.c b/toys/posix/find.c index a11a910..99cf5e2 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -141,41 +141,6 @@ static void do_print(struct dirtree *new, char c) free(s); } -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; -} - // Call this with 0 for first pass argument parsing and syntax checking (which // populates argdata). Later commands traverse argdata (in order) when they // need "do once" results. |