xref: /titanic_44/usr/src/lib/libc/port/locale/localeconv.c (revision 2d08521bd15501c8370ba2153b9cca4f094979d0)
14297a3b0SGarrett D'Amore /*
2*2d08521bSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
54297a3b0SGarrett D'Amore  * Copyright (c) 1991, 1993
64297a3b0SGarrett D'Amore  *	The Regents of the University of California.  All rights reserved.
74297a3b0SGarrett D'Amore  *
84297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
94297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
104297a3b0SGarrett D'Amore  * are met:
114297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
124297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
134297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
144297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
154297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
164297a3b0SGarrett D'Amore  * 4. Neither the name of the University nor the names of its contributors
174297a3b0SGarrett D'Amore  *    may be used to endorse or promote products derived from this software
184297a3b0SGarrett D'Amore  *    without specific prior written permission.
194297a3b0SGarrett D'Amore  *
204297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304297a3b0SGarrett D'Amore  * SUCH DAMAGE.
314297a3b0SGarrett D'Amore  */
324297a3b0SGarrett D'Amore 
334297a3b0SGarrett D'Amore #ifndef	_LCONV_C99
344297a3b0SGarrett D'Amore #define	_LCONV_C99	/* so we get all the extensions */
354297a3b0SGarrett D'Amore #endif
364297a3b0SGarrett D'Amore 
374297a3b0SGarrett D'Amore #include "lint.h"
384297a3b0SGarrett D'Amore #include <locale.h>
394297a3b0SGarrett D'Amore #include "lmonetary.h"
404297a3b0SGarrett D'Amore #include "lnumeric.h"
41*2d08521bSGarrett D'Amore #include "localeimpl.h"
424297a3b0SGarrett D'Amore 
434297a3b0SGarrett D'Amore /*
44*2d08521bSGarrett D'Amore  * Return the current locale conversion.
45*2d08521bSGarrett D'Amore  *
46*2d08521bSGarrett D'Amore  * Note that XPG7 specifically states that localeconv's return value may
47*2d08521bSGarrett D'Amore  * be invalidated if the application calls setlocale() or uselocale() within
48*2d08521bSGarrett D'Amore  * the same thread.
494297a3b0SGarrett D'Amore  *
504297a3b0SGarrett D'Amore  * Because localeconv() may be called many times (especially by library
514297a3b0SGarrett D'Amore  * routines like printf() & strtod()), the approprate members of the
524297a3b0SGarrett D'Amore  * lconv structure are computed only when the monetary or numeric
534297a3b0SGarrett D'Amore  * locale has been changed.
544297a3b0SGarrett D'Amore  */
554297a3b0SGarrett D'Amore struct lconv *
localeconv(void)564297a3b0SGarrett D'Amore localeconv(void)
574297a3b0SGarrett D'Amore {
58*2d08521bSGarrett D'Amore 	struct lconv	*lconv;
59*2d08521bSGarrett D'Amore 	locale_t	loc;
60*2d08521bSGarrett D'Amore 	struct lc_monetary	*mptr;
61*2d08521bSGarrett D'Amore 	struct lc_numeric	*nptr;
624297a3b0SGarrett D'Amore 
63*2d08521bSGarrett D'Amore 	loc = uselocale(NULL);
64*2d08521bSGarrett D'Amore 	lconv = &loc->lconv;
654297a3b0SGarrett D'Amore 
66*2d08521bSGarrett D'Amore 	if (loc->loaded[LC_MONETARY] == 0) {
67*2d08521bSGarrett D'Amore 		mptr = loc->locdata[LC_MONETARY]->l_data[0];
684297a3b0SGarrett D'Amore 
69*2d08521bSGarrett D'Amore #define	M_ASSIGN_STR(NAME) (lconv->NAME = (char *)mptr->NAME)
70*2d08521bSGarrett D'Amore #define	M_ASSIGN_CHAR(NAME) (lconv->NAME = mptr->NAME[0])
71*2d08521bSGarrett D'Amore 
724297a3b0SGarrett D'Amore 		M_ASSIGN_STR(int_curr_symbol);
734297a3b0SGarrett D'Amore 		M_ASSIGN_STR(currency_symbol);
744297a3b0SGarrett D'Amore 		M_ASSIGN_STR(mon_decimal_point);
754297a3b0SGarrett D'Amore 		M_ASSIGN_STR(mon_thousands_sep);
764297a3b0SGarrett D'Amore 		M_ASSIGN_STR(mon_grouping);
774297a3b0SGarrett D'Amore 		M_ASSIGN_STR(positive_sign);
784297a3b0SGarrett D'Amore 		M_ASSIGN_STR(negative_sign);
794297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_frac_digits);
804297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(frac_digits);
814297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(p_cs_precedes);
824297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(p_sep_by_space);
834297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(n_cs_precedes);
844297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(n_sep_by_space);
854297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(p_sign_posn);
864297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(n_sign_posn);
874297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_p_cs_precedes);
884297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_n_cs_precedes);
894297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_p_sep_by_space);
904297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_n_sep_by_space);
914297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_p_sign_posn);
924297a3b0SGarrett D'Amore 		M_ASSIGN_CHAR(int_n_sign_posn);
93*2d08521bSGarrett D'Amore 		loc->loaded[LC_MONETARY] = 1;
944297a3b0SGarrett D'Amore 	}
954297a3b0SGarrett D'Amore 
96*2d08521bSGarrett D'Amore 	if (loc->loaded[LC_NUMERIC] == 0) {
97*2d08521bSGarrett D'Amore 		nptr = loc->locdata[LC_NUMERIC]->l_data[0];
984297a3b0SGarrett D'Amore 
99*2d08521bSGarrett D'Amore #define	N_ASSIGN_STR(NAME) (lconv->NAME = (char *)nptr->NAME)
1004297a3b0SGarrett D'Amore 
1014297a3b0SGarrett D'Amore 		N_ASSIGN_STR(decimal_point);
1024297a3b0SGarrett D'Amore 		N_ASSIGN_STR(thousands_sep);
1034297a3b0SGarrett D'Amore 		N_ASSIGN_STR(grouping);
104*2d08521bSGarrett D'Amore 		loc->loaded[LC_NUMERIC] = 1;
1054297a3b0SGarrett D'Amore 	}
1064297a3b0SGarrett D'Amore 
107*2d08521bSGarrett D'Amore 	return (lconv);
1084297a3b0SGarrett D'Amore }
109