From f63c4fd91b8483c7d4364046fd6ef2d2679dd63a Mon Sep 17 00:00:00 2001 From: schwarze <> Date: Tue, 5 Sep 2017 03:16:14 +0000 Subject: New POSIX xlocale implementation written from scratch. Complete in the sense that all POSIX *locale(3) and *_l(3) functions are included, but in OpenBSD, we of course only really care about LC_CTYPE and we only support ASCII and UTF-8. With important help from kettenis@, guenther@, and jca@. Repeated testing in ports bulk builds by naddy@. Additional testing by jca@, sebastia@, dcoppa@, and others. OK kettenis@ dcoppa@, and guenther@ on an earlier version. Riding guenther@'s libc/librthread major bump. --- src/lib/libc/string/Makefile.inc | 10 ++--- src/lib/libc/string/strcasecmp.3 | 76 ++++++++++++++++++++++++++++---------- src/lib/libc/string/strcasecmp_l.c | 21 +++++++++++ src/lib/libc/string/strcoll.3 | 53 +++++++++++++++++++------- src/lib/libc/string/strcoll_l.c | 14 +++++++ src/lib/libc/string/strerror.3 | 75 +++++++++++++++++++++++++------------ src/lib/libc/string/strerror_l.c | 33 +++++++++++++++++ src/lib/libc/string/strxfrm.3 | 57 +++++++++++++++++++--------- src/lib/libc/string/strxfrm_l.c | 14 +++++++ src/lib/libc/string/wcscasecmp.3 | 76 +++++++++++++++++++++++++++++--------- src/lib/libc/string/wcscasecmp_l.c | 63 +++++++++++++++++++++++++++++++ 11 files changed, 396 insertions(+), 96 deletions(-) create mode 100644 src/lib/libc/string/strcasecmp_l.c create mode 100644 src/lib/libc/string/strcoll_l.c create mode 100644 src/lib/libc/string/strerror_l.c create mode 100644 src/lib/libc/string/strxfrm_l.c create mode 100644 src/lib/libc/string/wcscasecmp_l.c (limited to 'src/lib') diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 95eb93006b..a1b1934aad 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc @@ -1,17 +1,17 @@ -# $OpenBSD: Makefile.inc,v 1.38 2016/03/30 06:38:41 jmc Exp $ +# $OpenBSD: Makefile.inc,v 1.39 2017/09/05 03:16:13 schwarze Exp $ # string sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \ - strcasecmp.c strcasestr.c strcoll.c strdup.c \ - strerror.c strerror_r.c strmode.c strndup.c strnlen.c \ - strsignal.c strtok.c strxfrm.c \ + strcasecmp.c strcasecmp_l.c strcasestr.c strcoll.c strcoll_l.c \ + strdup.c strerror.c strerror_l.c strerror_r.c strmode.c \ + strndup.c strnlen.c strsignal.c strtok.c strxfrm.c strxfrm_l.c \ timingsafe_bcmp.c timingsafe_memcmp.c \ wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ - wmemmove.c wmemset.c wcsdup.c wcscasecmp.c + wmemmove.c wmemset.c wcsdup.c wcscasecmp.c wcscasecmp_l.c # machine-dependent net sources # ../arch/ARCH/Makefile.inc must include sources for: diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3 index 3870362216..bb39df895a 100644 --- a/src/lib/libc/string/strcasecmp.3 +++ b/src/lib/libc/string/strcasecmp.3 @@ -1,7 +1,8 @@ -.\" $OpenBSD: strcasecmp.3,v 1.13 2015/11/24 09:14:35 daniel Exp $ +.\" $OpenBSD: strcasecmp.3,v 1.14 2017/09/05 03:16:13 schwarze Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. +.\" Copyright (c) 2017 Ingo Schwarze .\" .\" This code is derived from software contributed to Berkeley by .\" Chris Torek. @@ -31,25 +32,43 @@ .\" .\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93 .\" -.Dd $Mdocdate: November 24 2015 $ +.Dd $Mdocdate: September 5 2017 $ .Dt STRCASECMP 3 .Os .Sh NAME .Nm strcasecmp , -.Nm strncasecmp +.Nm strcasecmp_l , +.Nm strncasecmp , +.Nm strncasecmp_l .Nd compare strings, ignoring case .Sh SYNOPSIS .In strings.h .Ft int -.Fn strcasecmp "const char *s1" "const char *s2" +.Fo strcasecmp +.Fa "const char *s1" +.Fa "const char *s2" +.Fc .Ft int -.Fn strncasecmp "const char *s1" "const char *s2" "size_t len" +.Fo strcasecmp_l +.Fa "const char *s1" +.Fa "const char *s2" +.Fa "locale_t locale" +.Fc +.Ft int +.Fo strncasecmp +.Fa "const char *s1" +.Fa "const char *s2" +.Fa "size_t len" +.Fc +.Ft int +.Fo strncasecmp_l +.Fa "const char *s1" +.Fa "const char *s2" +.Fa "size_t len" +.Fa "locale_t locale" +.Fc .Sh DESCRIPTION -The -.Fn strcasecmp -and -.Fn strncasecmp -functions compare the NUL-terminated strings +These functions compare the NUL-terminated strings .Fa s1 and .Fa s2 @@ -66,27 +85,44 @@ is greater than .Ql \e0 . .Pp .Fn strncasecmp -compares at most +and +.Fn strncasecmp_l +compare at most .Fa len characters. +.Pp +On +.Ox , +these functions always use the C locale and ignore +the global locale, the thread-specific locale, and the +.Fa locale +argument. +.Sh ENVIRONMENT +On other operating systems, the behaviour of +.Fn strcasecmp +and +.Fn strncasecmp +may depend on the +.Dv LC_CTYPE +.Xr locale 1 . .Sh SEE ALSO -.Xr bcmp 3 , -.Xr memcmp 3 , .Xr strcmp 3 , .Xr strcoll 3 , .Xr strxfrm 3 , .Xr wcscasecmp 3 .Sh STANDARDS -The -.Fn strcasecmp -and -.Fn strncasecmp -functions conform to +These functions conform to .St -p1003.1-2008 . .Sh HISTORY The .Fn strcasecmp and .Fn strncasecmp -functions first appeared in -.Bx 4.3 Tahoe . +functions have been available since +.Bx 4.3 Tahoe , +and +.Fn strcasecmp_l +and +.Fn strncasecmp_l +since +.Ox 6.2 . diff --git a/src/lib/libc/string/strcasecmp_l.c b/src/lib/libc/string/strcasecmp_l.c new file mode 100644 index 0000000000..a9543dda11 --- /dev/null +++ b/src/lib/libc/string/strcasecmp_l.c @@ -0,0 +1,21 @@ +/* $OpenBSD: strcasecmp_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */ +/* + * Written in 2017 by Ingo Schwarze . + * Released into the public domain. + */ + +#include + +int +strcasecmp_l(const char *s1, const char *s2, + locale_t locale __attribute__((__unused__))) +{ + return strcasecmp(s1, s2); +} + +int +strncasecmp_l(const char *s1, const char *s2, size_t n, + locale_t locale __attribute__((__unused__))) +{ + return strncasecmp(s1, s2, n); +} diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3 index d421200b62..4ba1f22677 100644 --- a/src/lib/libc/string/strcoll.3 +++ b/src/lib/libc/string/strcoll.3 @@ -1,4 +1,7 @@ +.\" $OpenBSD: strcoll.3,v 1.10 2017/09/05 03:16:13 schwarze Exp $ +.\" .\" Copyright (c) 1990, 1991 The Regents of the University of California. +.\" Copyright (c) 2017 Ingo Schwarze .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by @@ -29,45 +32,69 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strcoll.3,v 1.9 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ +.Dd $Mdocdate: September 5 2017 $ .Dt STRCOLL 3 .Os .Sh NAME -.Nm strcoll +.Nm strcoll , +.Nm strcoll_l .Nd compare strings according to current collation .Sh SYNOPSIS .In string.h .Ft int .Fn strcoll "const char *s1" "const char *s2" +.Ft int +.Fn strcoll_l "const char *s1" "const char *s2" "locale_t locale" .Sh DESCRIPTION The .Fn strcoll -function lexicographically compares the NUL-terminated strings +and +.Fn strcoll_l +functions lexicographically compare the NUL-terminated strings .Fa s1 and .Fa s2 according to the current locale collation -and returns an integer greater than, equal to, or less than 0, +and return an integer greater than, equal to, or less than 0, according to whether .Fa s1 is greater than, equal to, or less than .Fa s2 . +.Pp +On +.Ox , +they have the same effect as +.Xr strcmp 3 , +and the global locale, the thread-specific locale, and the +.Fa locale +argument are ignored. +.Sh ENVIRONMENT +On other operating systems, the behaviour of +.Fn strcoll +may depend on the +.Dv LC_CTYPE +.Xr locale 1 . .Sh SEE ALSO -.Xr bcmp 3 , -.Xr memcmp 3 , +.Xr newlocale 3 , .Xr setlocale 3 , -.Xr strcasecmp 3 , .Xr strcmp 3 , -.Xr strxfrm 3 +.Xr strxfrm 3 , +.Xr wcscoll 3 .Sh STANDARDS The .Fn strcoll function conforms to -.St -ansiC . +.St -ansiC , +and +.Fn strcoll_l +to +.St -p1003.1-2008 . .Sh HISTORY The .Fn strcoll -function first appeared in -.Bx 4.3 Reno . +function has been available since +.Bx 4.3 Reno , +and +.Fn strcoll_l +since +.Ox 6.2 . diff --git a/src/lib/libc/string/strcoll_l.c b/src/lib/libc/string/strcoll_l.c new file mode 100644 index 0000000000..bcd5b0f417 --- /dev/null +++ b/src/lib/libc/string/strcoll_l.c @@ -0,0 +1,14 @@ +/* $OpenBSD: strcoll_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */ +/* + * Written in 2017 by Ingo Schwarze . + * Released into the public domain. + */ + +#include + +int +strcoll_l(const char *s1, const char *s2, + locale_t locale __attribute__((__unused__))) +{ + return strcmp(s1, s2); +} diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3 index 8b2d32ea75..0d4f084e5e 100644 --- a/src/lib/libc/string/strerror.3 +++ b/src/lib/libc/string/strerror.3 @@ -1,4 +1,7 @@ +.\" $OpenBSD: strerror.3,v 1.15 2017/09/05 03:16:13 schwarze Exp $ +.\" .\" Copyright (c) 1980, 1991 Regents of the University of California. +.\" Copyright (c) 2017 Ingo Schwarze .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by @@ -29,46 +32,61 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strerror.3,v 1.14 2014/11/30 21:21:59 schwarze Exp $ -.\" -.Dd $Mdocdate: November 30 2014 $ +.Dd $Mdocdate: September 5 2017 $ .Dt STRERROR 3 .Os .Sh NAME .Nm strerror , +.Nm strerror_l , .Nm strerror_r .Nd get error message string .Sh SYNOPSIS .In string.h .Ft char * .Fn strerror "int errnum" +.Ft char * +.Fn strerror_l "int errnum" "locale_t locale" .Ft int .Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen" .Sh DESCRIPTION -The -.Fn strerror -and -.Fn strerror_r -functions map the error number +These functions map the error number .Fa errnum -to a language-dependent error message string. +to an error message string. .Pp .Fn strerror -returns a string containing a maximum of +and +.Fn strerror_l +return a string containing a maximum of .Dv NL_TEXTMAX characters, including the trailing NUL. -This string is not to be modified by the calling program, -but may be overwritten by subsequent calls to -.Fn strerror . +This string is not to be modified by the calling program. +The string returned by +.Fn strerror +may be overwritten by subsequent calls to +.Fn strerror +in any thread. +The string returned by +.Fn strerror_l +may be overwritten by subsequent calls to +.Fn strerror_l +in the same thread. .Pp .Fn strerror_r is a thread safe version of .Fn strerror that places the error message in the specified buffer .Fa strerrbuf . +.Pp +On +.Ox , +the global locale, the thread-specific locale, and the +.Fa locale +argument are ignored. .Sh RETURN VALUES .Fn strerror -returns a pointer to the error message string. +and +.Fn strerror_l +return a pointer to the error message string. If an error occurs, the error code is stored in .Va errno . .Pp @@ -77,11 +95,16 @@ returns zero upon successful completion. If an error occurs, the error code is stored in .Va errno and the error code is returned. -.Sh ERRORS +.Sh ENVIRONMENT +On other operating systems, the behaviour of .Fn strerror and .Fn strerror_r -may fail if: +may depend on the +.Dv LC_MESSAGES +.Xr locale 1 . +.Sh ERRORS +All these functions may fail if: .Bl -tag -width Er .It Bq Er EINVAL .Fa errnum @@ -91,7 +114,7 @@ The returned error string will consist of an error message that includes .El .Pp .Fn strerror_r -may fail if: +may also fail if: .Bl -tag -width Er .It Bq Er ERANGE The error message is larger than @@ -101,6 +124,7 @@ The message will be truncated to fit. .El .Sh SEE ALSO .Xr intro 2 , +.Xr newlocale 3 , .Xr perror 3 , .Xr setlocale 3 .Sh STANDARDS @@ -109,15 +133,20 @@ The function conforms to .St -isoC-99 . The +.Fn strerror_l +and .Fn strerror_r -function conforms to +functions conform to .St -p1003.1-2008 . .Sh HISTORY The .Fn strerror -function first appeared in -.Bx 4.3 Reno . -The +function has been available since +.Bx 4.3 Reno , .Fn strerror_r -function first appeared in -.Ox 3.3 . +since +.Ox 3.3 , +and +.Fn strerror_l +since +.Ox 6.2 . diff --git a/src/lib/libc/string/strerror_l.c b/src/lib/libc/string/strerror_l.c new file mode 100644 index 0000000000..c16be7a0cc --- /dev/null +++ b/src/lib/libc/string/strerror_l.c @@ -0,0 +1,33 @@ +/* $OpenBSD: strerror_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */ +/* + * Copyright (c) 2017 Ingo Schwarze + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "thread_private.h" + +char * +strerror_l(int errnum, locale_t locale) +{ + static char sel_buf[NL_TEXTMAX]; + _THREAD_PRIVATE_KEY(strerror_l); + char *p = _THREAD_PRIVATE(strerror_l, sel_buf, NULL); + + return p == NULL ? "no buffer available in strerror_l" : + strerror_r(errnum, p, sizeof(sel_buf)) ? + "strerror_r failure" : p; +} diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3 index 481f741cb7..1a96c8512e 100644 --- a/src/lib/libc/string/strxfrm.3 +++ b/src/lib/libc/string/strxfrm.3 @@ -1,4 +1,7 @@ +.\" $OpenBSD: strxfrm.3,v 1.11 2017/09/05 03:16:13 schwarze Exp $ +.\" .\" Copyright (c) 1990, 1991 The Regents of the University of California. +.\" Copyright (c) 2017 Ingo Schwarze .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by @@ -29,24 +32,27 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: strxfrm.3,v 1.10 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ +.Dd $Mdocdate: September 5 2017 $ .Dt STRXFRM 3 .Os .Sh NAME -.Nm strxfrm +.Nm strxfrm , +.Nm strxfrm_l .Nd transform a string under locale .Sh SYNOPSIS .In string.h .Ft size_t .Fn strxfrm "char *dst" "const char *src" "size_t n" +.Ft size_t +.Fn strxfrm_l "char *dst" "const char *src" "size_t n" "locale_t locale" .Sh DESCRIPTION The idea of .Fn strxfrm +and +.Fn strxfrm_l is to .Dq un-localize -a string: the function transforms +a string: the functions transform .Ar src , storing the result in .Ar dst , @@ -55,25 +61,42 @@ such that on transformed strings returns what .Xr strcoll 3 on the original untransformed strings would return. +.Pp +On +.Ox , +both have the same effect as +.Xr strlcpy 3 , +and the global locale, the thread-specific locale, and the +.Fa locale +argument are ignored. +.Sh ENVIRONMENT +On other operating systems, the behaviour of +.Fn strxfrm +may depend on the +.Dv LC_CTYPE +.Xr locale 1 . .Sh SEE ALSO -.Xr bcmp 3 , -.Xr memcmp 3 , +.Xr newlocale 3 , .Xr setlocale 3 , -.Xr strcasecmp 3 , .Xr strcmp 3 , -.Xr strcoll 3 +.Xr strcoll 3 , +.Xr strlcpy 3 , +.Xr wcsxfrm 3 .Sh STANDARDS The .Fn strxfrm function conforms to -.St -ansiC . +.St -ansiC , +and +.Fn strxfrm_l +to +.St -p1003.1-2008 . .Sh HISTORY The .Fn strxfrm -function first appeared in -.Bx 4.3 Reno . -.Sh BUGS -Since locales are not fully implemented on -.Ox , -.Fn strxfrm -just returns a copy of the original string. +function has been available since +.Bx 4.3 Reno , +and +.Fn strxfrm_l +since +.Ox 6.2 . diff --git a/src/lib/libc/string/strxfrm_l.c b/src/lib/libc/string/strxfrm_l.c new file mode 100644 index 0000000000..ff77947953 --- /dev/null +++ b/src/lib/libc/string/strxfrm_l.c @@ -0,0 +1,14 @@ +/* $OpenBSD: strxfrm_l.c,v 1.1 2017/09/05 03:16:14 schwarze Exp $ */ +/* + * Written in 2017 by Ingo Schwarze . + * Released into the public domain. + */ + +#include + +size_t +strxfrm_l(char *dst, const char *src, size_t n, + locale_t locale __attribute__((__unused__))) +{ + return strxfrm(dst, src, n); +} diff --git a/src/lib/libc/string/wcscasecmp.3 b/src/lib/libc/string/wcscasecmp.3 index daf397aece..9db4a829b9 100644 --- a/src/lib/libc/string/wcscasecmp.3 +++ b/src/lib/libc/string/wcscasecmp.3 @@ -1,7 +1,8 @@ -.\" $OpenBSD: wcscasecmp.3,v 1.4 2013/07/16 15:21:11 schwarze Exp $ +.\" $OpenBSD: wcscasecmp.3,v 1.5 2017/09/05 03:16:14 schwarze Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. +.\" Copyright (c) 2017 Ingo Schwarze .\" .\" This code is derived from software contributed to Berkeley by .\" Chris Torek. @@ -31,25 +32,43 @@ .\" .\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93 .\" -.Dd $Mdocdate: July 16 2013 $ +.Dd $Mdocdate: September 5 2017 $ .Dt WCSCASECMP 3 .Os .Sh NAME .Nm wcscasecmp , -.Nm wcsncasecmp +.Nm wcscasecmp_l , +.Nm wcsncasecmp , +.Nm wcsncasecmp_l .Nd compare wide strings, ignoring case .Sh SYNOPSIS .In wchar.h .Ft int -.Fn wcscasecmp "const wchar_t *s1" "const wchar_t *s2" +.Fo wcscasecmp +.Fa "const wchar_t *s1" +.Fa "const wchar_t *s2" +.Fc .Ft int -.Fn wcsncasecmp "const wchar_t *s1" "const wchar_t *s2" "size_t len" +.Fo wcscasecmp_l +.Fa "const wchar_t *s1" +.Fa "const wchar_t *s2" +.Fa "locale_t locale" +.Fc +.Ft int +.Fo wcsncasecmp +.Fa "const wchar_t *s1" +.Fa "const wchar_t *s2" +.Fa "size_t len" +.Fc +.Ft int +.Fo wcsncasecmp_l +.Fa "const wchar_t *s1" +.Fa "const wchar_t *s2" +.Fa "size_t len" +.Fa "locale_t locale" +.Fc .Sh DESCRIPTION -The -.Fn wcscasecmp -and -.Fn wcsncasecmp -functions compare the wide strings +These functions compare the wide strings .Fa s1 and .Fa s2 @@ -61,28 +80,49 @@ is lexicographically greater than, equal to, or less than after translation of each corresponding wide character to lower case. The wide strings themselves are not modified. .Pp +For the translation to lower case, +.Fn wcscasecmp +and +.Fn wcsncasecmp +use the thread-specific locale as defined with +.Xr uselocale 3 , +falling back to the global locale defined with +.Xr setlocale 3 . +.Fn wcscasecmp_l +and +.Fn wcsncasecmp_l +use the +.Fa locale +argument instead. +.Pp .Fn wcsncasecmp -compares at most +and +.Fn wcsncasecmp_l +compare at most .Fa len wide characters. .Sh SEE ALSO +.Xr newlocale 3 , +.Xr setlocale 3 , .Xr strcasecmp 3 , .Xr wcscmp 3 , .Xr wmemcmp 3 .Sh STANDARDS -The -.Fn wcscasecmp -and -.Fn wcsncasecmp -functions conform to +These functions conform to .St -p1003.1-2008 . .Sh HISTORY The .Fn wcscasecmp and .Fn wcsncasecmp -functions first appeared in -.Ox 5.0 . +functions have been available since +.Ox 5.0 , +and +.Fn wcscasecmp_l +and +.Fn wcsncasecmp_l +since +.Ox 6.2 . .Sh AUTHORS The .Ox diff --git a/src/lib/libc/string/wcscasecmp_l.c b/src/lib/libc/string/wcscasecmp_l.c new file mode 100644 index 0000000000..35b99225c1 --- /dev/null +++ b/src/lib/libc/string/wcscasecmp_l.c @@ -0,0 +1,63 @@ +/* $OpenBSD: wcscasecmp_l.c,v 1.1 2017/09/05 03:16:14 schwarze Exp $ */ + +/* + * Copyright (c) 2011 Marc Espie + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD + * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include "locale/runetype.h" + +int +wcscasecmp_l(const wchar_t *s1, const wchar_t *s2, locale_t locale) +{ + wchar_t l1, l2; + + while ((l1 = towlower_l(*s1++, locale)) == + (l2 = towlower_l(*s2++, locale))) { + if (l1 == 0) + return (0); + } + /* XXX assumes wchar_t = int */ + return ((rune_t)l1 - (rune_t)l2); +} + +int +wcsncasecmp_l(const wchar_t *s1, const wchar_t *s2, size_t n, locale_t locale) +{ + wchar_t l1, l2; + + if (n == 0) + return (0); + do { + if (((l1 = towlower_l(*s1++, locale))) != + (l2 = towlower_l(*s2++, locale))) { + /* XXX assumes wchar_t = int */ + return ((rune_t)l1 - (rune_t)l2); + } + if (l1 == 0) + break; + } while (--n != 0); + return (0); +} -- cgit v1.2.3-55-g6feb