xref: /illumos-gate/usr/src/lib/libc/port/locale/wctrans.c (revision 4a38094c)
1 /*
2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
4  * Copyright (c) 2002 Tim J. Robbins.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "lint.h"
30 #include <errno.h>
31 #include <string.h>
32 #include <wctype.h>
33 #include <locale.h>
34 
35 enum {
36 	_WCT_ERROR	= 0,
37 	_WCT_TOLOWER	= 1,
38 	_WCT_TOUPPER	= 2
39 };
40 
41 wint_t
towctrans_l(wint_t wc,wctrans_t desc,locale_t loc)42 towctrans_l(wint_t wc, wctrans_t desc, locale_t loc)
43 {
44 	switch (desc) {
45 	case _WCT_TOLOWER:
46 		wc = towlower_l(wc, loc);
47 		break;
48 	case _WCT_TOUPPER:
49 		wc = towupper_l(wc, loc);
50 		break;
51 	case _WCT_ERROR:
52 	default:
53 		errno = EINVAL;
54 		break;
55 	}
56 
57 	return (wc);
58 }
59 
60 wint_t
towctrans(wint_t wc,wctrans_t desc)61 towctrans(wint_t wc, wctrans_t desc)
62 {
63 	return (towctrans_l(wc, desc, uselocale(NULL)));
64 }
65 
66 /*
67  * For *now* we don't support locale sensitive transforms besides toupper
68  * and tolower.
69  */
70 wctrans_t
wctrans_l(const char * charclass,locale_t loc __unused)71 wctrans_l(const char *charclass, locale_t loc __unused)
72 {
73 	struct {
74 		const char	*name;
75 		wctrans_t	 trans;
76 	} ccls[] = {
77 		{ "tolower",	_WCT_TOLOWER },
78 		{ "toupper",	_WCT_TOUPPER },
79 		{ NULL,		_WCT_ERROR },		/* Default */
80 	};
81 	int i;
82 
83 	i = 0;
84 	while (ccls[i].name != NULL && strcmp(ccls[i].name, charclass) != 0)
85 		i++;
86 
87 	if (ccls[i].trans == _WCT_ERROR)
88 		errno = EINVAL;
89 	return (ccls[i].trans);
90 }
91 
92 wctrans_t
wctrans(const char * charclass)93 wctrans(const char *charclass)
94 {
95 	return (wctrans_l(charclass, uselocale(NULL)));
96 }
97