14297a3b0SGarrett D'Amore /*
262c3776aSGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
44297a3b0SGarrett D'Amore * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
54297a3b0SGarrett D'Amore * All rights reserved.
64297a3b0SGarrett D'Amore *
74297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without
84297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions
94297a3b0SGarrett D'Amore * are met:
104297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
114297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer.
124297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
134297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
144297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution.
154297a3b0SGarrett D'Amore *
164297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264297a3b0SGarrett D'Amore * SUCH DAMAGE.
274297a3b0SGarrett D'Amore */
284297a3b0SGarrett D'Amore
294297a3b0SGarrett D'Amore #include "lint.h"
304297a3b0SGarrett D'Amore #include <limits.h>
314297a3b0SGarrett D'Amore #include <stddef.h>
324297a3b0SGarrett D'Amore #include <stdlib.h>
3362c3776aSGarrett D'Amore #include <stdio.h>
3462c3776aSGarrett D'Amore #include <string.h>
3562c3776aSGarrett D'Amore #include <errno.h>
3662c3776aSGarrett D'Amore #include "libc.h"
374297a3b0SGarrett D'Amore #include "ldpart.h"
384297a3b0SGarrett D'Amore #include "lmonetary.h"
3962c3776aSGarrett D'Amore #include "localeimpl.h"
404297a3b0SGarrett D'Amore
414297a3b0SGarrett D'Amore extern const char *__fix_locale_grouping_str(const char *);
424297a3b0SGarrett D'Amore
4362c3776aSGarrett D'Amore #define LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary) / sizeof (char *))
444297a3b0SGarrett D'Amore #define LCMONETARY_SIZE_MIN \
4562c3776aSGarrett D'Amore (offsetof(struct lc_monetary, int_p_cs_precedes) / sizeof (char *))
464297a3b0SGarrett D'Amore
474297a3b0SGarrett D'Amore static char empty[] = "";
484297a3b0SGarrett D'Amore static char numempty[] = { CHAR_MAX, '\0' };
494297a3b0SGarrett D'Amore
5062c3776aSGarrett D'Amore struct lc_monetary lc_monetary_posix = {
514297a3b0SGarrett D'Amore empty, /* int_curr_symbol */
524297a3b0SGarrett D'Amore empty, /* currency_symbol */
534297a3b0SGarrett D'Amore empty, /* mon_decimal_point */
544297a3b0SGarrett D'Amore empty, /* mon_thousands_sep */
554297a3b0SGarrett D'Amore numempty, /* mon_grouping */
564297a3b0SGarrett D'Amore empty, /* positive_sign */
574297a3b0SGarrett D'Amore empty, /* negative_sign */
584297a3b0SGarrett D'Amore numempty, /* int_frac_digits */
594297a3b0SGarrett D'Amore numempty, /* frac_digits */
604297a3b0SGarrett D'Amore numempty, /* p_cs_precedes */
614297a3b0SGarrett D'Amore numempty, /* p_sep_by_space */
624297a3b0SGarrett D'Amore numempty, /* n_cs_precedes */
634297a3b0SGarrett D'Amore numempty, /* n_sep_by_space */
644297a3b0SGarrett D'Amore numempty, /* p_sign_posn */
654297a3b0SGarrett D'Amore numempty, /* n_sign_posn */
664297a3b0SGarrett D'Amore numempty, /* int_p_cs_precedes */
674297a3b0SGarrett D'Amore numempty, /* int_n_cs_precedes */
684297a3b0SGarrett D'Amore numempty, /* int_p_sep_by_space */
694297a3b0SGarrett D'Amore numempty, /* int_n_sep_by_space */
704297a3b0SGarrett D'Amore numempty, /* int_p_sign_posn */
7162c3776aSGarrett D'Amore numempty, /* int_n_sign_posn */
7262c3776aSGarrett D'Amore empty /* crncystr */
734297a3b0SGarrett D'Amore };
744297a3b0SGarrett D'Amore
7562c3776aSGarrett D'Amore struct locdata __posix_monetary_locdata = {
7662c3776aSGarrett D'Amore .l_lname = "C",
7762c3776aSGarrett D'Amore .l_data = { &lc_monetary_posix }
7862c3776aSGarrett D'Amore };
794297a3b0SGarrett D'Amore
804297a3b0SGarrett D'Amore static char
cnv(const char * str)814297a3b0SGarrett D'Amore cnv(const char *str)
824297a3b0SGarrett D'Amore {
834297a3b0SGarrett D'Amore int i = strtol(str, NULL, 10);
844297a3b0SGarrett D'Amore
854297a3b0SGarrett D'Amore if (i == -1)
864297a3b0SGarrett D'Amore i = CHAR_MAX;
874297a3b0SGarrett D'Amore return ((char)i);
884297a3b0SGarrett D'Amore }
894297a3b0SGarrett D'Amore
9062c3776aSGarrett D'Amore struct locdata *
__lc_monetary_load(const char * name)9162c3776aSGarrett D'Amore __lc_monetary_load(const char *name)
924297a3b0SGarrett D'Amore {
934297a3b0SGarrett D'Amore int ret;
9462c3776aSGarrett D'Amore int clen;
9562c3776aSGarrett D'Amore struct lc_monetary *lmon;
9662c3776aSGarrett D'Amore struct locdata *ldata;
974297a3b0SGarrett D'Amore
9862c3776aSGarrett D'Amore if ((ldata = __locdata_alloc(name, sizeof (*lmon))) == NULL) {
9962c3776aSGarrett D'Amore return (NULL);
10062c3776aSGarrett D'Amore }
10162c3776aSGarrett D'Amore lmon = ldata->l_data[0];
10262c3776aSGarrett D'Amore
10362c3776aSGarrett D'Amore ret = __part_load_locale(name, (char **)&ldata->l_data[1],
10462c3776aSGarrett D'Amore "LC_MONETARY", LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
10562c3776aSGarrett D'Amore (const char **)lmon);
10662c3776aSGarrett D'Amore
10762c3776aSGarrett D'Amore if (ret != _LDP_LOADED) {
10862c3776aSGarrett D'Amore __locdata_free(ldata);
10962c3776aSGarrett D'Amore errno = EINVAL;
11062c3776aSGarrett D'Amore return (NULL);
11162c3776aSGarrett D'Amore }
11262c3776aSGarrett D'Amore
11362c3776aSGarrett D'Amore /* special storage for currency string */
11462c3776aSGarrett D'Amore clen = strlen(lmon->currency_symbol) + 2;
11562c3776aSGarrett D'Amore ldata->l_data[2] = libc_malloc(clen);
11662c3776aSGarrett D'Amore lmon->crncystr = ldata->l_data[2];
11762c3776aSGarrett D'Amore
11862c3776aSGarrett D'Amore lmon->mon_grouping = __fix_locale_grouping_str(lmon->mon_grouping);
1194297a3b0SGarrett D'Amore
1204297a3b0SGarrett D'Amore #define M_ASSIGN_CHAR(NAME) \
12162c3776aSGarrett D'Amore (((char *)lmon->NAME)[0] = cnv(lmon->NAME))
1224297a3b0SGarrett D'Amore
1234297a3b0SGarrett D'Amore M_ASSIGN_CHAR(int_frac_digits);
1244297a3b0SGarrett D'Amore M_ASSIGN_CHAR(frac_digits);
1254297a3b0SGarrett D'Amore M_ASSIGN_CHAR(p_cs_precedes);
1264297a3b0SGarrett D'Amore M_ASSIGN_CHAR(p_sep_by_space);
1274297a3b0SGarrett D'Amore M_ASSIGN_CHAR(n_cs_precedes);
1284297a3b0SGarrett D'Amore M_ASSIGN_CHAR(n_sep_by_space);
1294297a3b0SGarrett D'Amore M_ASSIGN_CHAR(p_sign_posn);
1304297a3b0SGarrett D'Amore M_ASSIGN_CHAR(n_sign_posn);
1314297a3b0SGarrett D'Amore
1324297a3b0SGarrett D'Amore /*
1334297a3b0SGarrett D'Amore * The six additional C99 international monetary formatting
1344297a3b0SGarrett D'Amore * parameters default to the national parameters when
1354297a3b0SGarrett D'Amore * reading FreeBSD LC_MONETARY data files.
1364297a3b0SGarrett D'Amore */
1374297a3b0SGarrett D'Amore #define M_ASSIGN_ICHAR(NAME) \
13862c3776aSGarrett D'Amore if (lmon->int_##NAME == NULL) \
13962c3776aSGarrett D'Amore lmon->int_##NAME = lmon->NAME; \
1404297a3b0SGarrett D'Amore else \
1414297a3b0SGarrett D'Amore M_ASSIGN_CHAR(int_##NAME);
1424297a3b0SGarrett D'Amore
1434297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(p_cs_precedes);
1444297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(n_cs_precedes);
1454297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(p_sep_by_space);
1464297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(n_sep_by_space);
1474297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(p_sign_posn);
1484297a3b0SGarrett D'Amore M_ASSIGN_ICHAR(n_sign_posn);
14962c3776aSGarrett D'Amore
15062c3776aSGarrett D'Amore /*
15162c3776aSGarrett D'Amore * Now calculate the currency string (CRNCYSTR) for nl_langinfo.
15262c3776aSGarrett D'Amore * This is a legacy SUSv2 interface.
15362c3776aSGarrett D'Amore */
15462c3776aSGarrett D'Amore if ((lmon->p_cs_precedes[0] == lmon->n_cs_precedes[0]) &&
15562c3776aSGarrett D'Amore (lmon->currency_symbol[0] != '\0')) {
15662c3776aSGarrett D'Amore char sign = '\0';
15762c3776aSGarrett D'Amore switch (lmon->p_cs_precedes[0]) {
15862c3776aSGarrett D'Amore case 0:
159*f6b47728SGarrett D'Amore sign = '+';
16062c3776aSGarrett D'Amore break;
16162c3776aSGarrett D'Amore case 1:
162*f6b47728SGarrett D'Amore sign = '-';
16362c3776aSGarrett D'Amore break;
16462c3776aSGarrett D'Amore case CHAR_MAX:
16562c3776aSGarrett D'Amore /*
16662c3776aSGarrett D'Amore * Substitute currency string for radix character.
16762c3776aSGarrett D'Amore * To the best of my knowledge, no locale uses this.
16862c3776aSGarrett D'Amore */
16962c3776aSGarrett D'Amore if (strcmp(lmon->mon_decimal_point,
17062c3776aSGarrett D'Amore lmon->currency_symbol) == 0)
17162c3776aSGarrett D'Amore sign = '.';
17262c3776aSGarrett D'Amore break;
1734297a3b0SGarrett D'Amore }
17462c3776aSGarrett D'Amore (void) snprintf(lmon->crncystr, clen, "%c%s", sign,
17562c3776aSGarrett D'Amore lmon->currency_symbol);
1764297a3b0SGarrett D'Amore }
1774297a3b0SGarrett D'Amore
17862c3776aSGarrett D'Amore return (ldata);
1794297a3b0SGarrett D'Amore }
180