strfmon.c revision 104943
183104Sphantom/*-
287659Sphantom * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
383104Sphantom * All rights reserved.
483104Sphantom *
583104Sphantom * Redistribution and use in source and binary forms, with or without
683104Sphantom * modification, are permitted provided that the following conditions
783104Sphantom * are met:
883104Sphantom * 1. Redistributions of source code must retain the above copyright
983104Sphantom *    notice, this list of conditions and the following disclaimer.
1083104Sphantom * 2. Redistributions in binary form must reproduce the above copyright
1183104Sphantom *    notice, this list of conditions and the following disclaimer in the
1283104Sphantom *    documentation and/or other materials provided with the distribution.
1383104Sphantom *
1483104Sphantom * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1583104Sphantom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1683104Sphantom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1783104Sphantom * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1883104Sphantom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1983104Sphantom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2083104Sphantom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2183104Sphantom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2283104Sphantom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2383104Sphantom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2483104Sphantom * SUCH DAMAGE.
2583104Sphantom *
2683104Sphantom */
2783104Sphantom
2892986Sobrien#include <sys/cdefs.h>
2992986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/strfmon.c 104943 2002-10-11 23:04:59Z tjr $");
3083104Sphantom
3183104Sphantom#include <sys/types.h>
3283104Sphantom#include <ctype.h>
3383104Sphantom#include <errno.h>
3483104Sphantom#include <limits.h>
3583104Sphantom#include <locale.h>
3683104Sphantom#include <stdarg.h>
3783104Sphantom#include <stdio.h>
3883104Sphantom#include <stdlib.h>
3983104Sphantom#include <string.h>
4083104Sphantom
4183104Sphantom/* internal flags */
4283104Sphantom#define	NEED_GROUPING		0x01	/* print digits grouped (default) */
4383104Sphantom#define	SIGN_POSN_USED		0x02	/* '+' or '(' usage flag */
4483104Sphantom#define	LOCALE_POSN		0x04	/* use locale defined +/- (default) */
4583104Sphantom#define	PARENTH_POSN		0x08	/* enclose negative amount in () */
4683104Sphantom#define	SUPRESS_CURR_SYMBOL	0x10	/* supress the currency from output */
4783104Sphantom#define	LEFT_JUSTIFY		0x20	/* left justify */
4883104Sphantom#define	USE_INTL_CURRENCY	0x40	/* use international currency symbol */
4983104Sphantom#define IS_NEGATIVE		0x80	/* is argument value negative ? */
5083104Sphantom
5183104Sphantom/* internal macros */
5283559Smike#define PRINT(CH) do {						\
5383104Sphantom	if (dst >= s + maxsize) 				\
5483104Sphantom		goto e2big_error;				\
5583104Sphantom	*dst++ = CH;						\
5683559Smike} while (0)
5783104Sphantom
5883559Smike#define PRINTS(STR) do {					\
5983104Sphantom	char *tmps = STR;					\
6083104Sphantom	while (*tmps != '\0')					\
6183104Sphantom		PRINT(*tmps++);					\
6283559Smike} while (0)
6383104Sphantom
6483559Smike#define GET_NUMBER(VAR)	do {					\
6583104Sphantom	VAR = 0;						\
6683104Sphantom	while (isdigit((unsigned char)*fmt)) {			\
6783104Sphantom		VAR *= 10;					\
6883104Sphantom		VAR += *fmt - '0';				\
6983104Sphantom		fmt++;						\
7083104Sphantom	}							\
7183559Smike} while (0)
7283104Sphantom
7383559Smike#define GRPCPY(howmany) do {					\
7483559Smike	int i = howmany;					\
7583559Smike	while (i-- > 0) {					\
7683559Smike		avalue_size--;					\
7783559Smike		*--bufend = *(avalue+avalue_size+padded);	\
7883559Smike	}							\
7983559Smike} while (0)
8083559Smike
8183559Smike#define GRPSEP do {						\
8283559Smike	*--bufend = thousands_sep;				\
8383559Smike	groups++;						\
8483559Smike} while (0)
8583559Smike
8683104Sphantomstatic void __setup_vars(int, char *, char *, char *, char **);
8783104Sphantomstatic int __calc_left_pad(int, char *);
8883104Sphantomstatic char *__format_grouped_double(double, int *, int, int, int);
8983104Sphantom
9083104Sphantomssize_t
91103668Smikestrfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
92103668Smike    ...)
9383104Sphantom{
9483104Sphantom	va_list		ap;
9583104Sphantom	char 		*dst;		/* output destination pointer */
9683104Sphantom	const char 	*fmt;		/* current format poistion pointer */
9783104Sphantom	struct lconv 	*lc;		/* pointer to lconv structure */
9883104Sphantom	char		*asciivalue;	/* formatted double pointer */
9983104Sphantom
10083104Sphantom	int		flags;		/* formatting options */
10183104Sphantom	int		pad_char;	/* padding character */
10283104Sphantom	int		pad_size;	/* pad size */
10383104Sphantom	int		width;		/* field width */
10483104Sphantom	int		left_prec;	/* left precision */
10583104Sphantom	int		right_prec;	/* right precision */
10683104Sphantom	double		value;		/* just value */
10783104Sphantom	char		space_char = ' '; /* space after currency */
10883104Sphantom
10983104Sphantom	char		cs_precedes,	/* values gathered from struct lconv */
11083104Sphantom			sep_by_space,
11183104Sphantom			sign_posn,
11283104Sphantom			*signstr,
11383104Sphantom			*currency_symbol;
11483104Sphantom
11583104Sphantom	char		*tmpptr;	/* temporary vars */
11683104Sphantom	int		*ntmp;
11783104Sphantom
11883104Sphantom        va_start(ap, format);
11983104Sphantom
12083104Sphantom	lc = localeconv();
12183104Sphantom	dst = s;
12283104Sphantom	fmt = format;
12383104Sphantom	asciivalue = NULL;
12483104Sphantom	currency_symbol = NULL;
12583104Sphantom	pad_size = 0;
12683104Sphantom
12783104Sphantom	while (*fmt) {
12883104Sphantom		/* pass nonformating characters AS IS */
12983559Smike		if (*fmt != '%')
13083104Sphantom			goto literal;
13183104Sphantom
13283104Sphantom		/* '%' found ! */
13383104Sphantom
13483104Sphantom		/* "%%" mean just '%' */
13583104Sphantom		if (*(fmt+1) == '%') {
13683104Sphantom			fmt++;
13783104Sphantom	literal:
13883104Sphantom			PRINT(*fmt++);
13983104Sphantom			continue;
14083104Sphantom		}
14183104Sphantom
14283104Sphantom		/* set up initial values */
14383104Sphantom		flags = (NEED_GROUPING|LOCALE_POSN);
14483104Sphantom		pad_char = ' ';		/* padding character is "space" */
14583104Sphantom		left_prec = -1;		/* no left precision specified */
14683104Sphantom		right_prec = -1;	/* no right precision specified */
14783104Sphantom		width = -1;		/* no width specified */
14883104Sphantom		value = 0;		/* we have no value to print now */
14983104Sphantom
15083104Sphantom		/* Flags */
15183104Sphantom		while (1) {
15283104Sphantom			switch (*++fmt) {
15383104Sphantom				case '=':	/* fill character */
15483104Sphantom					pad_char = *++fmt;
15583104Sphantom					if (pad_char == '\0')
15683104Sphantom						goto format_error;
15783104Sphantom					continue;
15883104Sphantom				case '^':	/* not group currency  */
15983104Sphantom					flags &= ~(NEED_GROUPING);
16083104Sphantom					continue;
16183104Sphantom				case '+':	/* use locale defined signs */
16283104Sphantom					if (flags & SIGN_POSN_USED)
16383104Sphantom						goto format_error;
16483104Sphantom					flags |= (SIGN_POSN_USED|LOCALE_POSN);
16583104Sphantom					continue;
16683104Sphantom				case '(':	/* enclose negatives with () */
16783104Sphantom					if (flags & SIGN_POSN_USED)
16883104Sphantom						goto format_error;
16983104Sphantom					flags |= (SIGN_POSN_USED|PARENTH_POSN);
17083104Sphantom					continue;
17183104Sphantom				case '!':	/* suppress currency symbol */
17283104Sphantom					flags |= SUPRESS_CURR_SYMBOL;
17383104Sphantom					continue;
17483104Sphantom				case '-':	/* alignment (left)  */
17583104Sphantom					flags |= LEFT_JUSTIFY;
17683104Sphantom					continue;
17783104Sphantom				default:
17883104Sphantom					break;
17983104Sphantom			}
18083104Sphantom			break;
18183104Sphantom		}
18283104Sphantom
18383104Sphantom		/* field Width */
18483104Sphantom		if (isdigit((unsigned char)*fmt)) {
18583104Sphantom			GET_NUMBER(width);
18683104Sphantom			/* Do we have enough space to put number with
18783104Sphantom			 * required width ?
18883104Sphantom			 */
18983104Sphantom			if (dst + width >= s + maxsize)
19083104Sphantom				goto e2big_error;
19183104Sphantom		}
192104942Stjr
193104942Stjr		/* Left precision */
194104942Stjr		if (*fmt == '#') {
195104942Stjr			if (!isdigit((unsigned char)*++fmt))
196104942Stjr				goto format_error;
197104942Stjr			GET_NUMBER(left_prec);
198104942Stjr		}
199104942Stjr
200104942Stjr		/* Right precision */
201104942Stjr		if (*fmt == '.') {
202104942Stjr			if (!isdigit((unsigned char)*++fmt))
203104942Stjr				goto format_error;
204104942Stjr			GET_NUMBER(right_prec);
205104942Stjr		}
206104942Stjr
20783104Sphantom		/* Conversion Characters */
20883104Sphantom		switch (*fmt++) {
20983104Sphantom			case 'i':	/* use internaltion currency format */
21083104Sphantom				flags |= USE_INTL_CURRENCY;
21183104Sphantom				break;
21283104Sphantom			case 'n':	/* use national currency format */
21383104Sphantom				flags &= ~(USE_INTL_CURRENCY);
21483104Sphantom				break;
21583104Sphantom			default:	/* required character is missing or
21683104Sphantom					   premature EOS */
21783104Sphantom				goto format_error;
21883104Sphantom		}
21983104Sphantom
22083104Sphantom		if (flags & USE_INTL_CURRENCY) {
22183104Sphantom			currency_symbol = strdup(lc->int_curr_symbol);
22283104Sphantom			if (currency_symbol != NULL)
22383104Sphantom				space_char = *(currency_symbol+3);
22483559Smike		} else
22583104Sphantom			currency_symbol = strdup(lc->currency_symbol);
22683104Sphantom
22783104Sphantom		if (currency_symbol == NULL)
22883104Sphantom			goto end_error;			/* ENOMEM. */
22983104Sphantom
23083104Sphantom		/* value itself */
23183104Sphantom		value = va_arg(ap, double);
23283104Sphantom
23383104Sphantom		/* detect sign */
23483104Sphantom		if (value < 0) {
23583104Sphantom			flags |= IS_NEGATIVE;
23683104Sphantom			value = -value;
23783104Sphantom		}
23883104Sphantom
23983104Sphantom		/* fill left_prec with amount of padding chars */
24083104Sphantom		if (left_prec >= 0) {
24183104Sphantom			pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
24283104Sphantom							currency_symbol) -
24383104Sphantom				   __calc_left_pad(flags, currency_symbol);
24483104Sphantom			if (pad_size < 0)
24583104Sphantom				pad_size = 0;
24683104Sphantom		}
24783104Sphantom
24883104Sphantom		asciivalue = __format_grouped_double(value, &flags,
24983104Sphantom				left_prec, right_prec, pad_char);
25083104Sphantom		if (asciivalue == NULL)
25183104Sphantom			goto end_error;		/* errno already set     */
25283104Sphantom						/* to ENOMEM by malloc() */
25383104Sphantom
25483104Sphantom		/* set some variables for later use */
25583104Sphantom		__setup_vars(flags, &cs_precedes, &sep_by_space,
25683104Sphantom				&sign_posn, &signstr);
25783104Sphantom
25883104Sphantom		/*
25983104Sphantom		 * Description of some LC_MONETARY's values:
26083104Sphantom		 *
26183104Sphantom		 * p_cs_precedes & n_cs_precedes
26283104Sphantom		 *
26383104Sphantom		 * = 1 - $currency_symbol precedes the value
26483104Sphantom		 *       for a monetary quantity with a non-negative value
26583104Sphantom		 * = 0 - symbol succeeds the value
26683104Sphantom		 *
26783104Sphantom		 * p_sep_by_space & n_sep_by_space
26883104Sphantom                 *
26983104Sphantom		 * = 0 - no space separates $currency_symbol
27083104Sphantom		 *       from the value for a monetary quantity with a
27183104Sphantom		 *	 non-negative value
27283104Sphantom		 * = 1 - space separates the symbol from the value
27383104Sphantom		 * = 2 - space separates the symbol and the sign string,
27483104Sphantom		 *       if adjacent.
27583104Sphantom                 *
27683104Sphantom		 * p_sign_posn & n_sign_posn
27783104Sphantom                 *
27883104Sphantom		 * = 0 - parentheses enclose the quantity and the
27983104Sphantom		 *	 $currency_symbol
28083104Sphantom		 * = 1 - the sign string precedes the quantity and the
28183104Sphantom		 *       $currency_symbol
28283104Sphantom		 * = 2 - the sign string succeeds the quantity and the
28383104Sphantom		 *       $currency_symbol
28483104Sphantom		 * = 3 - the sign string precedes the $currency_symbol
28583104Sphantom		 * = 4 - the sign string succeeds the $currency_symbol
28683104Sphantom                 *
28783104Sphantom		 */
28883104Sphantom
28983104Sphantom		tmpptr = dst;
29083104Sphantom
29183104Sphantom		while (pad_size-- > 0)
29283104Sphantom			PRINT(' ');
29383104Sphantom
294104943Stjr		if (sign_posn == 0 && (flags & IS_NEGATIVE))
295104943Stjr			PRINT('(');
29683104Sphantom
29783104Sphantom		if (cs_precedes == 1) {
29883104Sphantom			if (sign_posn == 1 || sign_posn == 3) {
29983104Sphantom				PRINTS(signstr);
30083104Sphantom				if (sep_by_space == 2)		/* XXX: ? */
30183104Sphantom					PRINT(' ');
30283104Sphantom			}
30383104Sphantom
30483104Sphantom			if (!(flags & SUPRESS_CURR_SYMBOL)) {
30583104Sphantom				PRINTS(currency_symbol);
30683104Sphantom
30783104Sphantom				if (sign_posn == 4) {
30883104Sphantom					if (sep_by_space == 2)
30983104Sphantom						PRINT(space_char);
31083559Smike					PRINTS(signstr);
31183104Sphantom					if (sep_by_space == 1)
31283104Sphantom						PRINT(' ');
31383559Smike				} else if (sep_by_space == 1)
31483559Smike					PRINT(space_char);
31583104Sphantom			}
31683559Smike		} else if (sign_posn == 1)
31783559Smike			PRINTS(signstr);
31883104Sphantom
31983104Sphantom		PRINTS(asciivalue);
32083104Sphantom
32183104Sphantom		if (cs_precedes == 0) {
32283104Sphantom			if (sign_posn == 3) {
32383104Sphantom				if (sep_by_space == 1)
32483104Sphantom					PRINT(' ');
32583104Sphantom				PRINTS(signstr);
32683104Sphantom			}
32783104Sphantom
32883104Sphantom			if (!(flags & SUPRESS_CURR_SYMBOL)) {
32983104Sphantom				if ((sign_posn == 3 && sep_by_space == 2)
33083559Smike				    || (sep_by_space == 1
33183559Smike				    && (sign_posn = 0
33283559Smike				    || sign_posn == 1
33383559Smike				    || sign_posn == 2
33483559Smike				    || sign_posn == 4)))
33583559Smike					PRINT(space_char);
33683104Sphantom				PRINTS(currency_symbol); /* XXX: len */
33783104Sphantom				if (sign_posn == 4) {
33883104Sphantom					if (sep_by_space == 2)
33983104Sphantom						PRINT(' ');
34083104Sphantom					PRINTS(signstr);
34183104Sphantom				}
34283104Sphantom			}
34383104Sphantom		}
34483104Sphantom
34583104Sphantom		if (sign_posn == 2) {
34683104Sphantom			if (sep_by_space == 2)
34783104Sphantom				PRINT(' ');
34883104Sphantom			PRINTS(signstr);
34983104Sphantom		}
35083104Sphantom
35183104Sphantom		if (sign_posn == 0 && (flags & IS_NEGATIVE))
35283104Sphantom			PRINT(')');
35383104Sphantom
35483104Sphantom		if (dst - tmpptr < width) {
35583104Sphantom			if (flags & LEFT_JUSTIFY) {
35683104Sphantom				while (dst - tmpptr <= width)
35783104Sphantom					PRINT(' ');
35883104Sphantom			} else {
35983104Sphantom				pad_size = dst-tmpptr;
36083559Smike				memmove(tmpptr + width-pad_size, tmpptr,
36183559Smike				    pad_size);
36283104Sphantom				memset(tmpptr, ' ', width-pad_size);
36383104Sphantom				dst += width-pad_size;
36483104Sphantom			}
36583104Sphantom		}
36683104Sphantom	}
36783104Sphantom
36883104Sphantom	PRINT('\0');
36983104Sphantom	va_end(ap);
37083104Sphantom	return (dst - s - 1);	/* return size of put data except trailing '\0' */
37183104Sphantom
37283104Sphantome2big_error:
37383104Sphantom	errno = E2BIG;
37483104Sphantom	goto end_error;
37583104Sphantom
37683104Sphantomformat_error:
37783104Sphantom	errno = EINVAL;
37883104Sphantom
37983104Sphantomend_error:
38083104Sphantom	if (asciivalue != NULL)
38183104Sphantom		free(asciivalue);
38283104Sphantom	if (currency_symbol != NULL)
38383104Sphantom		free(currency_symbol);
38483104Sphantom	va_end(ap);
38583104Sphantom	return (-1);
38683104Sphantom}
38783104Sphantom
38883104Sphantomstatic void
38983104Sphantom__setup_vars(int flags, char *cs_precedes, char *sep_by_space,
39083104Sphantom		char *sign_posn, char **signstr) {
39183104Sphantom
39283104Sphantom	struct lconv *lc = localeconv();
39383104Sphantom
39483104Sphantom	if (flags & IS_NEGATIVE) {
39583104Sphantom		*cs_precedes = lc->n_cs_precedes;
39683104Sphantom		*sep_by_space = lc->n_sep_by_space;
39783559Smike		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
39883559Smike		*signstr = (lc->negative_sign == '\0') ? "-"
39983559Smike		    : lc->negative_sign;
40083104Sphantom	} else {
40183104Sphantom		*cs_precedes = lc->p_cs_precedes;
40283104Sphantom		*sep_by_space = lc->p_sep_by_space;
40383559Smike		*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
40483104Sphantom		*signstr = lc->positive_sign;
40583104Sphantom	}
40683104Sphantom
40783104Sphantom	/* Set defult values for unspecified information. */
40883104Sphantom	if (*cs_precedes != 0)
40983104Sphantom		*cs_precedes = 1;
41083104Sphantom	if (*sep_by_space == CHAR_MAX)
41183104Sphantom		*sep_by_space = 0;
41283104Sphantom	if (*sign_posn == CHAR_MAX)
41383104Sphantom		*sign_posn = 0;
41483104Sphantom}
41583104Sphantom
41683104Sphantomstatic int
41783104Sphantom__calc_left_pad(int flags, char *cur_symb) {
41883104Sphantom
41983104Sphantom	char cs_precedes, sep_by_space, sign_posn, *signstr;
42083104Sphantom	int left_chars = 0;
42183104Sphantom
42283104Sphantom	__setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
42383104Sphantom
42483104Sphantom	if (cs_precedes != 0) {
42583104Sphantom		left_chars += strlen(cur_symb);
42683104Sphantom		if (sep_by_space != 0)
42783104Sphantom			left_chars++;
42883104Sphantom	}
42983104Sphantom
43083104Sphantom	switch (sign_posn) {
43183104Sphantom		case 1:
43283104Sphantom			left_chars += strlen(signstr);
43383104Sphantom			break;
43483104Sphantom		case 3:
43583104Sphantom		case 4:
43683104Sphantom			if (cs_precedes != 0)
43783559Smike				left_chars += strlen(signstr);
43883104Sphantom	}
43983559Smike	return (left_chars);
44083104Sphantom}
44183104Sphantom
44283104Sphantomstatic int
44383104Sphantomget_groups(int size, char *grouping) {
44483104Sphantom
44583104Sphantom	int	chars = 0;
44683104Sphantom
44783104Sphantom	if (*grouping == CHAR_MAX || *grouping <= 0)	/* no grouping ? */
44883559Smike		return (0);
44983104Sphantom
45083104Sphantom	while (size > (int)*grouping) {
45183104Sphantom		chars++;
45283104Sphantom		size -= (int)*grouping++;
45383104Sphantom		/* no more grouping ? */
45483104Sphantom		if (*grouping == CHAR_MAX)
45583104Sphantom			break;
45683104Sphantom		/* rest grouping with same value ? */
45783104Sphantom		if (*grouping == 0) {
45883104Sphantom			chars += (size - 1) / *(grouping - 1);
45983104Sphantom			break;
46083104Sphantom		}
46183104Sphantom	}
46283559Smike	return (chars);
46383104Sphantom}
46483104Sphantom
46583104Sphantom/* convert double to ASCII */
46683104Sphantomstatic char *
46783104Sphantom__format_grouped_double(double value, int *flags,
46883104Sphantom			int left_prec, int right_prec, int pad_char) {
46983104Sphantom
47083104Sphantom	char		*rslt;
47183104Sphantom	char		*avalue;
47283104Sphantom	int		avalue_size;
47383104Sphantom	char		fmt[32];
47483104Sphantom
47583104Sphantom	size_t		bufsize;
47683104Sphantom	char		*bufend;
47783104Sphantom
47883104Sphantom	int		padded;
47983104Sphantom
48083104Sphantom	struct lconv	*lc = localeconv();
48183104Sphantom	char		*grouping;
48283104Sphantom	char		decimal_point;
48383104Sphantom	char		thousands_sep;
48483104Sphantom
48583104Sphantom	int groups = 0;
48683104Sphantom
48783104Sphantom	grouping = lc->mon_grouping;
48883104Sphantom	decimal_point = *lc->mon_decimal_point;
48983104Sphantom	if (decimal_point == '\0') {
49083104Sphantom		decimal_point = *lc->decimal_point;
49183104Sphantom		if (decimal_point == '\0')
49283104Sphantom			decimal_point = '.';
49383104Sphantom	}
49483104Sphantom	thousands_sep = *lc->mon_thousands_sep;
49583559Smike	if (thousands_sep == '\0')
49683104Sphantom		thousands_sep = *lc->thousands_sep;
49783104Sphantom
49883104Sphantom	/* fill left_prec with default value */
49983104Sphantom	if (left_prec == -1)
50083104Sphantom		left_prec = 0;
50183104Sphantom
50283104Sphantom	/* fill right_prec with default value */
50383104Sphantom	if (right_prec == -1) {
50483104Sphantom                if (*flags & USE_INTL_CURRENCY)
50583104Sphantom                        right_prec = lc->int_frac_digits;
50683104Sphantom                else
50783104Sphantom                        right_prec = lc->frac_digits;
50883104Sphantom
50983104Sphantom		if (right_prec == CHAR_MAX)	/* POSIX locale ? */
51083104Sphantom			right_prec = 2;
51183104Sphantom	}
51283104Sphantom
51383104Sphantom	if (*flags & NEED_GROUPING)
51483104Sphantom		left_prec += get_groups(left_prec, grouping);
51583104Sphantom
51683104Sphantom	/* convert to string */
51783559Smike	snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1,
51883559Smike	    right_prec);
51983104Sphantom	avalue_size = asprintf(&avalue, fmt, value);
52083104Sphantom	if (avalue_size < 0)
52183559Smike		return (NULL);
52283104Sphantom
52383104Sphantom	/* make sure that we've enough space for result string */
52483104Sphantom	bufsize = strlen(avalue)*2+1;
52583104Sphantom	rslt = malloc(bufsize);
52683104Sphantom	if (rslt == NULL) {
52783104Sphantom		free(avalue);
52883559Smike		return (NULL);
52983104Sphantom	}
53083104Sphantom	memset(rslt, 0, bufsize);
53183104Sphantom	bufend = rslt + bufsize - 1;	/* reserve space for trailing '\0' */
53283104Sphantom
53383104Sphantom	/* skip spaces at beggining */
53483104Sphantom	padded = 0;
53583104Sphantom	while (avalue[padded] == ' ') {
53683104Sphantom		padded++;
53783104Sphantom		avalue_size--;
53883104Sphantom	}
53983104Sphantom
54083104Sphantom	if (right_prec > 0) {
54183104Sphantom		bufend -= right_prec;
54283559Smike		memcpy(bufend, avalue + avalue_size+padded-right_prec,
54383559Smike		    right_prec);
54483104Sphantom		*--bufend = decimal_point;
54583104Sphantom		avalue_size -= (right_prec + 1);
54683104Sphantom	}
54783104Sphantom
54883104Sphantom	if ((*flags & NEED_GROUPING) &&
54983559Smike	    thousands_sep != '\0' &&	/* XXX: need investigation */
55083559Smike	    *grouping != CHAR_MAX &&
55183559Smike	    *grouping > 0) {
55283104Sphantom		while (avalue_size > (int)*grouping) {
55383104Sphantom			GRPCPY(*grouping);
55483104Sphantom			GRPSEP;
55583104Sphantom			grouping++;
55683104Sphantom
55783104Sphantom			/* no more grouping ? */
55883104Sphantom			if (*grouping == CHAR_MAX)
55983104Sphantom				break;
56083104Sphantom
56183104Sphantom			/* rest grouping with same value ? */
56283104Sphantom			if (*grouping == 0) {
56383104Sphantom				grouping--;
56483104Sphantom				while (avalue_size > *grouping) {
56583104Sphantom					GRPCPY(*grouping);
56683104Sphantom					GRPSEP;
56783104Sphantom				}
56883104Sphantom			}
56983104Sphantom		}
57083104Sphantom		if (avalue_size != 0)
57183104Sphantom			GRPCPY(avalue_size);
57283104Sphantom		padded -= groups;
57383104Sphantom
57483104Sphantom	} else {
57583104Sphantom		bufend -= avalue_size;
57683104Sphantom		memcpy(bufend, avalue+padded, avalue_size);
57783104Sphantom		if (right_prec == 0)
57883104Sphantom			padded--;	/* decrease assumed $decimal_point */
57983104Sphantom	}
58083104Sphantom
58183104Sphantom	/* do padding with pad_char */
58283104Sphantom	if (padded > 0) {
58383104Sphantom		bufend -= padded;
58483104Sphantom		memset(bufend, pad_char, padded);
58583104Sphantom	}
58683104Sphantom
58783559Smike	bufsize = bufsize - (rslt - bufend);
58883104Sphantom	memmove(rslt, bufend, bufsize);
58983104Sphantom	free(avalue);
59083559Smike	return (rslt);
59183104Sphantom}
592