4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright (c) 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/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Use is subject to license terms.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include "lint.h"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <ctype.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <limits.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore#include <stddef.h>
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amorestatic const char nogrouping[] = { CHAR_MAX, '\0' };
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore/*
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * Internal helper used to convert grouping sequences from string
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * representation into POSIX specified form, i.e.
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore * "3;3;-1" -> "\003\003\177\000"
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amoreconst char *
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore__fix_locale_grouping_str(const char *str)
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore{
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char *src, *dst;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore char n;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (str == NULL || *str == '\0') {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (nogrouping);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore for (src = (char *)str, dst = (char *)str; *src != '\0'; src++) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* input string examples: "3;3", "3;2;-1" */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (*src == ';')
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore continue;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (*src == '-' && *(src+1) == '1') {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *dst++ = CHAR_MAX;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore src++;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore continue;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (!isdigit((unsigned char)*src)) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* broken grouping string */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (nogrouping);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* assume all numbers <= 99 */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore n = *src - '0';
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (isdigit((unsigned char)*(src+1))) {
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore src++;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore n *= 10;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore n += *src - '0';
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *dst = n;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore /* NOTE: assume all input started with "0" as 'no grouping' */
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore if (*dst == '\0')
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return ((dst == (char *)str) ? nogrouping : str);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore dst++;
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore }
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore *dst = '\0';
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore return (str);
4297a3b0d0a35d80f86fff155e288e885a100e6dGarrett D'Amore}