4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Redistribution and use in source and binary forms, with or without
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * modification, are permitted provided that the following conditions
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * are met:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * documentation and/or other materials provided with the distribution.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUCH DAMAGE.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lint.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stddef.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdlib.h>
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include <stdio.h>
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include <string.h>
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include <errno.h>
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include "libc.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "ldpart.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lmonetary.h"
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include "localeimpl.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreextern const char *__fix_locale_grouping_str(const char *);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#define LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary) / sizeof (char *))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define LCMONETARY_SIZE_MIN \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (offsetof(struct lc_monetary, int_p_cs_precedes) / sizeof (char *))
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char empty[] = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char numempty[] = { CHAR_MAX, '\0' };
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct lc_monetary lc_monetary_posix = {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* int_curr_symbol */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* currency_symbol */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* mon_decimal_point */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* mon_thousands_sep */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* mon_grouping */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* positive_sign */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore empty, /* negative_sign */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_frac_digits */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* frac_digits */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* p_cs_precedes */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* p_sep_by_space */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* n_cs_precedes */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* n_sep_by_space */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* p_sign_posn */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* n_sign_posn */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_p_cs_precedes */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_n_cs_precedes */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_p_sep_by_space */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_n_sep_by_space */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore numempty, /* int_p_sign_posn */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore numempty, /* int_n_sign_posn */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore empty /* crncystr */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore};
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct locdata __posix_monetary_locdata = {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .l_lname = "C",
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore .l_data = { &lc_monetary_posix }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore};
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic char
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorecnv(const char *str)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore int i = strtol(str, NULL, 10);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (i == -1)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore i = CHAR_MAX;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return ((char)i);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestruct locdata *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore__lc_monetary_load(const char *name)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore int ret;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int clen;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore struct lc_monetary *lmon;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore struct locdata *ldata;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if ((ldata = __locdata_alloc(name, sizeof (*lmon))) == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon = ldata->l_data[0];
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = __part_load_locale(name, (char **)&ldata->l_data[1],
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore "LC_MONETARY", LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (const char **)lmon);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (ret != _LDP_LOADED) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore __locdata_free(ldata);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore errno = EINVAL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /* special storage for currency string */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore clen = strlen(lmon->currency_symbol) + 2;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ldata->l_data[2] = libc_malloc(clen);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon->crncystr = ldata->l_data[2];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon->mon_grouping = __fix_locale_grouping_str(lmon->mon_grouping);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define M_ASSIGN_CHAR(NAME) \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (((char *)lmon->NAME)[0] = cnv(lmon->NAME))
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(int_frac_digits);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(frac_digits);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(p_cs_precedes);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(p_sep_by_space);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(n_cs_precedes);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(n_sep_by_space);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(p_sign_posn);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(n_sign_posn);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * The six additional C99 international monetary formatting
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * parameters default to the national parameters when
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * reading FreeBSD LC_MONETARY data files.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#define M_ASSIGN_ICHAR(NAME) \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (lmon->int_##NAME == NULL) \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon->int_##NAME = lmon->NAME; \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore else \
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_CHAR(int_##NAME);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(p_cs_precedes);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(n_cs_precedes);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(p_sep_by_space);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(n_sep_by_space);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(p_sign_posn);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore M_ASSIGN_ICHAR(n_sign_posn);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Now calculate the currency string (CRNCYSTR) for nl_langinfo.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * This is a legacy SUSv2 interface.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if ((lmon->p_cs_precedes[0] == lmon->n_cs_precedes[0]) &&
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (lmon->currency_symbol[0] != '\0')) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore char sign = '\0';
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore switch (lmon->p_cs_precedes[0]) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore case 0:
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore sign = '+';
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore case 1:
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore sign = '-';
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore case CHAR_MAX:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Substitute currency string for radix character.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * To the best of my knowledge, no locale uses this.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (strcmp(lmon->mon_decimal_point,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon->currency_symbol) == 0)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore sign = '.';
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) snprintf(lmon->crncystr, clen, "%c%s", sign,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore lmon->currency_symbol);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (ldata);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}