xref: /freebsd/lib/libc/stdlib/strfmon.c (revision d915a14ef094c8dfc1a5aee70e135abfec01d0f1)
19d430a59SAlexey Zelkin /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*d915a14eSPedro F. Giffuni  *
452d6b430SAlexey Zelkin  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
59d430a59SAlexey Zelkin  * All rights reserved.
69d430a59SAlexey Zelkin  *
73c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
83c87aa1dSDavid Chisnall  * All rights reserved.
93c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
103c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
113c87aa1dSDavid Chisnall  *
129d430a59SAlexey Zelkin  * Redistribution and use in source and binary forms, with or without
139d430a59SAlexey Zelkin  * modification, are permitted provided that the following conditions
149d430a59SAlexey Zelkin  * are met:
159d430a59SAlexey Zelkin  * 1. Redistributions of source code must retain the above copyright
169d430a59SAlexey Zelkin  *    notice, this list of conditions and the following disclaimer.
179d430a59SAlexey Zelkin  * 2. Redistributions in binary form must reproduce the above copyright
189d430a59SAlexey Zelkin  *    notice, this list of conditions and the following disclaimer in the
199d430a59SAlexey Zelkin  *    documentation and/or other materials provided with the distribution.
209d430a59SAlexey Zelkin  *
219d430a59SAlexey Zelkin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
229d430a59SAlexey Zelkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239d430a59SAlexey Zelkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249d430a59SAlexey Zelkin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
259d430a59SAlexey Zelkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269d430a59SAlexey Zelkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279d430a59SAlexey Zelkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289d430a59SAlexey Zelkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299d430a59SAlexey Zelkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309d430a59SAlexey Zelkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319d430a59SAlexey Zelkin  * SUCH DAMAGE.
329d430a59SAlexey Zelkin  *
339d430a59SAlexey Zelkin  */
349d430a59SAlexey Zelkin 
35333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
36333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
379d430a59SAlexey Zelkin 
389d430a59SAlexey Zelkin #include <sys/types.h>
399d430a59SAlexey Zelkin #include <ctype.h>
409d430a59SAlexey Zelkin #include <errno.h>
419d430a59SAlexey Zelkin #include <limits.h>
429d430a59SAlexey Zelkin #include <locale.h>
4361310091SStefan Farfeleder #include <monetary.h>
449d430a59SAlexey Zelkin #include <stdarg.h>
459d430a59SAlexey Zelkin #include <stdio.h>
469d430a59SAlexey Zelkin #include <stdlib.h>
479d430a59SAlexey Zelkin #include <string.h>
483c87aa1dSDavid Chisnall #include "xlocale_private.h"
499d430a59SAlexey Zelkin 
509d430a59SAlexey Zelkin /* internal flags */
519d430a59SAlexey Zelkin #define	NEED_GROUPING		0x01	/* print digits grouped (default) */
529d430a59SAlexey Zelkin #define	SIGN_POSN_USED		0x02	/* '+' or '(' usage flag */
539d430a59SAlexey Zelkin #define	LOCALE_POSN		0x04	/* use locale defined +/- (default) */
549d430a59SAlexey Zelkin #define	PARENTH_POSN		0x08	/* enclose negative amount in () */
5532223c1bSPedro F. Giffuni #define	SUPRESS_CURR_SYMBOL	0x10	/* suppress the currency from output */
569d430a59SAlexey Zelkin #define	LEFT_JUSTIFY		0x20	/* left justify */
579d430a59SAlexey Zelkin #define	USE_INTL_CURRENCY	0x40	/* use international currency symbol */
589d430a59SAlexey Zelkin #define IS_NEGATIVE		0x80	/* is argument value negative ? */
599d430a59SAlexey Zelkin 
609d430a59SAlexey Zelkin /* internal macros */
61a5aecc77SMike Barcroft #define PRINT(CH) do {						\
629d430a59SAlexey Zelkin 	if (dst >= s + maxsize) 				\
639d430a59SAlexey Zelkin 		goto e2big_error;				\
649d430a59SAlexey Zelkin 	*dst++ = CH;						\
65a5aecc77SMike Barcroft } while (0)
669d430a59SAlexey Zelkin 
67a5aecc77SMike Barcroft #define PRINTS(STR) do {					\
689d430a59SAlexey Zelkin 	char *tmps = STR;					\
699d430a59SAlexey Zelkin 	while (*tmps != '\0')					\
709d430a59SAlexey Zelkin 		PRINT(*tmps++);					\
71a5aecc77SMike Barcroft } while (0)
729d430a59SAlexey Zelkin 
73a5aecc77SMike Barcroft #define GET_NUMBER(VAR)	do {					\
749d430a59SAlexey Zelkin 	VAR = 0;						\
759d430a59SAlexey Zelkin 	while (isdigit((unsigned char)*fmt)) {			\
76eff93c80SRuslan Ermilov 		if (VAR > INT_MAX / 10)				\
77eff93c80SRuslan Ermilov 			goto e2big_error;			\
789d430a59SAlexey Zelkin 		VAR *= 10;					\
799d430a59SAlexey Zelkin 		VAR += *fmt - '0';				\
803890416fSRuslan Ermilov 		if (VAR < 0)					\
813890416fSRuslan Ermilov 			goto e2big_error;			\
829d430a59SAlexey Zelkin 		fmt++;						\
839d430a59SAlexey Zelkin 	}							\
84a5aecc77SMike Barcroft } while (0)
85a5aecc77SMike Barcroft 
86a5aecc77SMike Barcroft #define GRPCPY(howmany) do {					\
87a5aecc77SMike Barcroft 	int i = howmany;					\
88a5aecc77SMike Barcroft 	while (i-- > 0) {					\
89a5aecc77SMike Barcroft 		avalue_size--;					\
90a5aecc77SMike Barcroft 		*--bufend = *(avalue+avalue_size+padded);	\
91a5aecc77SMike Barcroft 	}							\
92a5aecc77SMike Barcroft } while (0)
93a5aecc77SMike Barcroft 
94a5aecc77SMike Barcroft #define GRPSEP do {						\
95a5aecc77SMike Barcroft 	*--bufend = thousands_sep;				\
96a5aecc77SMike Barcroft 	groups++;						\
97a5aecc77SMike Barcroft } while (0)
989d430a59SAlexey Zelkin 
999d430a59SAlexey Zelkin static void __setup_vars(int, char *, char *, char *, char **);
1009d430a59SAlexey Zelkin static int __calc_left_pad(int, char *);
1019d430a59SAlexey Zelkin static char *__format_grouped_double(double, int *, int, int, int);
1029d430a59SAlexey Zelkin 
1033c87aa1dSDavid Chisnall static ssize_t
1043c87aa1dSDavid Chisnall vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
1053c87aa1dSDavid Chisnall 		const char * __restrict format, va_list ap)
1069d430a59SAlexey Zelkin {
1079d430a59SAlexey Zelkin 	char 		*dst;		/* output destination pointer */
1089d430a59SAlexey Zelkin 	const char 	*fmt;		/* current format poistion pointer */
1099d430a59SAlexey Zelkin 	struct lconv 	*lc;		/* pointer to lconv structure */
1109d430a59SAlexey Zelkin 	char		*asciivalue;	/* formatted double pointer */
1119d430a59SAlexey Zelkin 
1129d430a59SAlexey Zelkin 	int		flags;		/* formatting options */
1139d430a59SAlexey Zelkin 	int		pad_char;	/* padding character */
1149d430a59SAlexey Zelkin 	int		pad_size;	/* pad size */
1159d430a59SAlexey Zelkin 	int		width;		/* field width */
1169d430a59SAlexey Zelkin 	int		left_prec;	/* left precision */
1179d430a59SAlexey Zelkin 	int		right_prec;	/* right precision */
1189d430a59SAlexey Zelkin 	double		value;		/* just value */
1199d430a59SAlexey Zelkin 	char		space_char = ' '; /* space after currency */
1209d430a59SAlexey Zelkin 
1219d430a59SAlexey Zelkin 	char		cs_precedes,	/* values gathered from struct lconv */
1229d430a59SAlexey Zelkin 			sep_by_space,
1239d430a59SAlexey Zelkin 			sign_posn,
1249d430a59SAlexey Zelkin 			*signstr,
1259d430a59SAlexey Zelkin 			*currency_symbol;
1269d430a59SAlexey Zelkin 
1279d430a59SAlexey Zelkin 	char		*tmpptr;	/* temporary vars */
1282e9212d9STim J. Robbins 	int		sverrno;
1293c87aa1dSDavid Chisnall 	FIX_LOCALE(loc);
1309d430a59SAlexey Zelkin 
1319d430a59SAlexey Zelkin 
1323c87aa1dSDavid Chisnall 	lc = localeconv_l(loc);
1339d430a59SAlexey Zelkin 	dst = s;
1349d430a59SAlexey Zelkin 	fmt = format;
1359d430a59SAlexey Zelkin 	asciivalue = NULL;
1369d430a59SAlexey Zelkin 	currency_symbol = NULL;
1379d430a59SAlexey Zelkin 	pad_size = 0;
1389d430a59SAlexey Zelkin 
1399d430a59SAlexey Zelkin 	while (*fmt) {
1409d430a59SAlexey Zelkin 		/* pass nonformating characters AS IS */
141a5aecc77SMike Barcroft 		if (*fmt != '%')
1429d430a59SAlexey Zelkin 			goto literal;
1439d430a59SAlexey Zelkin 
1449d430a59SAlexey Zelkin 		/* '%' found ! */
1459d430a59SAlexey Zelkin 
1469d430a59SAlexey Zelkin 		/* "%%" mean just '%' */
1479d430a59SAlexey Zelkin 		if (*(fmt+1) == '%') {
1489d430a59SAlexey Zelkin 			fmt++;
1499d430a59SAlexey Zelkin 	literal:
1509d430a59SAlexey Zelkin 			PRINT(*fmt++);
1519d430a59SAlexey Zelkin 			continue;
1529d430a59SAlexey Zelkin 		}
1539d430a59SAlexey Zelkin 
1549d430a59SAlexey Zelkin 		/* set up initial values */
1559d430a59SAlexey Zelkin 		flags = (NEED_GROUPING|LOCALE_POSN);
1569d430a59SAlexey Zelkin 		pad_char = ' ';		/* padding character is "space" */
1579d430a59SAlexey Zelkin 		left_prec = -1;		/* no left precision specified */
1589d430a59SAlexey Zelkin 		right_prec = -1;	/* no right precision specified */
1599d430a59SAlexey Zelkin 		width = -1;		/* no width specified */
1609d430a59SAlexey Zelkin 		value = 0;		/* we have no value to print now */
1619d430a59SAlexey Zelkin 
1629d430a59SAlexey Zelkin 		/* Flags */
1639d430a59SAlexey Zelkin 		while (1) {
1649d430a59SAlexey Zelkin 			switch (*++fmt) {
1659d430a59SAlexey Zelkin 				case '=':	/* fill character */
1669d430a59SAlexey Zelkin 					pad_char = *++fmt;
1679d430a59SAlexey Zelkin 					if (pad_char == '\0')
1689d430a59SAlexey Zelkin 						goto format_error;
1699d430a59SAlexey Zelkin 					continue;
1709d430a59SAlexey Zelkin 				case '^':	/* not group currency  */
1719d430a59SAlexey Zelkin 					flags &= ~(NEED_GROUPING);
1729d430a59SAlexey Zelkin 					continue;
1739d430a59SAlexey Zelkin 				case '+':	/* use locale defined signs */
1749d430a59SAlexey Zelkin 					if (flags & SIGN_POSN_USED)
1759d430a59SAlexey Zelkin 						goto format_error;
1769d430a59SAlexey Zelkin 					flags |= (SIGN_POSN_USED|LOCALE_POSN);
1779d430a59SAlexey Zelkin 					continue;
1789d430a59SAlexey Zelkin 				case '(':	/* enclose negatives with () */
1799d430a59SAlexey Zelkin 					if (flags & SIGN_POSN_USED)
1809d430a59SAlexey Zelkin 						goto format_error;
1819d430a59SAlexey Zelkin 					flags |= (SIGN_POSN_USED|PARENTH_POSN);
1829d430a59SAlexey Zelkin 					continue;
1839d430a59SAlexey Zelkin 				case '!':	/* suppress currency symbol */
1849d430a59SAlexey Zelkin 					flags |= SUPRESS_CURR_SYMBOL;
1859d430a59SAlexey Zelkin 					continue;
1869d430a59SAlexey Zelkin 				case '-':	/* alignment (left)  */
1879d430a59SAlexey Zelkin 					flags |= LEFT_JUSTIFY;
1889d430a59SAlexey Zelkin 					continue;
1899d430a59SAlexey Zelkin 				default:
1909d430a59SAlexey Zelkin 					break;
1919d430a59SAlexey Zelkin 			}
1929d430a59SAlexey Zelkin 			break;
1939d430a59SAlexey Zelkin 		}
1949d430a59SAlexey Zelkin 
1959d430a59SAlexey Zelkin 		/* field Width */
1969d430a59SAlexey Zelkin 		if (isdigit((unsigned char)*fmt)) {
1979d430a59SAlexey Zelkin 			GET_NUMBER(width);
1989d430a59SAlexey Zelkin 			/* Do we have enough space to put number with
1999d430a59SAlexey Zelkin 			 * required width ?
2009d430a59SAlexey Zelkin 			 */
2013890416fSRuslan Ermilov 			if ((unsigned int)width >= maxsize - (dst - s))
2029d430a59SAlexey Zelkin 				goto e2big_error;
2039d430a59SAlexey Zelkin 		}
2049d430a59SAlexey Zelkin 
205284d5622STim J. Robbins 		/* Left precision */
206284d5622STim J. Robbins 		if (*fmt == '#') {
207284d5622STim J. Robbins 			if (!isdigit((unsigned char)*++fmt))
208284d5622STim J. Robbins 				goto format_error;
209284d5622STim J. Robbins 			GET_NUMBER(left_prec);
2103890416fSRuslan Ermilov 			if ((unsigned int)left_prec >= maxsize - (dst - s))
2113890416fSRuslan Ermilov 				goto e2big_error;
212284d5622STim J. Robbins 		}
213284d5622STim J. Robbins 
214284d5622STim J. Robbins 		/* Right precision */
215284d5622STim J. Robbins 		if (*fmt == '.') {
216284d5622STim J. Robbins 			if (!isdigit((unsigned char)*++fmt))
217284d5622STim J. Robbins 				goto format_error;
218284d5622STim J. Robbins 			GET_NUMBER(right_prec);
2193890416fSRuslan Ermilov 			if ((unsigned int)right_prec >= maxsize - (dst - s) -
2203890416fSRuslan Ermilov 			    left_prec)
2213890416fSRuslan Ermilov 				goto e2big_error;
222284d5622STim J. Robbins 		}
223284d5622STim J. Robbins 
2249d430a59SAlexey Zelkin 		/* Conversion Characters */
2259d430a59SAlexey Zelkin 		switch (*fmt++) {
2269d430a59SAlexey Zelkin 			case 'i':	/* use internaltion currency format */
2279d430a59SAlexey Zelkin 				flags |= USE_INTL_CURRENCY;
2289d430a59SAlexey Zelkin 				break;
2299d430a59SAlexey Zelkin 			case 'n':	/* use national currency format */
2309d430a59SAlexey Zelkin 				flags &= ~(USE_INTL_CURRENCY);
2319d430a59SAlexey Zelkin 				break;
2329d430a59SAlexey Zelkin 			default:	/* required character is missing or
2339d430a59SAlexey Zelkin 					   premature EOS */
2349d430a59SAlexey Zelkin 				goto format_error;
2359d430a59SAlexey Zelkin 		}
2369d430a59SAlexey Zelkin 
2375b30d6caSRuslan Ermilov 		if (currency_symbol != NULL)
2385b30d6caSRuslan Ermilov 			free(currency_symbol);
2399d430a59SAlexey Zelkin 		if (flags & USE_INTL_CURRENCY) {
2409d430a59SAlexey Zelkin 			currency_symbol = strdup(lc->int_curr_symbol);
2419d430a59SAlexey Zelkin 			if (currency_symbol != NULL)
2429d430a59SAlexey Zelkin 				space_char = *(currency_symbol+3);
243a5aecc77SMike Barcroft 		} else
2449d430a59SAlexey Zelkin 			currency_symbol = strdup(lc->currency_symbol);
2459d430a59SAlexey Zelkin 
2469d430a59SAlexey Zelkin 		if (currency_symbol == NULL)
2479d430a59SAlexey Zelkin 			goto end_error;			/* ENOMEM. */
2489d430a59SAlexey Zelkin 
2499d430a59SAlexey Zelkin 		/* value itself */
2509d430a59SAlexey Zelkin 		value = va_arg(ap, double);
2519d430a59SAlexey Zelkin 
2529d430a59SAlexey Zelkin 		/* detect sign */
2539d430a59SAlexey Zelkin 		if (value < 0) {
2549d430a59SAlexey Zelkin 			flags |= IS_NEGATIVE;
2559d430a59SAlexey Zelkin 			value = -value;
2569d430a59SAlexey Zelkin 		}
2579d430a59SAlexey Zelkin 
2589d430a59SAlexey Zelkin 		/* fill left_prec with amount of padding chars */
2599d430a59SAlexey Zelkin 		if (left_prec >= 0) {
2609d430a59SAlexey Zelkin 			pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
2619d430a59SAlexey Zelkin 							currency_symbol) -
2629d430a59SAlexey Zelkin 				   __calc_left_pad(flags, currency_symbol);
2639d430a59SAlexey Zelkin 			if (pad_size < 0)
2649d430a59SAlexey Zelkin 				pad_size = 0;
2659d430a59SAlexey Zelkin 		}
2669d430a59SAlexey Zelkin 
2675b30d6caSRuslan Ermilov 		if (asciivalue != NULL)
2685b30d6caSRuslan Ermilov 			free(asciivalue);
2699d430a59SAlexey Zelkin 		asciivalue = __format_grouped_double(value, &flags,
2709d430a59SAlexey Zelkin 				left_prec, right_prec, pad_char);
2719d430a59SAlexey Zelkin 		if (asciivalue == NULL)
2729d430a59SAlexey Zelkin 			goto end_error;		/* errno already set     */
2739d430a59SAlexey Zelkin 						/* to ENOMEM by malloc() */
2749d430a59SAlexey Zelkin 
2759d430a59SAlexey Zelkin 		/* set some variables for later use */
2769d430a59SAlexey Zelkin 		__setup_vars(flags, &cs_precedes, &sep_by_space,
2779d430a59SAlexey Zelkin 				&sign_posn, &signstr);
2789d430a59SAlexey Zelkin 
2799d430a59SAlexey Zelkin 		/*
2809d430a59SAlexey Zelkin 		 * Description of some LC_MONETARY's values:
2819d430a59SAlexey Zelkin 		 *
2829d430a59SAlexey Zelkin 		 * p_cs_precedes & n_cs_precedes
2839d430a59SAlexey Zelkin 		 *
2849d430a59SAlexey Zelkin 		 * = 1 - $currency_symbol precedes the value
2859d430a59SAlexey Zelkin 		 *       for a monetary quantity with a non-negative value
2869d430a59SAlexey Zelkin 		 * = 0 - symbol succeeds the value
2879d430a59SAlexey Zelkin 		 *
2889d430a59SAlexey Zelkin 		 * p_sep_by_space & n_sep_by_space
2899d430a59SAlexey Zelkin                  *
2909d430a59SAlexey Zelkin 		 * = 0 - no space separates $currency_symbol
2919d430a59SAlexey Zelkin 		 *       from the value for a monetary quantity with a
2929d430a59SAlexey Zelkin 		 *	 non-negative value
2939d430a59SAlexey Zelkin 		 * = 1 - space separates the symbol from the value
2949d430a59SAlexey Zelkin 		 * = 2 - space separates the symbol and the sign string,
2959d430a59SAlexey Zelkin 		 *       if adjacent.
2969d430a59SAlexey Zelkin                  *
2979d430a59SAlexey Zelkin 		 * p_sign_posn & n_sign_posn
2989d430a59SAlexey Zelkin                  *
2999d430a59SAlexey Zelkin 		 * = 0 - parentheses enclose the quantity and the
3009d430a59SAlexey Zelkin 		 *	 $currency_symbol
3019d430a59SAlexey Zelkin 		 * = 1 - the sign string precedes the quantity and the
3029d430a59SAlexey Zelkin 		 *       $currency_symbol
3039d430a59SAlexey Zelkin 		 * = 2 - the sign string succeeds the quantity and the
3049d430a59SAlexey Zelkin 		 *       $currency_symbol
3059d430a59SAlexey Zelkin 		 * = 3 - the sign string precedes the $currency_symbol
3069d430a59SAlexey Zelkin 		 * = 4 - the sign string succeeds the $currency_symbol
3079d430a59SAlexey Zelkin                  *
3089d430a59SAlexey Zelkin 		 */
3099d430a59SAlexey Zelkin 
3109d430a59SAlexey Zelkin 		tmpptr = dst;
3119d430a59SAlexey Zelkin 
3129d430a59SAlexey Zelkin 		while (pad_size-- > 0)
3139d430a59SAlexey Zelkin 			PRINT(' ');
3149d430a59SAlexey Zelkin 
31540a48101STim J. Robbins 		if (sign_posn == 0 && (flags & IS_NEGATIVE))
3169d430a59SAlexey Zelkin 			PRINT('(');
3179d430a59SAlexey Zelkin 
3189d430a59SAlexey Zelkin 		if (cs_precedes == 1) {
3199d430a59SAlexey Zelkin 			if (sign_posn == 1 || sign_posn == 3) {
3209d430a59SAlexey Zelkin 				PRINTS(signstr);
3219d430a59SAlexey Zelkin 				if (sep_by_space == 2)		/* XXX: ? */
3229d430a59SAlexey Zelkin 					PRINT(' ');
3239d430a59SAlexey Zelkin 			}
3249d430a59SAlexey Zelkin 
3259d430a59SAlexey Zelkin 			if (!(flags & SUPRESS_CURR_SYMBOL)) {
3269d430a59SAlexey Zelkin 				PRINTS(currency_symbol);
3279d430a59SAlexey Zelkin 
3289d430a59SAlexey Zelkin 				if (sign_posn == 4) {
3299d430a59SAlexey Zelkin 					if (sep_by_space == 2)
3309d430a59SAlexey Zelkin 						PRINT(space_char);
3319d430a59SAlexey Zelkin 					PRINTS(signstr);
3329d430a59SAlexey Zelkin 					if (sep_by_space == 1)
3339d430a59SAlexey Zelkin 						PRINT(' ');
334a5aecc77SMike Barcroft 				} else if (sep_by_space == 1)
3359d430a59SAlexey Zelkin 					PRINT(space_char);
3369d430a59SAlexey Zelkin 			}
337a5aecc77SMike Barcroft 		} else if (sign_posn == 1)
3389d430a59SAlexey Zelkin 			PRINTS(signstr);
3399d430a59SAlexey Zelkin 
3409d430a59SAlexey Zelkin 		PRINTS(asciivalue);
3419d430a59SAlexey Zelkin 
3429d430a59SAlexey Zelkin 		if (cs_precedes == 0) {
3439d430a59SAlexey Zelkin 			if (sign_posn == 3) {
3449d430a59SAlexey Zelkin 				if (sep_by_space == 1)
3459d430a59SAlexey Zelkin 					PRINT(' ');
3469d430a59SAlexey Zelkin 				PRINTS(signstr);
3479d430a59SAlexey Zelkin 			}
3489d430a59SAlexey Zelkin 
3499d430a59SAlexey Zelkin 			if (!(flags & SUPRESS_CURR_SYMBOL)) {
3509d430a59SAlexey Zelkin 				if ((sign_posn == 3 && sep_by_space == 2)
3519d430a59SAlexey Zelkin 				    || (sep_by_space == 1
352bd26dcd1STim J. Robbins 				    && (sign_posn == 0
3539d430a59SAlexey Zelkin 				    || sign_posn == 1
3549d430a59SAlexey Zelkin 				    || sign_posn == 2
3559d430a59SAlexey Zelkin 				    || sign_posn == 4)))
3569d430a59SAlexey Zelkin 					PRINT(space_char);
3579d430a59SAlexey Zelkin 				PRINTS(currency_symbol); /* XXX: len */
3589d430a59SAlexey Zelkin 				if (sign_posn == 4) {
3599d430a59SAlexey Zelkin 					if (sep_by_space == 2)
3609d430a59SAlexey Zelkin 						PRINT(' ');
3619d430a59SAlexey Zelkin 					PRINTS(signstr);
3629d430a59SAlexey Zelkin 				}
3639d430a59SAlexey Zelkin 			}
3649d430a59SAlexey Zelkin 		}
3659d430a59SAlexey Zelkin 
3669d430a59SAlexey Zelkin 		if (sign_posn == 2) {
3679d430a59SAlexey Zelkin 			if (sep_by_space == 2)
3689d430a59SAlexey Zelkin 				PRINT(' ');
3699d430a59SAlexey Zelkin 			PRINTS(signstr);
3709d430a59SAlexey Zelkin 		}
3719d430a59SAlexey Zelkin 
3729d430a59SAlexey Zelkin 		if (sign_posn == 0 && (flags & IS_NEGATIVE))
3739d430a59SAlexey Zelkin 			PRINT(')');
3749d430a59SAlexey Zelkin 
3759d430a59SAlexey Zelkin 		if (dst - tmpptr < width) {
3769d430a59SAlexey Zelkin 			if (flags & LEFT_JUSTIFY) {
377bd26dcd1STim J. Robbins 				while (dst - tmpptr < width)
3789d430a59SAlexey Zelkin 					PRINT(' ');
3799d430a59SAlexey Zelkin 			} else {
3809d430a59SAlexey Zelkin 				pad_size = dst-tmpptr;
381a5aecc77SMike Barcroft 				memmove(tmpptr + width-pad_size, tmpptr,
382a5aecc77SMike Barcroft 				    pad_size);
3839d430a59SAlexey Zelkin 				memset(tmpptr, ' ', width-pad_size);
3849d430a59SAlexey Zelkin 				dst += width-pad_size;
3859d430a59SAlexey Zelkin 			}
3869d430a59SAlexey Zelkin 		}
3879d430a59SAlexey Zelkin 	}
3889d430a59SAlexey Zelkin 
3899d430a59SAlexey Zelkin 	PRINT('\0');
390bd26dcd1STim J. Robbins 	free(asciivalue);
391bd26dcd1STim J. Robbins 	free(currency_symbol);
3929d430a59SAlexey Zelkin 	return (dst - s - 1);	/* return size of put data except trailing '\0' */
3939d430a59SAlexey Zelkin 
3949d430a59SAlexey Zelkin e2big_error:
3959d430a59SAlexey Zelkin 	errno = E2BIG;
3969d430a59SAlexey Zelkin 	goto end_error;
3979d430a59SAlexey Zelkin 
3989d430a59SAlexey Zelkin format_error:
3999d430a59SAlexey Zelkin 	errno = EINVAL;
4009d430a59SAlexey Zelkin 
4019d430a59SAlexey Zelkin end_error:
4022e9212d9STim J. Robbins 	sverrno = errno;
4039d430a59SAlexey Zelkin 	if (asciivalue != NULL)
4049d430a59SAlexey Zelkin 		free(asciivalue);
4059d430a59SAlexey Zelkin 	if (currency_symbol != NULL)
4069d430a59SAlexey Zelkin 		free(currency_symbol);
4072e9212d9STim J. Robbins 	errno = sverrno;
4089d430a59SAlexey Zelkin 	return (-1);
4099d430a59SAlexey Zelkin }
4103c87aa1dSDavid Chisnall ssize_t
4113c87aa1dSDavid Chisnall strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format,
4123c87aa1dSDavid Chisnall     ...)
4133c87aa1dSDavid Chisnall {
4143c87aa1dSDavid Chisnall 	size_t ret;
4153c87aa1dSDavid Chisnall 	va_list ap;
4163c87aa1dSDavid Chisnall 	va_start(ap, format);
4173c87aa1dSDavid Chisnall 	ret = vstrfmon_l(s, maxsize, loc, format, ap);
4183c87aa1dSDavid Chisnall 	va_end(ap);
4193c87aa1dSDavid Chisnall 	return ret;
4203c87aa1dSDavid Chisnall }
4213c87aa1dSDavid Chisnall 
4223c87aa1dSDavid Chisnall ssize_t
4233c87aa1dSDavid Chisnall strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
4243c87aa1dSDavid Chisnall     ...)
4253c87aa1dSDavid Chisnall {
4263c87aa1dSDavid Chisnall 	size_t ret;
4273c87aa1dSDavid Chisnall 	va_list ap;
4283c87aa1dSDavid Chisnall 	va_start(ap, format);
4293c87aa1dSDavid Chisnall 	ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
4303c87aa1dSDavid Chisnall 	va_end(ap);
4313c87aa1dSDavid Chisnall 	return ret;
4323c87aa1dSDavid Chisnall }
4333c87aa1dSDavid Chisnall 
4349d430a59SAlexey Zelkin 
4359d430a59SAlexey Zelkin static void
4369d430a59SAlexey Zelkin __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
4379d430a59SAlexey Zelkin 		char *sign_posn, char **signstr) {
4389d430a59SAlexey Zelkin 
4399d430a59SAlexey Zelkin 	struct lconv *lc = localeconv();
4409d430a59SAlexey Zelkin 
4412621915fSTim J. Robbins 	if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
4422621915fSTim J. Robbins 		*cs_precedes = lc->int_n_cs_precedes;
4432621915fSTim J. Robbins 		*sep_by_space = lc->int_n_sep_by_space;
4442621915fSTim J. Robbins 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
4453a921aa7SMartin Cracauer 		*signstr = (lc->negative_sign[0] == '\0') ? "-"
4462621915fSTim J. Robbins 		    : lc->negative_sign;
4472621915fSTim J. Robbins 	} else if (flags & USE_INTL_CURRENCY) {
4482621915fSTim J. Robbins 		*cs_precedes = lc->int_p_cs_precedes;
4492621915fSTim J. Robbins 		*sep_by_space = lc->int_p_sep_by_space;
4502621915fSTim J. Robbins 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
4512621915fSTim J. Robbins 		*signstr = lc->positive_sign;
4522621915fSTim J. Robbins 	} else if (flags & IS_NEGATIVE) {
4539d430a59SAlexey Zelkin 		*cs_precedes = lc->n_cs_precedes;
4549d430a59SAlexey Zelkin 		*sep_by_space = lc->n_sep_by_space;
4559d430a59SAlexey Zelkin 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
4563a921aa7SMartin Cracauer 		*signstr = (lc->negative_sign[0] == '\0') ? "-"
457a5aecc77SMike Barcroft 		    : lc->negative_sign;
4589d430a59SAlexey Zelkin 	} else {
4599d430a59SAlexey Zelkin 		*cs_precedes = lc->p_cs_precedes;
4609d430a59SAlexey Zelkin 		*sep_by_space = lc->p_sep_by_space;
4619d430a59SAlexey Zelkin 		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
4629d430a59SAlexey Zelkin 		*signstr = lc->positive_sign;
4639d430a59SAlexey Zelkin 	}
4649d430a59SAlexey Zelkin 
4659d430a59SAlexey Zelkin 	/* Set defult values for unspecified information. */
4669d430a59SAlexey Zelkin 	if (*cs_precedes != 0)
4679d430a59SAlexey Zelkin 		*cs_precedes = 1;
4689d430a59SAlexey Zelkin 	if (*sep_by_space == CHAR_MAX)
4699d430a59SAlexey Zelkin 		*sep_by_space = 0;
4709d430a59SAlexey Zelkin 	if (*sign_posn == CHAR_MAX)
4719d430a59SAlexey Zelkin 		*sign_posn = 0;
4729d430a59SAlexey Zelkin }
4739d430a59SAlexey Zelkin 
4749d430a59SAlexey Zelkin static int
4759d430a59SAlexey Zelkin __calc_left_pad(int flags, char *cur_symb) {
4769d430a59SAlexey Zelkin 
4779d430a59SAlexey Zelkin 	char cs_precedes, sep_by_space, sign_posn, *signstr;
4789d430a59SAlexey Zelkin 	int left_chars = 0;
4799d430a59SAlexey Zelkin 
4809d430a59SAlexey Zelkin 	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
4819d430a59SAlexey Zelkin 
4829d430a59SAlexey Zelkin 	if (cs_precedes != 0) {
4839d430a59SAlexey Zelkin 		left_chars += strlen(cur_symb);
4849d430a59SAlexey Zelkin 		if (sep_by_space != 0)
4859d430a59SAlexey Zelkin 			left_chars++;
4869d430a59SAlexey Zelkin 	}
4879d430a59SAlexey Zelkin 
4889d430a59SAlexey Zelkin 	switch (sign_posn) {
4899d430a59SAlexey Zelkin 		case 1:
4909d430a59SAlexey Zelkin 			left_chars += strlen(signstr);
4919d430a59SAlexey Zelkin 			break;
4929d430a59SAlexey Zelkin 		case 3:
4939d430a59SAlexey Zelkin 		case 4:
4949d430a59SAlexey Zelkin 			if (cs_precedes != 0)
4959d430a59SAlexey Zelkin 				left_chars += strlen(signstr);
4969d430a59SAlexey Zelkin 	}
497a5aecc77SMike Barcroft 	return (left_chars);
4989d430a59SAlexey Zelkin }
4999d430a59SAlexey Zelkin 
5009d430a59SAlexey Zelkin static int
5019d430a59SAlexey Zelkin get_groups(int size, char *grouping) {
5029d430a59SAlexey Zelkin 
5039d430a59SAlexey Zelkin 	int	chars = 0;
5049d430a59SAlexey Zelkin 
5059d430a59SAlexey Zelkin 	if (*grouping == CHAR_MAX || *grouping <= 0)	/* no grouping ? */
506a5aecc77SMike Barcroft 		return (0);
5079d430a59SAlexey Zelkin 
5089d430a59SAlexey Zelkin 	while (size > (int)*grouping) {
5099d430a59SAlexey Zelkin 		chars++;
5109d430a59SAlexey Zelkin 		size -= (int)*grouping++;
5119d430a59SAlexey Zelkin 		/* no more grouping ? */
5129d430a59SAlexey Zelkin 		if (*grouping == CHAR_MAX)
5139d430a59SAlexey Zelkin 			break;
5149d430a59SAlexey Zelkin 		/* rest grouping with same value ? */
5159d430a59SAlexey Zelkin 		if (*grouping == 0) {
5169d430a59SAlexey Zelkin 			chars += (size - 1) / *(grouping - 1);
5179d430a59SAlexey Zelkin 			break;
5189d430a59SAlexey Zelkin 		}
5199d430a59SAlexey Zelkin 	}
520a5aecc77SMike Barcroft 	return (chars);
5219d430a59SAlexey Zelkin }
5229d430a59SAlexey Zelkin 
5239d430a59SAlexey Zelkin /* convert double to ASCII */
5249d430a59SAlexey Zelkin static char *
5259d430a59SAlexey Zelkin __format_grouped_double(double value, int *flags,
5269d430a59SAlexey Zelkin 			int left_prec, int right_prec, int pad_char) {
5279d430a59SAlexey Zelkin 
5289d430a59SAlexey Zelkin 	char		*rslt;
5299d430a59SAlexey Zelkin 	char		*avalue;
5309d430a59SAlexey Zelkin 	int		avalue_size;
5319d430a59SAlexey Zelkin 
5329d430a59SAlexey Zelkin 	size_t		bufsize;
5339d430a59SAlexey Zelkin 	char		*bufend;
5349d430a59SAlexey Zelkin 
5359d430a59SAlexey Zelkin 	int		padded;
5369d430a59SAlexey Zelkin 
5379d430a59SAlexey Zelkin 	struct lconv	*lc = localeconv();
5389d430a59SAlexey Zelkin 	char		*grouping;
5399d430a59SAlexey Zelkin 	char		decimal_point;
5409d430a59SAlexey Zelkin 	char		thousands_sep;
5419d430a59SAlexey Zelkin 
5429d430a59SAlexey Zelkin 	int groups = 0;
5439d430a59SAlexey Zelkin 
5449d430a59SAlexey Zelkin 	grouping = lc->mon_grouping;
5459d430a59SAlexey Zelkin 	decimal_point = *lc->mon_decimal_point;
5469d430a59SAlexey Zelkin 	if (decimal_point == '\0')
54785bebbc1SAndrey A. Chernov 		decimal_point = *lc->decimal_point;
5489d430a59SAlexey Zelkin 	thousands_sep = *lc->mon_thousands_sep;
549a5aecc77SMike Barcroft 	if (thousands_sep == '\0')
5509d430a59SAlexey Zelkin 		thousands_sep = *lc->thousands_sep;
5519d430a59SAlexey Zelkin 
5529d430a59SAlexey Zelkin 	/* fill left_prec with default value */
5539d430a59SAlexey Zelkin 	if (left_prec == -1)
5549d430a59SAlexey Zelkin 		left_prec = 0;
5559d430a59SAlexey Zelkin 
5569d430a59SAlexey Zelkin 	/* fill right_prec with default value */
5579d430a59SAlexey Zelkin 	if (right_prec == -1) {
5589d430a59SAlexey Zelkin                 if (*flags & USE_INTL_CURRENCY)
5599d430a59SAlexey Zelkin                         right_prec = lc->int_frac_digits;
5609d430a59SAlexey Zelkin                 else
5619d430a59SAlexey Zelkin                         right_prec = lc->frac_digits;
5629d430a59SAlexey Zelkin 
5639d430a59SAlexey Zelkin 		if (right_prec == CHAR_MAX)	/* POSIX locale ? */
5649d430a59SAlexey Zelkin 			right_prec = 2;
5659d430a59SAlexey Zelkin 	}
5669d430a59SAlexey Zelkin 
5679d430a59SAlexey Zelkin 	if (*flags & NEED_GROUPING)
5689d430a59SAlexey Zelkin 		left_prec += get_groups(left_prec, grouping);
5699d430a59SAlexey Zelkin 
5709d430a59SAlexey Zelkin 	/* convert to string */
5710c029579SPedro F. Giffuni 	avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
5720c029579SPedro F. Giffuni 	    right_prec, value);
5739d430a59SAlexey Zelkin 	if (avalue_size < 0)
574a5aecc77SMike Barcroft 		return (NULL);
5759d430a59SAlexey Zelkin 
5769d430a59SAlexey Zelkin 	/* make sure that we've enough space for result string */
5770c029579SPedro F. Giffuni 	bufsize = avalue_size * 2 + 1;
57892226c92SXin LI 	rslt = calloc(1, bufsize);
5799d430a59SAlexey Zelkin 	if (rslt == NULL) {
5809d430a59SAlexey Zelkin 		free(avalue);
581a5aecc77SMike Barcroft 		return (NULL);
5829d430a59SAlexey Zelkin 	}
5839d430a59SAlexey Zelkin 	bufend = rslt + bufsize - 1;	/* reserve space for trailing '\0' */
5849d430a59SAlexey Zelkin 
58532223c1bSPedro F. Giffuni 	/* skip spaces at beginning */
5869d430a59SAlexey Zelkin 	padded = 0;
5879d430a59SAlexey Zelkin 	while (avalue[padded] == ' ') {
5889d430a59SAlexey Zelkin 		padded++;
5899d430a59SAlexey Zelkin 		avalue_size--;
5909d430a59SAlexey Zelkin 	}
5919d430a59SAlexey Zelkin 
5929d430a59SAlexey Zelkin 	if (right_prec > 0) {
5939d430a59SAlexey Zelkin 		bufend -= right_prec;
594a5aecc77SMike Barcroft 		memcpy(bufend, avalue + avalue_size+padded-right_prec,
595a5aecc77SMike Barcroft 		    right_prec);
5969d430a59SAlexey Zelkin 		*--bufend = decimal_point;
5979d430a59SAlexey Zelkin 		avalue_size -= (right_prec + 1);
5989d430a59SAlexey Zelkin 	}
5999d430a59SAlexey Zelkin 
6009d430a59SAlexey Zelkin 	if ((*flags & NEED_GROUPING) &&
6019d430a59SAlexey Zelkin 	    thousands_sep != '\0' &&	/* XXX: need investigation */
6029d430a59SAlexey Zelkin 	    *grouping != CHAR_MAX &&
6039d430a59SAlexey Zelkin 	    *grouping > 0) {
6049d430a59SAlexey Zelkin 		while (avalue_size > (int)*grouping) {
6059d430a59SAlexey Zelkin 			GRPCPY(*grouping);
6069d430a59SAlexey Zelkin 			GRPSEP;
6079d430a59SAlexey Zelkin 			grouping++;
6089d430a59SAlexey Zelkin 
6099d430a59SAlexey Zelkin 			/* no more grouping ? */
6109d430a59SAlexey Zelkin 			if (*grouping == CHAR_MAX)
6119d430a59SAlexey Zelkin 				break;
6129d430a59SAlexey Zelkin 
6139d430a59SAlexey Zelkin 			/* rest grouping with same value ? */
6149d430a59SAlexey Zelkin 			if (*grouping == 0) {
6159d430a59SAlexey Zelkin 				grouping--;
6169d430a59SAlexey Zelkin 				while (avalue_size > *grouping) {
6179d430a59SAlexey Zelkin 					GRPCPY(*grouping);
6189d430a59SAlexey Zelkin 					GRPSEP;
6199d430a59SAlexey Zelkin 				}
6209d430a59SAlexey Zelkin 			}
6219d430a59SAlexey Zelkin 		}
6229d430a59SAlexey Zelkin 		if (avalue_size != 0)
6239d430a59SAlexey Zelkin 			GRPCPY(avalue_size);
6249d430a59SAlexey Zelkin 		padded -= groups;
6259d430a59SAlexey Zelkin 
6269d430a59SAlexey Zelkin 	} else {
6279d430a59SAlexey Zelkin 		bufend -= avalue_size;
6289d430a59SAlexey Zelkin 		memcpy(bufend, avalue+padded, avalue_size);
6299d430a59SAlexey Zelkin 		if (right_prec == 0)
6309d430a59SAlexey Zelkin 			padded--;	/* decrease assumed $decimal_point */
6319d430a59SAlexey Zelkin 	}
6329d430a59SAlexey Zelkin 
6339d430a59SAlexey Zelkin 	/* do padding with pad_char */
6349d430a59SAlexey Zelkin 	if (padded > 0) {
6359d430a59SAlexey Zelkin 		bufend -= padded;
6369d430a59SAlexey Zelkin 		memset(bufend, pad_char, padded);
6379d430a59SAlexey Zelkin 	}
6389d430a59SAlexey Zelkin 
639bd26dcd1STim J. Robbins 	bufsize = bufsize - (bufend - rslt) + 1;
6409d430a59SAlexey Zelkin 	memmove(rslt, bufend, bufsize);
6419d430a59SAlexey Zelkin 	free(avalue);
642a5aecc77SMike Barcroft 	return (rslt);
6439d430a59SAlexey Zelkin }
644