1350a3d3eSAndrey A. Chernov /*- 2350a3d3eSAndrey A. Chernov * Copyright (c) 1993 3350a3d3eSAndrey A. Chernov * The Regents of the University of California. All rights reserved. 4350a3d3eSAndrey A. Chernov * 5350a3d3eSAndrey A. Chernov * This code is derived from software contributed to Berkeley by 6350a3d3eSAndrey A. Chernov * Paul Borman at Krystal Technologies. 7350a3d3eSAndrey A. Chernov * 83c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 93c87aa1dSDavid Chisnall * All rights reserved. 103c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 113c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 123c87aa1dSDavid Chisnall * 13350a3d3eSAndrey A. Chernov * Redistribution and use in source and binary forms, with or without 14350a3d3eSAndrey A. Chernov * modification, are permitted provided that the following conditions 15350a3d3eSAndrey A. Chernov * are met: 16350a3d3eSAndrey A. Chernov * 1. Redistributions of source code must retain the above copyright 17350a3d3eSAndrey A. Chernov * notice, this list of conditions and the following disclaimer. 18350a3d3eSAndrey A. Chernov * 2. Redistributions in binary form must reproduce the above copyright 19350a3d3eSAndrey A. Chernov * notice, this list of conditions and the following disclaimer in the 20350a3d3eSAndrey A. Chernov * documentation and/or other materials provided with the distribution. 21350a3d3eSAndrey A. Chernov * 4. Neither the name of the University nor the names of its contributors 22350a3d3eSAndrey A. Chernov * may be used to endorse or promote products derived from this software 23350a3d3eSAndrey A. Chernov * without specific prior written permission. 24350a3d3eSAndrey A. Chernov * 25350a3d3eSAndrey A. Chernov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26350a3d3eSAndrey A. Chernov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27350a3d3eSAndrey A. Chernov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28350a3d3eSAndrey A. Chernov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29350a3d3eSAndrey A. Chernov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30350a3d3eSAndrey A. Chernov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31350a3d3eSAndrey A. Chernov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32350a3d3eSAndrey A. Chernov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33350a3d3eSAndrey A. Chernov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34350a3d3eSAndrey A. Chernov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35350a3d3eSAndrey A. Chernov * SUCH DAMAGE. 36350a3d3eSAndrey A. Chernov */ 37350a3d3eSAndrey A. Chernov 38333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 40333fc21eSDavid E. O'Brien 4161310091SStefan Farfeleder #include <ctype.h> 422f04ec53SAndrey A. Chernov #include <stdio.h> 43a0998ce6STim J. Robbins #include <runetype.h> 443c87aa1dSDavid Chisnall #include <wchar.h> 453c87aa1dSDavid Chisnall #include "mblocal.h" 462f04ec53SAndrey A. Chernov 47abbd8902SMike Barcroft __ct_rune_t 48c83f3fc4SCraig Rodrigues ___toupper_l(__ct_rune_t c, locale_t l) 492f04ec53SAndrey A. Chernov { 5045a11576STim J. Robbins size_t lim; 513c87aa1dSDavid Chisnall FIX_LOCALE(l); 5283c476cdSDimitry Andric _RuneRange *rr = &XLOCALE_CTYPE(l)->runes->__mapupper_ext; 5345a11576STim J. Robbins _RuneEntry *base, *re; 542f04ec53SAndrey A. Chernov 557871e368SAndrey A. Chernov if (c < 0 || c == EOF) 56f9cbdf41SAndrey A. Chernov return(c); 577871e368SAndrey A. Chernov 5845a11576STim J. Robbins /* Binary search -- see bsearch.c for explanation. */ 59ddc1ededSTim J. Robbins base = rr->__ranges; 60ddc1ededSTim J. Robbins for (lim = rr->__nranges; lim != 0; lim >>= 1) { 6145a11576STim J. Robbins re = base + (lim >> 1); 62ddc1ededSTim J. Robbins if (re->__min <= c && c <= re->__max) 633c87aa1dSDavid Chisnall { 64ddc1ededSTim J. Robbins return (re->__map + c - re->__min); 653c87aa1dSDavid Chisnall } 66ddc1ededSTim J. Robbins else if (c > re->__max) { 6745a11576STim J. Robbins base = re + 1; 6845a11576STim J. Robbins lim--; 6945a11576STim J. Robbins } 702f04ec53SAndrey A. Chernov } 718b96e6c9SAndrey A. Chernov 722f04ec53SAndrey A. Chernov return(c); 732f04ec53SAndrey A. Chernov } 743c87aa1dSDavid Chisnall __ct_rune_t 75c83f3fc4SCraig Rodrigues ___toupper(__ct_rune_t c) 763c87aa1dSDavid Chisnall { 773c87aa1dSDavid Chisnall return ___toupper_l(c, __get_locale()); 783c87aa1dSDavid Chisnall } 79