strfmon.c revision 104946
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 104946 2002-10-11 23:31:50Z 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; 117104946Stjr int sverrno; 11883104Sphantom 11983104Sphantom va_start(ap, format); 12083104Sphantom 12183104Sphantom lc = localeconv(); 12283104Sphantom dst = s; 12383104Sphantom fmt = format; 12483104Sphantom asciivalue = NULL; 12583104Sphantom currency_symbol = NULL; 12683104Sphantom pad_size = 0; 12783104Sphantom 12883104Sphantom while (*fmt) { 12983104Sphantom /* pass nonformating characters AS IS */ 13083559Smike if (*fmt != '%') 13183104Sphantom goto literal; 13283104Sphantom 13383104Sphantom /* '%' found ! */ 13483104Sphantom 13583104Sphantom /* "%%" mean just '%' */ 13683104Sphantom if (*(fmt+1) == '%') { 13783104Sphantom fmt++; 13883104Sphantom literal: 13983104Sphantom PRINT(*fmt++); 14083104Sphantom continue; 14183104Sphantom } 14283104Sphantom 14383104Sphantom /* set up initial values */ 14483104Sphantom flags = (NEED_GROUPING|LOCALE_POSN); 14583104Sphantom pad_char = ' '; /* padding character is "space" */ 14683104Sphantom left_prec = -1; /* no left precision specified */ 14783104Sphantom right_prec = -1; /* no right precision specified */ 14883104Sphantom width = -1; /* no width specified */ 14983104Sphantom value = 0; /* we have no value to print now */ 15083104Sphantom 15183104Sphantom /* Flags */ 15283104Sphantom while (1) { 15383104Sphantom switch (*++fmt) { 15483104Sphantom case '=': /* fill character */ 15583104Sphantom pad_char = *++fmt; 15683104Sphantom if (pad_char == '\0') 15783104Sphantom goto format_error; 15883104Sphantom continue; 15983104Sphantom case '^': /* not group currency */ 16083104Sphantom flags &= ~(NEED_GROUPING); 16183104Sphantom continue; 16283104Sphantom case '+': /* use locale defined signs */ 16383104Sphantom if (flags & SIGN_POSN_USED) 16483104Sphantom goto format_error; 16583104Sphantom flags |= (SIGN_POSN_USED|LOCALE_POSN); 16683104Sphantom continue; 16783104Sphantom case '(': /* enclose negatives with () */ 16883104Sphantom if (flags & SIGN_POSN_USED) 16983104Sphantom goto format_error; 17083104Sphantom flags |= (SIGN_POSN_USED|PARENTH_POSN); 17183104Sphantom continue; 17283104Sphantom case '!': /* suppress currency symbol */ 17383104Sphantom flags |= SUPRESS_CURR_SYMBOL; 17483104Sphantom continue; 17583104Sphantom case '-': /* alignment (left) */ 17683104Sphantom flags |= LEFT_JUSTIFY; 17783104Sphantom continue; 17883104Sphantom default: 17983104Sphantom break; 18083104Sphantom } 18183104Sphantom break; 18283104Sphantom } 18383104Sphantom 18483104Sphantom /* field Width */ 18583104Sphantom if (isdigit((unsigned char)*fmt)) { 18683104Sphantom GET_NUMBER(width); 18783104Sphantom /* Do we have enough space to put number with 18883104Sphantom * required width ? 18983104Sphantom */ 19083104Sphantom if (dst + width >= s + maxsize) 19183104Sphantom goto e2big_error; 19283104Sphantom } 193104942Stjr 194104942Stjr /* Left precision */ 195104942Stjr if (*fmt == '#') { 196104942Stjr if (!isdigit((unsigned char)*++fmt)) 197104942Stjr goto format_error; 198104942Stjr GET_NUMBER(left_prec); 199104942Stjr } 200104942Stjr 201104942Stjr /* Right precision */ 202104942Stjr if (*fmt == '.') { 203104942Stjr if (!isdigit((unsigned char)*++fmt)) 204104942Stjr goto format_error; 205104942Stjr GET_NUMBER(right_prec); 206104942Stjr } 207104942Stjr 20883104Sphantom /* Conversion Characters */ 20983104Sphantom switch (*fmt++) { 21083104Sphantom case 'i': /* use internaltion currency format */ 21183104Sphantom flags |= USE_INTL_CURRENCY; 21283104Sphantom break; 21383104Sphantom case 'n': /* use national currency format */ 21483104Sphantom flags &= ~(USE_INTL_CURRENCY); 21583104Sphantom break; 21683104Sphantom default: /* required character is missing or 21783104Sphantom premature EOS */ 21883104Sphantom goto format_error; 21983104Sphantom } 22083104Sphantom 22183104Sphantom if (flags & USE_INTL_CURRENCY) { 22283104Sphantom currency_symbol = strdup(lc->int_curr_symbol); 22383104Sphantom if (currency_symbol != NULL) 22483104Sphantom space_char = *(currency_symbol+3); 22583559Smike } else 22683104Sphantom currency_symbol = strdup(lc->currency_symbol); 22783104Sphantom 22883104Sphantom if (currency_symbol == NULL) 22983104Sphantom goto end_error; /* ENOMEM. */ 23083104Sphantom 23183104Sphantom /* value itself */ 23283104Sphantom value = va_arg(ap, double); 23383104Sphantom 23483104Sphantom /* detect sign */ 23583104Sphantom if (value < 0) { 23683104Sphantom flags |= IS_NEGATIVE; 23783104Sphantom value = -value; 23883104Sphantom } 23983104Sphantom 24083104Sphantom /* fill left_prec with amount of padding chars */ 24183104Sphantom if (left_prec >= 0) { 24283104Sphantom pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 24383104Sphantom currency_symbol) - 24483104Sphantom __calc_left_pad(flags, currency_symbol); 24583104Sphantom if (pad_size < 0) 24683104Sphantom pad_size = 0; 24783104Sphantom } 24883104Sphantom 24983104Sphantom asciivalue = __format_grouped_double(value, &flags, 25083104Sphantom left_prec, right_prec, pad_char); 25183104Sphantom if (asciivalue == NULL) 25283104Sphantom goto end_error; /* errno already set */ 25383104Sphantom /* to ENOMEM by malloc() */ 25483104Sphantom 25583104Sphantom /* set some variables for later use */ 25683104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, 25783104Sphantom &sign_posn, &signstr); 25883104Sphantom 25983104Sphantom /* 26083104Sphantom * Description of some LC_MONETARY's values: 26183104Sphantom * 26283104Sphantom * p_cs_precedes & n_cs_precedes 26383104Sphantom * 26483104Sphantom * = 1 - $currency_symbol precedes the value 26583104Sphantom * for a monetary quantity with a non-negative value 26683104Sphantom * = 0 - symbol succeeds the value 26783104Sphantom * 26883104Sphantom * p_sep_by_space & n_sep_by_space 26983104Sphantom * 27083104Sphantom * = 0 - no space separates $currency_symbol 27183104Sphantom * from the value for a monetary quantity with a 27283104Sphantom * non-negative value 27383104Sphantom * = 1 - space separates the symbol from the value 27483104Sphantom * = 2 - space separates the symbol and the sign string, 27583104Sphantom * if adjacent. 27683104Sphantom * 27783104Sphantom * p_sign_posn & n_sign_posn 27883104Sphantom * 27983104Sphantom * = 0 - parentheses enclose the quantity and the 28083104Sphantom * $currency_symbol 28183104Sphantom * = 1 - the sign string precedes the quantity and the 28283104Sphantom * $currency_symbol 28383104Sphantom * = 2 - the sign string succeeds the quantity and the 28483104Sphantom * $currency_symbol 28583104Sphantom * = 3 - the sign string precedes the $currency_symbol 28683104Sphantom * = 4 - the sign string succeeds the $currency_symbol 28783104Sphantom * 28883104Sphantom */ 28983104Sphantom 29083104Sphantom tmpptr = dst; 29183104Sphantom 29283104Sphantom while (pad_size-- > 0) 29383104Sphantom PRINT(' '); 29483104Sphantom 295104943Stjr if (sign_posn == 0 && (flags & IS_NEGATIVE)) 296104943Stjr PRINT('('); 29783104Sphantom 29883104Sphantom if (cs_precedes == 1) { 29983104Sphantom if (sign_posn == 1 || sign_posn == 3) { 30083104Sphantom PRINTS(signstr); 30183104Sphantom if (sep_by_space == 2) /* XXX: ? */ 30283104Sphantom PRINT(' '); 30383104Sphantom } 30483104Sphantom 30583104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 30683104Sphantom PRINTS(currency_symbol); 30783104Sphantom 30883104Sphantom if (sign_posn == 4) { 30983104Sphantom if (sep_by_space == 2) 31083104Sphantom PRINT(space_char); 31183559Smike PRINTS(signstr); 31283104Sphantom if (sep_by_space == 1) 31383104Sphantom PRINT(' '); 31483559Smike } else if (sep_by_space == 1) 31583559Smike PRINT(space_char); 31683104Sphantom } 31783559Smike } else if (sign_posn == 1) 31883559Smike PRINTS(signstr); 31983104Sphantom 32083104Sphantom PRINTS(asciivalue); 32183104Sphantom 32283104Sphantom if (cs_precedes == 0) { 32383104Sphantom if (sign_posn == 3) { 32483104Sphantom if (sep_by_space == 1) 32583104Sphantom PRINT(' '); 32683104Sphantom PRINTS(signstr); 32783104Sphantom } 32883104Sphantom 32983104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 33083104Sphantom if ((sign_posn == 3 && sep_by_space == 2) 33183559Smike || (sep_by_space == 1 33283559Smike && (sign_posn = 0 33383559Smike || sign_posn == 1 33483559Smike || sign_posn == 2 33583559Smike || sign_posn == 4))) 33683559Smike PRINT(space_char); 33783104Sphantom PRINTS(currency_symbol); /* XXX: len */ 33883104Sphantom if (sign_posn == 4) { 33983104Sphantom if (sep_by_space == 2) 34083104Sphantom PRINT(' '); 34183104Sphantom PRINTS(signstr); 34283104Sphantom } 34383104Sphantom } 34483104Sphantom } 34583104Sphantom 34683104Sphantom if (sign_posn == 2) { 34783104Sphantom if (sep_by_space == 2) 34883104Sphantom PRINT(' '); 34983104Sphantom PRINTS(signstr); 35083104Sphantom } 35183104Sphantom 35283104Sphantom if (sign_posn == 0 && (flags & IS_NEGATIVE)) 35383104Sphantom PRINT(')'); 35483104Sphantom 35583104Sphantom if (dst - tmpptr < width) { 35683104Sphantom if (flags & LEFT_JUSTIFY) { 35783104Sphantom while (dst - tmpptr <= width) 35883104Sphantom PRINT(' '); 35983104Sphantom } else { 36083104Sphantom pad_size = dst-tmpptr; 36183559Smike memmove(tmpptr + width-pad_size, tmpptr, 36283559Smike pad_size); 36383104Sphantom memset(tmpptr, ' ', width-pad_size); 36483104Sphantom dst += width-pad_size; 36583104Sphantom } 36683104Sphantom } 36783104Sphantom } 36883104Sphantom 36983104Sphantom PRINT('\0'); 37083104Sphantom va_end(ap); 37183104Sphantom return (dst - s - 1); /* return size of put data except trailing '\0' */ 37283104Sphantom 37383104Sphantome2big_error: 37483104Sphantom errno = E2BIG; 37583104Sphantom goto end_error; 37683104Sphantom 37783104Sphantomformat_error: 37883104Sphantom errno = EINVAL; 37983104Sphantom 38083104Sphantomend_error: 381104946Stjr sverrno = errno; 38283104Sphantom if (asciivalue != NULL) 38383104Sphantom free(asciivalue); 38483104Sphantom if (currency_symbol != NULL) 38583104Sphantom free(currency_symbol); 386104946Stjr errno = sverrno; 38783104Sphantom va_end(ap); 38883104Sphantom return (-1); 38983104Sphantom} 39083104Sphantom 39183104Sphantomstatic void 39283104Sphantom__setup_vars(int flags, char *cs_precedes, char *sep_by_space, 39383104Sphantom char *sign_posn, char **signstr) { 39483104Sphantom 39583104Sphantom struct lconv *lc = localeconv(); 39683104Sphantom 397104944Stjr if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 398104944Stjr *cs_precedes = lc->int_n_cs_precedes; 399104944Stjr *sep_by_space = lc->int_n_sep_by_space; 400104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 401104944Stjr *signstr = (lc->negative_sign == '\0') ? "-" 402104944Stjr : lc->negative_sign; 403104944Stjr } else if (flags & USE_INTL_CURRENCY) { 404104944Stjr *cs_precedes = lc->int_p_cs_precedes; 405104944Stjr *sep_by_space = lc->int_p_sep_by_space; 406104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 407104944Stjr *signstr = lc->positive_sign; 408104944Stjr } else if (flags & IS_NEGATIVE) { 40983104Sphantom *cs_precedes = lc->n_cs_precedes; 41083104Sphantom *sep_by_space = lc->n_sep_by_space; 41183559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 41283559Smike *signstr = (lc->negative_sign == '\0') ? "-" 41383559Smike : lc->negative_sign; 41483104Sphantom } else { 41583104Sphantom *cs_precedes = lc->p_cs_precedes; 41683104Sphantom *sep_by_space = lc->p_sep_by_space; 41783559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 41883104Sphantom *signstr = lc->positive_sign; 41983104Sphantom } 42083104Sphantom 42183104Sphantom /* Set defult values for unspecified information. */ 42283104Sphantom if (*cs_precedes != 0) 42383104Sphantom *cs_precedes = 1; 42483104Sphantom if (*sep_by_space == CHAR_MAX) 42583104Sphantom *sep_by_space = 0; 42683104Sphantom if (*sign_posn == CHAR_MAX) 42783104Sphantom *sign_posn = 0; 42883104Sphantom} 42983104Sphantom 43083104Sphantomstatic int 43183104Sphantom__calc_left_pad(int flags, char *cur_symb) { 43283104Sphantom 43383104Sphantom char cs_precedes, sep_by_space, sign_posn, *signstr; 43483104Sphantom int left_chars = 0; 43583104Sphantom 43683104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 43783104Sphantom 43883104Sphantom if (cs_precedes != 0) { 43983104Sphantom left_chars += strlen(cur_symb); 44083104Sphantom if (sep_by_space != 0) 44183104Sphantom left_chars++; 44283104Sphantom } 44383104Sphantom 44483104Sphantom switch (sign_posn) { 44583104Sphantom case 1: 44683104Sphantom left_chars += strlen(signstr); 44783104Sphantom break; 44883104Sphantom case 3: 44983104Sphantom case 4: 45083104Sphantom if (cs_precedes != 0) 45183559Smike left_chars += strlen(signstr); 45283104Sphantom } 45383559Smike return (left_chars); 45483104Sphantom} 45583104Sphantom 45683104Sphantomstatic int 45783104Sphantomget_groups(int size, char *grouping) { 45883104Sphantom 45983104Sphantom int chars = 0; 46083104Sphantom 46183104Sphantom if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 46283559Smike return (0); 46383104Sphantom 46483104Sphantom while (size > (int)*grouping) { 46583104Sphantom chars++; 46683104Sphantom size -= (int)*grouping++; 46783104Sphantom /* no more grouping ? */ 46883104Sphantom if (*grouping == CHAR_MAX) 46983104Sphantom break; 47083104Sphantom /* rest grouping with same value ? */ 47183104Sphantom if (*grouping == 0) { 47283104Sphantom chars += (size - 1) / *(grouping - 1); 47383104Sphantom break; 47483104Sphantom } 47583104Sphantom } 47683559Smike return (chars); 47783104Sphantom} 47883104Sphantom 47983104Sphantom/* convert double to ASCII */ 48083104Sphantomstatic char * 48183104Sphantom__format_grouped_double(double value, int *flags, 48283104Sphantom int left_prec, int right_prec, int pad_char) { 48383104Sphantom 48483104Sphantom char *rslt; 48583104Sphantom char *avalue; 48683104Sphantom int avalue_size; 48783104Sphantom char fmt[32]; 48883104Sphantom 48983104Sphantom size_t bufsize; 49083104Sphantom char *bufend; 49183104Sphantom 49283104Sphantom int padded; 49383104Sphantom 49483104Sphantom struct lconv *lc = localeconv(); 49583104Sphantom char *grouping; 49683104Sphantom char decimal_point; 49783104Sphantom char thousands_sep; 49883104Sphantom 49983104Sphantom int groups = 0; 50083104Sphantom 50183104Sphantom grouping = lc->mon_grouping; 50283104Sphantom decimal_point = *lc->mon_decimal_point; 50383104Sphantom if (decimal_point == '\0') { 50483104Sphantom decimal_point = *lc->decimal_point; 50583104Sphantom if (decimal_point == '\0') 50683104Sphantom decimal_point = '.'; 50783104Sphantom } 50883104Sphantom thousands_sep = *lc->mon_thousands_sep; 50983559Smike if (thousands_sep == '\0') 51083104Sphantom thousands_sep = *lc->thousands_sep; 51183104Sphantom 51283104Sphantom /* fill left_prec with default value */ 51383104Sphantom if (left_prec == -1) 51483104Sphantom left_prec = 0; 51583104Sphantom 51683104Sphantom /* fill right_prec with default value */ 51783104Sphantom if (right_prec == -1) { 51883104Sphantom if (*flags & USE_INTL_CURRENCY) 51983104Sphantom right_prec = lc->int_frac_digits; 52083104Sphantom else 52183104Sphantom right_prec = lc->frac_digits; 52283104Sphantom 52383104Sphantom if (right_prec == CHAR_MAX) /* POSIX locale ? */ 52483104Sphantom right_prec = 2; 52583104Sphantom } 52683104Sphantom 52783104Sphantom if (*flags & NEED_GROUPING) 52883104Sphantom left_prec += get_groups(left_prec, grouping); 52983104Sphantom 53083104Sphantom /* convert to string */ 53183559Smike snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 53283559Smike right_prec); 53383104Sphantom avalue_size = asprintf(&avalue, fmt, value); 53483104Sphantom if (avalue_size < 0) 53583559Smike return (NULL); 53683104Sphantom 53783104Sphantom /* make sure that we've enough space for result string */ 53883104Sphantom bufsize = strlen(avalue)*2+1; 53983104Sphantom rslt = malloc(bufsize); 54083104Sphantom if (rslt == NULL) { 54183104Sphantom free(avalue); 54283559Smike return (NULL); 54383104Sphantom } 54483104Sphantom memset(rslt, 0, bufsize); 54583104Sphantom bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 54683104Sphantom 54783104Sphantom /* skip spaces at beggining */ 54883104Sphantom padded = 0; 54983104Sphantom while (avalue[padded] == ' ') { 55083104Sphantom padded++; 55183104Sphantom avalue_size--; 55283104Sphantom } 55383104Sphantom 55483104Sphantom if (right_prec > 0) { 55583104Sphantom bufend -= right_prec; 55683559Smike memcpy(bufend, avalue + avalue_size+padded-right_prec, 55783559Smike right_prec); 55883104Sphantom *--bufend = decimal_point; 55983104Sphantom avalue_size -= (right_prec + 1); 56083104Sphantom } 56183104Sphantom 56283104Sphantom if ((*flags & NEED_GROUPING) && 56383559Smike thousands_sep != '\0' && /* XXX: need investigation */ 56483559Smike *grouping != CHAR_MAX && 56583559Smike *grouping > 0) { 56683104Sphantom while (avalue_size > (int)*grouping) { 56783104Sphantom GRPCPY(*grouping); 56883104Sphantom GRPSEP; 56983104Sphantom grouping++; 57083104Sphantom 57183104Sphantom /* no more grouping ? */ 57283104Sphantom if (*grouping == CHAR_MAX) 57383104Sphantom break; 57483104Sphantom 57583104Sphantom /* rest grouping with same value ? */ 57683104Sphantom if (*grouping == 0) { 57783104Sphantom grouping--; 57883104Sphantom while (avalue_size > *grouping) { 57983104Sphantom GRPCPY(*grouping); 58083104Sphantom GRPSEP; 58183104Sphantom } 58283104Sphantom } 58383104Sphantom } 58483104Sphantom if (avalue_size != 0) 58583104Sphantom GRPCPY(avalue_size); 58683104Sphantom padded -= groups; 58783104Sphantom 58883104Sphantom } else { 58983104Sphantom bufend -= avalue_size; 59083104Sphantom memcpy(bufend, avalue+padded, avalue_size); 59183104Sphantom if (right_prec == 0) 59283104Sphantom padded--; /* decrease assumed $decimal_point */ 59383104Sphantom } 59483104Sphantom 59583104Sphantom /* do padding with pad_char */ 59683104Sphantom if (padded > 0) { 59783104Sphantom bufend -= padded; 59883104Sphantom memset(bufend, pad_char, padded); 59983104Sphantom } 60083104Sphantom 60183559Smike bufsize = bufsize - (rslt - bufend); 60283104Sphantom memmove(rslt, bufend, bufsize); 60383104Sphantom free(avalue); 60483559Smike return (rslt); 60583104Sphantom} 606