strfmon.c revision 178313
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 178313 2008-04-19 07:22:58Z ru $"); 3083104Sphantom 3183104Sphantom#include <sys/types.h> 3283104Sphantom#include <ctype.h> 3383104Sphantom#include <errno.h> 3483104Sphantom#include <limits.h> 3583104Sphantom#include <locale.h> 36150065Sstefanf#include <monetary.h> 3783104Sphantom#include <stdarg.h> 3883104Sphantom#include <stdio.h> 3983104Sphantom#include <stdlib.h> 4083104Sphantom#include <string.h> 4183104Sphantom 4283104Sphantom/* internal flags */ 4383104Sphantom#define NEED_GROUPING 0x01 /* print digits grouped (default) */ 4483104Sphantom#define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 4583104Sphantom#define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 4683104Sphantom#define PARENTH_POSN 0x08 /* enclose negative amount in () */ 4783104Sphantom#define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 4883104Sphantom#define LEFT_JUSTIFY 0x20 /* left justify */ 4983104Sphantom#define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 5083104Sphantom#define IS_NEGATIVE 0x80 /* is argument value negative ? */ 5183104Sphantom 5283104Sphantom/* internal macros */ 5383559Smike#define PRINT(CH) do { \ 5483104Sphantom if (dst >= s + maxsize) \ 5583104Sphantom goto e2big_error; \ 5683104Sphantom *dst++ = CH; \ 5783559Smike} while (0) 5883104Sphantom 5983559Smike#define PRINTS(STR) do { \ 6083104Sphantom char *tmps = STR; \ 6183104Sphantom while (*tmps != '\0') \ 6283104Sphantom PRINT(*tmps++); \ 6383559Smike} while (0) 6483104Sphantom 6583559Smike#define GET_NUMBER(VAR) do { \ 6683104Sphantom VAR = 0; \ 6783104Sphantom while (isdigit((unsigned char)*fmt)) { \ 6883104Sphantom VAR *= 10; \ 6983104Sphantom VAR += *fmt - '0'; \ 70178312Sru if (VAR < 0) \ 71178312Sru goto e2big_error; \ 7283104Sphantom fmt++; \ 7383104Sphantom } \ 7483559Smike} while (0) 7583104Sphantom 7683559Smike#define GRPCPY(howmany) do { \ 7783559Smike int i = howmany; \ 7883559Smike while (i-- > 0) { \ 7983559Smike avalue_size--; \ 8083559Smike *--bufend = *(avalue+avalue_size+padded); \ 8183559Smike } \ 8283559Smike} while (0) 8383559Smike 8483559Smike#define GRPSEP do { \ 8583559Smike *--bufend = thousands_sep; \ 8683559Smike groups++; \ 8783559Smike} while (0) 8883559Smike 8983104Sphantomstatic void __setup_vars(int, char *, char *, char *, char **); 9083104Sphantomstatic int __calc_left_pad(int, char *); 9183104Sphantomstatic char *__format_grouped_double(double, int *, int, int, int); 9283104Sphantom 9383104Sphantomssize_t 94103668Smikestrfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 95103668Smike ...) 9683104Sphantom{ 9783104Sphantom va_list ap; 9883104Sphantom char *dst; /* output destination pointer */ 9983104Sphantom const char *fmt; /* current format poistion pointer */ 10083104Sphantom struct lconv *lc; /* pointer to lconv structure */ 10183104Sphantom char *asciivalue; /* formatted double pointer */ 10283104Sphantom 10383104Sphantom int flags; /* formatting options */ 10483104Sphantom int pad_char; /* padding character */ 10583104Sphantom int pad_size; /* pad size */ 10683104Sphantom int width; /* field width */ 10783104Sphantom int left_prec; /* left precision */ 10883104Sphantom int right_prec; /* right precision */ 10983104Sphantom double value; /* just value */ 11083104Sphantom char space_char = ' '; /* space after currency */ 11183104Sphantom 11283104Sphantom char cs_precedes, /* values gathered from struct lconv */ 11383104Sphantom sep_by_space, 11483104Sphantom sign_posn, 11583104Sphantom *signstr, 11683104Sphantom *currency_symbol; 11783104Sphantom 11883104Sphantom char *tmpptr; /* temporary vars */ 119104946Stjr int sverrno; 12083104Sphantom 12183104Sphantom va_start(ap, format); 12283104Sphantom 12383104Sphantom lc = localeconv(); 12483104Sphantom dst = s; 12583104Sphantom fmt = format; 12683104Sphantom asciivalue = NULL; 12783104Sphantom currency_symbol = NULL; 12883104Sphantom pad_size = 0; 12983104Sphantom 13083104Sphantom while (*fmt) { 13183104Sphantom /* pass nonformating characters AS IS */ 13283559Smike if (*fmt != '%') 13383104Sphantom goto literal; 13483104Sphantom 13583104Sphantom /* '%' found ! */ 13683104Sphantom 13783104Sphantom /* "%%" mean just '%' */ 13883104Sphantom if (*(fmt+1) == '%') { 13983104Sphantom fmt++; 14083104Sphantom literal: 14183104Sphantom PRINT(*fmt++); 14283104Sphantom continue; 14383104Sphantom } 14483104Sphantom 14583104Sphantom /* set up initial values */ 14683104Sphantom flags = (NEED_GROUPING|LOCALE_POSN); 14783104Sphantom pad_char = ' '; /* padding character is "space" */ 14883104Sphantom left_prec = -1; /* no left precision specified */ 14983104Sphantom right_prec = -1; /* no right precision specified */ 15083104Sphantom width = -1; /* no width specified */ 15183104Sphantom value = 0; /* we have no value to print now */ 15283104Sphantom 15383104Sphantom /* Flags */ 15483104Sphantom while (1) { 15583104Sphantom switch (*++fmt) { 15683104Sphantom case '=': /* fill character */ 15783104Sphantom pad_char = *++fmt; 15883104Sphantom if (pad_char == '\0') 15983104Sphantom goto format_error; 16083104Sphantom continue; 16183104Sphantom case '^': /* not group currency */ 16283104Sphantom flags &= ~(NEED_GROUPING); 16383104Sphantom continue; 16483104Sphantom case '+': /* use locale defined signs */ 16583104Sphantom if (flags & SIGN_POSN_USED) 16683104Sphantom goto format_error; 16783104Sphantom flags |= (SIGN_POSN_USED|LOCALE_POSN); 16883104Sphantom continue; 16983104Sphantom case '(': /* enclose negatives with () */ 17083104Sphantom if (flags & SIGN_POSN_USED) 17183104Sphantom goto format_error; 17283104Sphantom flags |= (SIGN_POSN_USED|PARENTH_POSN); 17383104Sphantom continue; 17483104Sphantom case '!': /* suppress currency symbol */ 17583104Sphantom flags |= SUPRESS_CURR_SYMBOL; 17683104Sphantom continue; 17783104Sphantom case '-': /* alignment (left) */ 17883104Sphantom flags |= LEFT_JUSTIFY; 17983104Sphantom continue; 18083104Sphantom default: 18183104Sphantom break; 18283104Sphantom } 18383104Sphantom break; 18483104Sphantom } 18583104Sphantom 18683104Sphantom /* field Width */ 18783104Sphantom if (isdigit((unsigned char)*fmt)) { 18883104Sphantom GET_NUMBER(width); 18983104Sphantom /* Do we have enough space to put number with 19083104Sphantom * required width ? 19183104Sphantom */ 192178312Sru if ((unsigned int)width >= maxsize - (dst - s)) 19383104Sphantom goto e2big_error; 19483104Sphantom } 195104942Stjr 196104942Stjr /* Left precision */ 197104942Stjr if (*fmt == '#') { 198104942Stjr if (!isdigit((unsigned char)*++fmt)) 199104942Stjr goto format_error; 200104942Stjr GET_NUMBER(left_prec); 201178312Sru if ((unsigned int)left_prec >= maxsize - (dst - s)) 202178312Sru goto e2big_error; 203104942Stjr } 204104942Stjr 205104942Stjr /* Right precision */ 206104942Stjr if (*fmt == '.') { 207104942Stjr if (!isdigit((unsigned char)*++fmt)) 208104942Stjr goto format_error; 209104942Stjr GET_NUMBER(right_prec); 210178312Sru if ((unsigned int)right_prec >= maxsize - (dst - s) - 211178312Sru left_prec) 212178312Sru goto e2big_error; 213104942Stjr } 214104942Stjr 21583104Sphantom /* Conversion Characters */ 21683104Sphantom switch (*fmt++) { 21783104Sphantom case 'i': /* use internaltion currency format */ 21883104Sphantom flags |= USE_INTL_CURRENCY; 21983104Sphantom break; 22083104Sphantom case 'n': /* use national currency format */ 22183104Sphantom flags &= ~(USE_INTL_CURRENCY); 22283104Sphantom break; 22383104Sphantom default: /* required character is missing or 22483104Sphantom premature EOS */ 22583104Sphantom goto format_error; 22683104Sphantom } 22783104Sphantom 228178313Sru if (currency_symbol != NULL) 229178313Sru free(currency_symbol); 23083104Sphantom if (flags & USE_INTL_CURRENCY) { 23183104Sphantom currency_symbol = strdup(lc->int_curr_symbol); 23283104Sphantom if (currency_symbol != NULL) 23383104Sphantom space_char = *(currency_symbol+3); 23483559Smike } else 23583104Sphantom currency_symbol = strdup(lc->currency_symbol); 23683104Sphantom 23783104Sphantom if (currency_symbol == NULL) 23883104Sphantom goto end_error; /* ENOMEM. */ 23983104Sphantom 24083104Sphantom /* value itself */ 24183104Sphantom value = va_arg(ap, double); 24283104Sphantom 24383104Sphantom /* detect sign */ 24483104Sphantom if (value < 0) { 24583104Sphantom flags |= IS_NEGATIVE; 24683104Sphantom value = -value; 24783104Sphantom } 24883104Sphantom 24983104Sphantom /* fill left_prec with amount of padding chars */ 25083104Sphantom if (left_prec >= 0) { 25183104Sphantom pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 25283104Sphantom currency_symbol) - 25383104Sphantom __calc_left_pad(flags, currency_symbol); 25483104Sphantom if (pad_size < 0) 25583104Sphantom pad_size = 0; 25683104Sphantom } 25783104Sphantom 258178313Sru if (asciivalue != NULL) 259178313Sru free(asciivalue); 26083104Sphantom asciivalue = __format_grouped_double(value, &flags, 26183104Sphantom left_prec, right_prec, pad_char); 26283104Sphantom if (asciivalue == NULL) 26383104Sphantom goto end_error; /* errno already set */ 26483104Sphantom /* to ENOMEM by malloc() */ 26583104Sphantom 26683104Sphantom /* set some variables for later use */ 26783104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, 26883104Sphantom &sign_posn, &signstr); 26983104Sphantom 27083104Sphantom /* 27183104Sphantom * Description of some LC_MONETARY's values: 27283104Sphantom * 27383104Sphantom * p_cs_precedes & n_cs_precedes 27483104Sphantom * 27583104Sphantom * = 1 - $currency_symbol precedes the value 27683104Sphantom * for a monetary quantity with a non-negative value 27783104Sphantom * = 0 - symbol succeeds the value 27883104Sphantom * 27983104Sphantom * p_sep_by_space & n_sep_by_space 28083104Sphantom * 28183104Sphantom * = 0 - no space separates $currency_symbol 28283104Sphantom * from the value for a monetary quantity with a 28383104Sphantom * non-negative value 28483104Sphantom * = 1 - space separates the symbol from the value 28583104Sphantom * = 2 - space separates the symbol and the sign string, 28683104Sphantom * if adjacent. 28783104Sphantom * 28883104Sphantom * p_sign_posn & n_sign_posn 28983104Sphantom * 29083104Sphantom * = 0 - parentheses enclose the quantity and the 29183104Sphantom * $currency_symbol 29283104Sphantom * = 1 - the sign string precedes the quantity and the 29383104Sphantom * $currency_symbol 29483104Sphantom * = 2 - the sign string succeeds the quantity and the 29583104Sphantom * $currency_symbol 29683104Sphantom * = 3 - the sign string precedes the $currency_symbol 29783104Sphantom * = 4 - the sign string succeeds the $currency_symbol 29883104Sphantom * 29983104Sphantom */ 30083104Sphantom 30183104Sphantom tmpptr = dst; 30283104Sphantom 30383104Sphantom while (pad_size-- > 0) 30483104Sphantom PRINT(' '); 30583104Sphantom 306104943Stjr if (sign_posn == 0 && (flags & IS_NEGATIVE)) 307104943Stjr PRINT('('); 30883104Sphantom 30983104Sphantom if (cs_precedes == 1) { 31083104Sphantom if (sign_posn == 1 || sign_posn == 3) { 31183104Sphantom PRINTS(signstr); 31283104Sphantom if (sep_by_space == 2) /* XXX: ? */ 31383104Sphantom PRINT(' '); 31483104Sphantom } 31583104Sphantom 31683104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 31783104Sphantom PRINTS(currency_symbol); 31883104Sphantom 31983104Sphantom if (sign_posn == 4) { 32083104Sphantom if (sep_by_space == 2) 32183104Sphantom PRINT(space_char); 32283559Smike PRINTS(signstr); 32383104Sphantom if (sep_by_space == 1) 32483104Sphantom PRINT(' '); 32583559Smike } else if (sep_by_space == 1) 32683559Smike PRINT(space_char); 32783104Sphantom } 32883559Smike } else if (sign_posn == 1) 32983559Smike PRINTS(signstr); 33083104Sphantom 33183104Sphantom PRINTS(asciivalue); 33283104Sphantom 33383104Sphantom if (cs_precedes == 0) { 33483104Sphantom if (sign_posn == 3) { 33583104Sphantom if (sep_by_space == 1) 33683104Sphantom PRINT(' '); 33783104Sphantom PRINTS(signstr); 33883104Sphantom } 33983104Sphantom 34083104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 34183104Sphantom if ((sign_posn == 3 && sep_by_space == 2) 34283559Smike || (sep_by_space == 1 343104963Stjr && (sign_posn == 0 34483559Smike || sign_posn == 1 34583559Smike || sign_posn == 2 34683559Smike || sign_posn == 4))) 34783559Smike PRINT(space_char); 34883104Sphantom PRINTS(currency_symbol); /* XXX: len */ 34983104Sphantom if (sign_posn == 4) { 35083104Sphantom if (sep_by_space == 2) 35183104Sphantom PRINT(' '); 35283104Sphantom PRINTS(signstr); 35383104Sphantom } 35483104Sphantom } 35583104Sphantom } 35683104Sphantom 35783104Sphantom if (sign_posn == 2) { 35883104Sphantom if (sep_by_space == 2) 35983104Sphantom PRINT(' '); 36083104Sphantom PRINTS(signstr); 36183104Sphantom } 36283104Sphantom 36383104Sphantom if (sign_posn == 0 && (flags & IS_NEGATIVE)) 36483104Sphantom PRINT(')'); 36583104Sphantom 36683104Sphantom if (dst - tmpptr < width) { 36783104Sphantom if (flags & LEFT_JUSTIFY) { 368104963Stjr while (dst - tmpptr < width) 36983104Sphantom PRINT(' '); 37083104Sphantom } else { 37183104Sphantom pad_size = dst-tmpptr; 37283559Smike memmove(tmpptr + width-pad_size, tmpptr, 37383559Smike pad_size); 37483104Sphantom memset(tmpptr, ' ', width-pad_size); 37583104Sphantom dst += width-pad_size; 37683104Sphantom } 37783104Sphantom } 37883104Sphantom } 37983104Sphantom 38083104Sphantom PRINT('\0'); 38183104Sphantom va_end(ap); 382104963Stjr free(asciivalue); 383104963Stjr free(currency_symbol); 38483104Sphantom return (dst - s - 1); /* return size of put data except trailing '\0' */ 38583104Sphantom 38683104Sphantome2big_error: 38783104Sphantom errno = E2BIG; 38883104Sphantom goto end_error; 38983104Sphantom 39083104Sphantomformat_error: 39183104Sphantom errno = EINVAL; 39283104Sphantom 39383104Sphantomend_error: 394104946Stjr sverrno = errno; 39583104Sphantom if (asciivalue != NULL) 39683104Sphantom free(asciivalue); 39783104Sphantom if (currency_symbol != NULL) 39883104Sphantom free(currency_symbol); 399104946Stjr errno = sverrno; 40083104Sphantom va_end(ap); 40183104Sphantom return (-1); 40283104Sphantom} 40383104Sphantom 40483104Sphantomstatic void 40583104Sphantom__setup_vars(int flags, char *cs_precedes, char *sep_by_space, 40683104Sphantom char *sign_posn, char **signstr) { 40783104Sphantom 40883104Sphantom struct lconv *lc = localeconv(); 40983104Sphantom 410104944Stjr if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 411104944Stjr *cs_precedes = lc->int_n_cs_precedes; 412104944Stjr *sep_by_space = lc->int_n_sep_by_space; 413104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 414104944Stjr *signstr = (lc->negative_sign == '\0') ? "-" 415104944Stjr : lc->negative_sign; 416104944Stjr } else if (flags & USE_INTL_CURRENCY) { 417104944Stjr *cs_precedes = lc->int_p_cs_precedes; 418104944Stjr *sep_by_space = lc->int_p_sep_by_space; 419104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 420104944Stjr *signstr = lc->positive_sign; 421104944Stjr } else if (flags & IS_NEGATIVE) { 42283104Sphantom *cs_precedes = lc->n_cs_precedes; 42383104Sphantom *sep_by_space = lc->n_sep_by_space; 42483559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 42583559Smike *signstr = (lc->negative_sign == '\0') ? "-" 42683559Smike : lc->negative_sign; 42783104Sphantom } else { 42883104Sphantom *cs_precedes = lc->p_cs_precedes; 42983104Sphantom *sep_by_space = lc->p_sep_by_space; 43083559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 43183104Sphantom *signstr = lc->positive_sign; 43283104Sphantom } 43383104Sphantom 43483104Sphantom /* Set defult values for unspecified information. */ 43583104Sphantom if (*cs_precedes != 0) 43683104Sphantom *cs_precedes = 1; 43783104Sphantom if (*sep_by_space == CHAR_MAX) 43883104Sphantom *sep_by_space = 0; 43983104Sphantom if (*sign_posn == CHAR_MAX) 44083104Sphantom *sign_posn = 0; 44183104Sphantom} 44283104Sphantom 44383104Sphantomstatic int 44483104Sphantom__calc_left_pad(int flags, char *cur_symb) { 44583104Sphantom 44683104Sphantom char cs_precedes, sep_by_space, sign_posn, *signstr; 44783104Sphantom int left_chars = 0; 44883104Sphantom 44983104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 45083104Sphantom 45183104Sphantom if (cs_precedes != 0) { 45283104Sphantom left_chars += strlen(cur_symb); 45383104Sphantom if (sep_by_space != 0) 45483104Sphantom left_chars++; 45583104Sphantom } 45683104Sphantom 45783104Sphantom switch (sign_posn) { 45883104Sphantom case 1: 45983104Sphantom left_chars += strlen(signstr); 46083104Sphantom break; 46183104Sphantom case 3: 46283104Sphantom case 4: 46383104Sphantom if (cs_precedes != 0) 46483559Smike left_chars += strlen(signstr); 46583104Sphantom } 46683559Smike return (left_chars); 46783104Sphantom} 46883104Sphantom 46983104Sphantomstatic int 47083104Sphantomget_groups(int size, char *grouping) { 47183104Sphantom 47283104Sphantom int chars = 0; 47383104Sphantom 47483104Sphantom if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 47583559Smike return (0); 47683104Sphantom 47783104Sphantom while (size > (int)*grouping) { 47883104Sphantom chars++; 47983104Sphantom size -= (int)*grouping++; 48083104Sphantom /* no more grouping ? */ 48183104Sphantom if (*grouping == CHAR_MAX) 48283104Sphantom break; 48383104Sphantom /* rest grouping with same value ? */ 48483104Sphantom if (*grouping == 0) { 48583104Sphantom chars += (size - 1) / *(grouping - 1); 48683104Sphantom break; 48783104Sphantom } 48883104Sphantom } 48983559Smike return (chars); 49083104Sphantom} 49183104Sphantom 49283104Sphantom/* convert double to ASCII */ 49383104Sphantomstatic char * 49483104Sphantom__format_grouped_double(double value, int *flags, 49583104Sphantom int left_prec, int right_prec, int pad_char) { 49683104Sphantom 49783104Sphantom char *rslt; 49883104Sphantom char *avalue; 49983104Sphantom int avalue_size; 50083104Sphantom char fmt[32]; 50183104Sphantom 50283104Sphantom size_t bufsize; 50383104Sphantom char *bufend; 50483104Sphantom 50583104Sphantom int padded; 50683104Sphantom 50783104Sphantom struct lconv *lc = localeconv(); 50883104Sphantom char *grouping; 50983104Sphantom char decimal_point; 51083104Sphantom char thousands_sep; 51183104Sphantom 51283104Sphantom int groups = 0; 51383104Sphantom 51483104Sphantom grouping = lc->mon_grouping; 51583104Sphantom decimal_point = *lc->mon_decimal_point; 516112427Sache if (decimal_point == '\0') 51783104Sphantom decimal_point = *lc->decimal_point; 51883104Sphantom thousands_sep = *lc->mon_thousands_sep; 51983559Smike if (thousands_sep == '\0') 52083104Sphantom thousands_sep = *lc->thousands_sep; 52183104Sphantom 52283104Sphantom /* fill left_prec with default value */ 52383104Sphantom if (left_prec == -1) 52483104Sphantom left_prec = 0; 52583104Sphantom 52683104Sphantom /* fill right_prec with default value */ 52783104Sphantom if (right_prec == -1) { 52883104Sphantom if (*flags & USE_INTL_CURRENCY) 52983104Sphantom right_prec = lc->int_frac_digits; 53083104Sphantom else 53183104Sphantom right_prec = lc->frac_digits; 53283104Sphantom 53383104Sphantom if (right_prec == CHAR_MAX) /* POSIX locale ? */ 53483104Sphantom right_prec = 2; 53583104Sphantom } 53683104Sphantom 53783104Sphantom if (*flags & NEED_GROUPING) 53883104Sphantom left_prec += get_groups(left_prec, grouping); 53983104Sphantom 54083104Sphantom /* convert to string */ 54183559Smike snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 54283559Smike right_prec); 54383104Sphantom avalue_size = asprintf(&avalue, fmt, value); 54483104Sphantom if (avalue_size < 0) 54583559Smike return (NULL); 54683104Sphantom 54783104Sphantom /* make sure that we've enough space for result string */ 54883104Sphantom bufsize = strlen(avalue)*2+1; 549178175Sdelphij rslt = calloc(1, bufsize); 55083104Sphantom if (rslt == NULL) { 55183104Sphantom free(avalue); 55283559Smike return (NULL); 55383104Sphantom } 55483104Sphantom bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 55583104Sphantom 55683104Sphantom /* skip spaces at beggining */ 55783104Sphantom padded = 0; 55883104Sphantom while (avalue[padded] == ' ') { 55983104Sphantom padded++; 56083104Sphantom avalue_size--; 56183104Sphantom } 56283104Sphantom 56383104Sphantom if (right_prec > 0) { 56483104Sphantom bufend -= right_prec; 56583559Smike memcpy(bufend, avalue + avalue_size+padded-right_prec, 56683559Smike right_prec); 56783104Sphantom *--bufend = decimal_point; 56883104Sphantom avalue_size -= (right_prec + 1); 56983104Sphantom } 57083104Sphantom 57183104Sphantom if ((*flags & NEED_GROUPING) && 57283559Smike thousands_sep != '\0' && /* XXX: need investigation */ 57383559Smike *grouping != CHAR_MAX && 57483559Smike *grouping > 0) { 57583104Sphantom while (avalue_size > (int)*grouping) { 57683104Sphantom GRPCPY(*grouping); 57783104Sphantom GRPSEP; 57883104Sphantom grouping++; 57983104Sphantom 58083104Sphantom /* no more grouping ? */ 58183104Sphantom if (*grouping == CHAR_MAX) 58283104Sphantom break; 58383104Sphantom 58483104Sphantom /* rest grouping with same value ? */ 58583104Sphantom if (*grouping == 0) { 58683104Sphantom grouping--; 58783104Sphantom while (avalue_size > *grouping) { 58883104Sphantom GRPCPY(*grouping); 58983104Sphantom GRPSEP; 59083104Sphantom } 59183104Sphantom } 59283104Sphantom } 59383104Sphantom if (avalue_size != 0) 59483104Sphantom GRPCPY(avalue_size); 59583104Sphantom padded -= groups; 59683104Sphantom 59783104Sphantom } else { 59883104Sphantom bufend -= avalue_size; 59983104Sphantom memcpy(bufend, avalue+padded, avalue_size); 60083104Sphantom if (right_prec == 0) 60183104Sphantom padded--; /* decrease assumed $decimal_point */ 60283104Sphantom } 60383104Sphantom 60483104Sphantom /* do padding with pad_char */ 60583104Sphantom if (padded > 0) { 60683104Sphantom bufend -= padded; 60783104Sphantom memset(bufend, pad_char, padded); 60883104Sphantom } 60983104Sphantom 610104963Stjr bufsize = bufsize - (bufend - rslt) + 1; 61183104Sphantom memmove(rslt, bufend, bufsize); 61283104Sphantom free(avalue); 61383559Smike return (rslt); 61483104Sphantom} 615