4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org>
6b5e5868e7ebf1aff3a5abd7d0c4ef0e5fbf3648Garrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 2001, 2003 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 <langinfo.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <locale.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stdlib.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <string.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lnumeric.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lmessages.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lmonetary.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "timelocal.h"
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore#include "localeimpl.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#define _REL(BASE) ((int)item-BASE)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#pragma weak _nl_langinfo = nl_langinfo
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorechar *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorenl_langinfo_l(nl_item item, locale_t loc)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *ret, *s, *cs;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore struct locdata *ldata;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const struct lc_monetary *lmon = loc->monetary;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const struct lc_numeric *lnum = loc->numeric;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const struct lc_messages *lmsgs = loc->messages;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore const struct lc_time *ltime = loc->time;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore switch (item) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case CODESET:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * The codeset is the suffix of a locale, for most it will
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * will be UTF-8, as in "en_US.UTF-8". Short form locales are
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * not supported. Note also that although FreeBSD uses
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * US-ASCII, Solaris historically has reported "646" for the
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * C locale.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Note that this code will need to change if we ever support
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * POSIX defined locale variants (suffixes with an @ sign)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ldata = loc->locdata[LC_CTYPE];
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore s = ldata ? ldata->l_lname : NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (s != NULL) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if ((cs = strchr(s, '.')) != NULL)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = cs + 1;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore else if (strcmp(s, "C") == 0 || strcmp(s, "POSIX") == 0)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "646";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case D_T_FMT:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->c_fmt;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case D_FMT:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->x_fmt;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case T_FMT:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->X_fmt;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case T_FMT_AMPM:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->ampm_fmt;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case AM_STR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->am;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case PM_STR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->pm;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case DAY_1: case DAY_2: case DAY_3:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case DAY_4: case DAY_5: case DAY_6: case DAY_7:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->weekday[_REL(DAY_1)];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ABDAY_1: case ABDAY_2: case ABDAY_3:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->wday[_REL(ABDAY_1)];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case MON_1: case MON_2: case MON_3: case MON_4:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case MON_5: case MON_6: case MON_7: case MON_8:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case MON_9: case MON_10: case MON_11: case MON_12:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->month[_REL(MON_1)];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->mon[_REL(ABMON_1)];
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ERA:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* XXX: need to be implemented */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ERA_D_FMT:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* XXX: need to be implemented */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ERA_D_T_FMT:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* XXX: need to be implemented */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ERA_T_FMT:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* XXX: need to be implemented */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case ALT_DIGITS:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* XXX: need to be implemented */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case RADIXCHAR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lnum->decimal_point;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case THOUSEP:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lnum->thousands_sep;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case YESEXPR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lmsgs->yesexpr;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case NOEXPR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lmsgs->noexpr;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * YESSTR and NOSTR items were removed from Issue 7. But
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * older applications might still need them. Their use is
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * discouraged.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case YESSTR: /* LEGACY */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lmsgs->yesstr;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case NOSTR: /* LEGACY */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)lmsgs->nostr;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * SUSv2 special formatted currency string
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case CRNCYSTR:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = lmon->crncystr;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore case _DATE_FMT: /* Solaris specific extension */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore ret = (char *)ltime->date_fmt;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore break;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Note that FreeBSD also had a private D_MD_ORDER, but that appears
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * to have been specific to FreeBSD, so we have not included it here.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore default:
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore ret = "";
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (ret);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorechar *
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorenl_langinfo(nl_item item)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (nl_langinfo_l(item, uselocale(NULL)));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore}