1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4f2d0f427SAlexey Zelkin * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
51bd7723dSAlexey Zelkin * All rights reserved.
61bd7723dSAlexey Zelkin *
71bd7723dSAlexey Zelkin * Redistribution and use in source and binary forms, with or without
81bd7723dSAlexey Zelkin * modification, are permitted provided that the following conditions
91bd7723dSAlexey Zelkin * are met:
101bd7723dSAlexey Zelkin * 1. Redistributions of source code must retain the above copyright
111bd7723dSAlexey Zelkin * notice, this list of conditions and the following disclaimer.
121bd7723dSAlexey Zelkin * 2. Redistributions in binary form must reproduce the above copyright
131bd7723dSAlexey Zelkin * notice, this list of conditions and the following disclaimer in the
141bd7723dSAlexey Zelkin * documentation and/or other materials provided with the distribution.
151bd7723dSAlexey Zelkin *
161bd7723dSAlexey Zelkin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171bd7723dSAlexey Zelkin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181bd7723dSAlexey Zelkin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191bd7723dSAlexey Zelkin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201bd7723dSAlexey Zelkin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211bd7723dSAlexey Zelkin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221bd7723dSAlexey Zelkin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231bd7723dSAlexey Zelkin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241bd7723dSAlexey Zelkin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251bd7723dSAlexey Zelkin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261bd7723dSAlexey Zelkin * SUCH DAMAGE.
271bd7723dSAlexey Zelkin */
281bd7723dSAlexey Zelkin
29cb03ae30SAlexey Zelkin #include <ctype.h>
301bd7723dSAlexey Zelkin #include <limits.h>
31ba4a5c92SAlexey Zelkin #include <stddef.h>
321bd7723dSAlexey Zelkin
333bac3490SEd Maste static const char nogrouping[] = { '\0' };
341bd7723dSAlexey Zelkin
351bd7723dSAlexey Zelkin /*
36683fe113SAlexey Zelkin * Internal helper used to convert grouping sequences from string
37683fe113SAlexey Zelkin * representation into POSIX specified form, i.e.
38683fe113SAlexey Zelkin *
39683fe113SAlexey Zelkin * "3;3;-1" -> "\003\003\177\000"
401bd7723dSAlexey Zelkin */
411bd7723dSAlexey Zelkin
421bd7723dSAlexey Zelkin const char *
__fix_locale_grouping_str(const char * str)43683fe113SAlexey Zelkin __fix_locale_grouping_str(const char *str)
44683fe113SAlexey Zelkin {
451bd7723dSAlexey Zelkin char *src, *dst;
46cb03ae30SAlexey Zelkin char n;
471bd7723dSAlexey Zelkin
48ba4a5c92SAlexey Zelkin if (str == NULL || *str == '\0') {
491bd7723dSAlexey Zelkin return nogrouping;
501bd7723dSAlexey Zelkin }
51ba4a5c92SAlexey Zelkin
52ba4a5c92SAlexey Zelkin for (src = (char*)str, dst = (char*)str; *src != '\0'; src++) {
531bd7723dSAlexey Zelkin
541bd7723dSAlexey Zelkin /* input string examples: "3;3", "3;2;-1" */
551bd7723dSAlexey Zelkin if (*src == ';')
561bd7723dSAlexey Zelkin continue;
571bd7723dSAlexey Zelkin
581bd7723dSAlexey Zelkin if (*src == '-' && *(src+1) == '1') {
591bd7723dSAlexey Zelkin *dst++ = CHAR_MAX;
601bd7723dSAlexey Zelkin src++;
611bd7723dSAlexey Zelkin continue;
621bd7723dSAlexey Zelkin }
631bd7723dSAlexey Zelkin
64cb03ae30SAlexey Zelkin if (!isdigit((unsigned char)*src)) {
651bd7723dSAlexey Zelkin /* broken grouping string */
661bd7723dSAlexey Zelkin return nogrouping;
671bd7723dSAlexey Zelkin }
681bd7723dSAlexey Zelkin
69b21656a8SAlexey Zelkin /* assume all numbers <= 99 */
70b21656a8SAlexey Zelkin n = *src - '0';
71b21656a8SAlexey Zelkin if (isdigit((unsigned char)*(src+1))) {
72b21656a8SAlexey Zelkin src++;
73cb03ae30SAlexey Zelkin n *= 10;
74cb03ae30SAlexey Zelkin n += *src - '0';
75cb03ae30SAlexey Zelkin }
76cb03ae30SAlexey Zelkin
77c80a9160SAndrey A. Chernov *dst = n;
78c80a9160SAndrey A. Chernov /* NOTE: assume all input started with "0" as 'no grouping' */
79c80a9160SAndrey A. Chernov if (*dst == '\0')
80c80a9160SAndrey A. Chernov return (dst == (char*)str) ? nogrouping : str;
81c80a9160SAndrey A. Chernov dst++;
821bd7723dSAlexey Zelkin }
831bd7723dSAlexey Zelkin *dst = '\0';
841bd7723dSAlexey Zelkin return str;
851bd7723dSAlexey Zelkin }
86