strfmon.c revision 203734
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 203734 2010-02-10 00:02:09Z cracauer $"); 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)) { \ 68178457Sru if (VAR > INT_MAX / 10) \ 69178457Sru goto e2big_error; \ 7083104Sphantom VAR *= 10; \ 7183104Sphantom VAR += *fmt - '0'; \ 72178312Sru if (VAR < 0) \ 73178312Sru goto e2big_error; \ 7483104Sphantom fmt++; \ 7583104Sphantom } \ 7683559Smike} while (0) 7783104Sphantom 7883559Smike#define GRPCPY(howmany) do { \ 7983559Smike int i = howmany; \ 8083559Smike while (i-- > 0) { \ 8183559Smike avalue_size--; \ 8283559Smike *--bufend = *(avalue+avalue_size+padded); \ 8383559Smike } \ 8483559Smike} while (0) 8583559Smike 8683559Smike#define GRPSEP do { \ 8783559Smike *--bufend = thousands_sep; \ 8883559Smike groups++; \ 8983559Smike} while (0) 9083559Smike 9183104Sphantomstatic void __setup_vars(int, char *, char *, char *, char **); 9283104Sphantomstatic int __calc_left_pad(int, char *); 9383104Sphantomstatic char *__format_grouped_double(double, int *, int, int, int); 9483104Sphantom 9583104Sphantomssize_t 96103668Smikestrfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 97103668Smike ...) 9883104Sphantom{ 9983104Sphantom va_list ap; 10083104Sphantom char *dst; /* output destination pointer */ 10183104Sphantom const char *fmt; /* current format poistion pointer */ 10283104Sphantom struct lconv *lc; /* pointer to lconv structure */ 10383104Sphantom char *asciivalue; /* formatted double pointer */ 10483104Sphantom 10583104Sphantom int flags; /* formatting options */ 10683104Sphantom int pad_char; /* padding character */ 10783104Sphantom int pad_size; /* pad size */ 10883104Sphantom int width; /* field width */ 10983104Sphantom int left_prec; /* left precision */ 11083104Sphantom int right_prec; /* right precision */ 11183104Sphantom double value; /* just value */ 11283104Sphantom char space_char = ' '; /* space after currency */ 11383104Sphantom 11483104Sphantom char cs_precedes, /* values gathered from struct lconv */ 11583104Sphantom sep_by_space, 11683104Sphantom sign_posn, 11783104Sphantom *signstr, 11883104Sphantom *currency_symbol; 11983104Sphantom 12083104Sphantom char *tmpptr; /* temporary vars */ 121104946Stjr int sverrno; 12283104Sphantom 12383104Sphantom va_start(ap, format); 12483104Sphantom 12583104Sphantom lc = localeconv(); 12683104Sphantom dst = s; 12783104Sphantom fmt = format; 12883104Sphantom asciivalue = NULL; 12983104Sphantom currency_symbol = NULL; 13083104Sphantom pad_size = 0; 13183104Sphantom 13283104Sphantom while (*fmt) { 13383104Sphantom /* pass nonformating characters AS IS */ 13483559Smike if (*fmt != '%') 13583104Sphantom goto literal; 13683104Sphantom 13783104Sphantom /* '%' found ! */ 13883104Sphantom 13983104Sphantom /* "%%" mean just '%' */ 14083104Sphantom if (*(fmt+1) == '%') { 14183104Sphantom fmt++; 14283104Sphantom literal: 14383104Sphantom PRINT(*fmt++); 14483104Sphantom continue; 14583104Sphantom } 14683104Sphantom 14783104Sphantom /* set up initial values */ 14883104Sphantom flags = (NEED_GROUPING|LOCALE_POSN); 14983104Sphantom pad_char = ' '; /* padding character is "space" */ 15083104Sphantom left_prec = -1; /* no left precision specified */ 15183104Sphantom right_prec = -1; /* no right precision specified */ 15283104Sphantom width = -1; /* no width specified */ 15383104Sphantom value = 0; /* we have no value to print now */ 15483104Sphantom 15583104Sphantom /* Flags */ 15683104Sphantom while (1) { 15783104Sphantom switch (*++fmt) { 15883104Sphantom case '=': /* fill character */ 15983104Sphantom pad_char = *++fmt; 16083104Sphantom if (pad_char == '\0') 16183104Sphantom goto format_error; 16283104Sphantom continue; 16383104Sphantom case '^': /* not group currency */ 16483104Sphantom flags &= ~(NEED_GROUPING); 16583104Sphantom continue; 16683104Sphantom case '+': /* use locale defined signs */ 16783104Sphantom if (flags & SIGN_POSN_USED) 16883104Sphantom goto format_error; 16983104Sphantom flags |= (SIGN_POSN_USED|LOCALE_POSN); 17083104Sphantom continue; 17183104Sphantom case '(': /* enclose negatives with () */ 17283104Sphantom if (flags & SIGN_POSN_USED) 17383104Sphantom goto format_error; 17483104Sphantom flags |= (SIGN_POSN_USED|PARENTH_POSN); 17583104Sphantom continue; 17683104Sphantom case '!': /* suppress currency symbol */ 17783104Sphantom flags |= SUPRESS_CURR_SYMBOL; 17883104Sphantom continue; 17983104Sphantom case '-': /* alignment (left) */ 18083104Sphantom flags |= LEFT_JUSTIFY; 18183104Sphantom continue; 18283104Sphantom default: 18383104Sphantom break; 18483104Sphantom } 18583104Sphantom break; 18683104Sphantom } 18783104Sphantom 18883104Sphantom /* field Width */ 18983104Sphantom if (isdigit((unsigned char)*fmt)) { 19083104Sphantom GET_NUMBER(width); 19183104Sphantom /* Do we have enough space to put number with 19283104Sphantom * required width ? 19383104Sphantom */ 194178312Sru if ((unsigned int)width >= maxsize - (dst - s)) 19583104Sphantom goto e2big_error; 19683104Sphantom } 197104942Stjr 198104942Stjr /* Left precision */ 199104942Stjr if (*fmt == '#') { 200104942Stjr if (!isdigit((unsigned char)*++fmt)) 201104942Stjr goto format_error; 202104942Stjr GET_NUMBER(left_prec); 203178312Sru if ((unsigned int)left_prec >= maxsize - (dst - s)) 204178312Sru goto e2big_error; 205104942Stjr } 206104942Stjr 207104942Stjr /* Right precision */ 208104942Stjr if (*fmt == '.') { 209104942Stjr if (!isdigit((unsigned char)*++fmt)) 210104942Stjr goto format_error; 211104942Stjr GET_NUMBER(right_prec); 212178312Sru if ((unsigned int)right_prec >= maxsize - (dst - s) - 213178312Sru left_prec) 214178312Sru goto e2big_error; 215104942Stjr } 216104942Stjr 21783104Sphantom /* Conversion Characters */ 21883104Sphantom switch (*fmt++) { 21983104Sphantom case 'i': /* use internaltion currency format */ 22083104Sphantom flags |= USE_INTL_CURRENCY; 22183104Sphantom break; 22283104Sphantom case 'n': /* use national currency format */ 22383104Sphantom flags &= ~(USE_INTL_CURRENCY); 22483104Sphantom break; 22583104Sphantom default: /* required character is missing or 22683104Sphantom premature EOS */ 22783104Sphantom goto format_error; 22883104Sphantom } 22983104Sphantom 230178313Sru if (currency_symbol != NULL) 231178313Sru free(currency_symbol); 23283104Sphantom if (flags & USE_INTL_CURRENCY) { 23383104Sphantom currency_symbol = strdup(lc->int_curr_symbol); 23483104Sphantom if (currency_symbol != NULL) 23583104Sphantom space_char = *(currency_symbol+3); 23683559Smike } else 23783104Sphantom currency_symbol = strdup(lc->currency_symbol); 23883104Sphantom 23983104Sphantom if (currency_symbol == NULL) 24083104Sphantom goto end_error; /* ENOMEM. */ 24183104Sphantom 24283104Sphantom /* value itself */ 24383104Sphantom value = va_arg(ap, double); 24483104Sphantom 24583104Sphantom /* detect sign */ 24683104Sphantom if (value < 0) { 24783104Sphantom flags |= IS_NEGATIVE; 24883104Sphantom value = -value; 24983104Sphantom } 25083104Sphantom 25183104Sphantom /* fill left_prec with amount of padding chars */ 25283104Sphantom if (left_prec >= 0) { 25383104Sphantom pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 25483104Sphantom currency_symbol) - 25583104Sphantom __calc_left_pad(flags, currency_symbol); 25683104Sphantom if (pad_size < 0) 25783104Sphantom pad_size = 0; 25883104Sphantom } 25983104Sphantom 260178313Sru if (asciivalue != NULL) 261178313Sru free(asciivalue); 26283104Sphantom asciivalue = __format_grouped_double(value, &flags, 26383104Sphantom left_prec, right_prec, pad_char); 26483104Sphantom if (asciivalue == NULL) 26583104Sphantom goto end_error; /* errno already set */ 26683104Sphantom /* to ENOMEM by malloc() */ 26783104Sphantom 26883104Sphantom /* set some variables for later use */ 26983104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, 27083104Sphantom &sign_posn, &signstr); 27183104Sphantom 27283104Sphantom /* 27383104Sphantom * Description of some LC_MONETARY's values: 27483104Sphantom * 27583104Sphantom * p_cs_precedes & n_cs_precedes 27683104Sphantom * 27783104Sphantom * = 1 - $currency_symbol precedes the value 27883104Sphantom * for a monetary quantity with a non-negative value 27983104Sphantom * = 0 - symbol succeeds the value 28083104Sphantom * 28183104Sphantom * p_sep_by_space & n_sep_by_space 28283104Sphantom * 28383104Sphantom * = 0 - no space separates $currency_symbol 28483104Sphantom * from the value for a monetary quantity with a 28583104Sphantom * non-negative value 28683104Sphantom * = 1 - space separates the symbol from the value 28783104Sphantom * = 2 - space separates the symbol and the sign string, 28883104Sphantom * if adjacent. 28983104Sphantom * 29083104Sphantom * p_sign_posn & n_sign_posn 29183104Sphantom * 29283104Sphantom * = 0 - parentheses enclose the quantity and the 29383104Sphantom * $currency_symbol 29483104Sphantom * = 1 - the sign string precedes the quantity and the 29583104Sphantom * $currency_symbol 29683104Sphantom * = 2 - the sign string succeeds the quantity and the 29783104Sphantom * $currency_symbol 29883104Sphantom * = 3 - the sign string precedes the $currency_symbol 29983104Sphantom * = 4 - the sign string succeeds the $currency_symbol 30083104Sphantom * 30183104Sphantom */ 30283104Sphantom 30383104Sphantom tmpptr = dst; 30483104Sphantom 30583104Sphantom while (pad_size-- > 0) 30683104Sphantom PRINT(' '); 30783104Sphantom 308104943Stjr if (sign_posn == 0 && (flags & IS_NEGATIVE)) 309104943Stjr PRINT('('); 31083104Sphantom 31183104Sphantom if (cs_precedes == 1) { 31283104Sphantom if (sign_posn == 1 || sign_posn == 3) { 31383104Sphantom PRINTS(signstr); 31483104Sphantom if (sep_by_space == 2) /* XXX: ? */ 31583104Sphantom PRINT(' '); 31683104Sphantom } 31783104Sphantom 31883104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 31983104Sphantom PRINTS(currency_symbol); 32083104Sphantom 32183104Sphantom if (sign_posn == 4) { 32283104Sphantom if (sep_by_space == 2) 32383104Sphantom PRINT(space_char); 32483559Smike PRINTS(signstr); 32583104Sphantom if (sep_by_space == 1) 32683104Sphantom PRINT(' '); 32783559Smike } else if (sep_by_space == 1) 32883559Smike PRINT(space_char); 32983104Sphantom } 33083559Smike } else if (sign_posn == 1) 33183559Smike PRINTS(signstr); 33283104Sphantom 33383104Sphantom PRINTS(asciivalue); 33483104Sphantom 33583104Sphantom if (cs_precedes == 0) { 33683104Sphantom if (sign_posn == 3) { 33783104Sphantom if (sep_by_space == 1) 33883104Sphantom PRINT(' '); 33983104Sphantom PRINTS(signstr); 34083104Sphantom } 34183104Sphantom 34283104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 34383104Sphantom if ((sign_posn == 3 && sep_by_space == 2) 34483559Smike || (sep_by_space == 1 345104963Stjr && (sign_posn == 0 34683559Smike || sign_posn == 1 34783559Smike || sign_posn == 2 34883559Smike || sign_posn == 4))) 34983559Smike PRINT(space_char); 35083104Sphantom PRINTS(currency_symbol); /* XXX: len */ 35183104Sphantom if (sign_posn == 4) { 35283104Sphantom if (sep_by_space == 2) 35383104Sphantom PRINT(' '); 35483104Sphantom PRINTS(signstr); 35583104Sphantom } 35683104Sphantom } 35783104Sphantom } 35883104Sphantom 35983104Sphantom if (sign_posn == 2) { 36083104Sphantom if (sep_by_space == 2) 36183104Sphantom PRINT(' '); 36283104Sphantom PRINTS(signstr); 36383104Sphantom } 36483104Sphantom 36583104Sphantom if (sign_posn == 0 && (flags & IS_NEGATIVE)) 36683104Sphantom PRINT(')'); 36783104Sphantom 36883104Sphantom if (dst - tmpptr < width) { 36983104Sphantom if (flags & LEFT_JUSTIFY) { 370104963Stjr while (dst - tmpptr < width) 37183104Sphantom PRINT(' '); 37283104Sphantom } else { 37383104Sphantom pad_size = dst-tmpptr; 37483559Smike memmove(tmpptr + width-pad_size, tmpptr, 37583559Smike pad_size); 37683104Sphantom memset(tmpptr, ' ', width-pad_size); 37783104Sphantom dst += width-pad_size; 37883104Sphantom } 37983104Sphantom } 38083104Sphantom } 38183104Sphantom 38283104Sphantom PRINT('\0'); 38383104Sphantom va_end(ap); 384104963Stjr free(asciivalue); 385104963Stjr free(currency_symbol); 38683104Sphantom return (dst - s - 1); /* return size of put data except trailing '\0' */ 38783104Sphantom 38883104Sphantome2big_error: 38983104Sphantom errno = E2BIG; 39083104Sphantom goto end_error; 39183104Sphantom 39283104Sphantomformat_error: 39383104Sphantom errno = EINVAL; 39483104Sphantom 39583104Sphantomend_error: 396104946Stjr sverrno = errno; 39783104Sphantom if (asciivalue != NULL) 39883104Sphantom free(asciivalue); 39983104Sphantom if (currency_symbol != NULL) 40083104Sphantom free(currency_symbol); 401104946Stjr errno = sverrno; 40283104Sphantom va_end(ap); 40383104Sphantom return (-1); 40483104Sphantom} 40583104Sphantom 40683104Sphantomstatic void 40783104Sphantom__setup_vars(int flags, char *cs_precedes, char *sep_by_space, 40883104Sphantom char *sign_posn, char **signstr) { 40983104Sphantom 41083104Sphantom struct lconv *lc = localeconv(); 41183104Sphantom 412104944Stjr if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 413104944Stjr *cs_precedes = lc->int_n_cs_precedes; 414104944Stjr *sep_by_space = lc->int_n_sep_by_space; 415104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 416203734Scracauer *signstr = (lc->negative_sign[0] == '\0') ? "-" 417104944Stjr : lc->negative_sign; 418104944Stjr } else if (flags & USE_INTL_CURRENCY) { 419104944Stjr *cs_precedes = lc->int_p_cs_precedes; 420104944Stjr *sep_by_space = lc->int_p_sep_by_space; 421104944Stjr *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 422104944Stjr *signstr = lc->positive_sign; 423104944Stjr } else if (flags & IS_NEGATIVE) { 42483104Sphantom *cs_precedes = lc->n_cs_precedes; 42583104Sphantom *sep_by_space = lc->n_sep_by_space; 42683559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 427203734Scracauer *signstr = (lc->negative_sign[0] == '\0') ? "-" 42883559Smike : lc->negative_sign; 42983104Sphantom } else { 43083104Sphantom *cs_precedes = lc->p_cs_precedes; 43183104Sphantom *sep_by_space = lc->p_sep_by_space; 43283559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 43383104Sphantom *signstr = lc->positive_sign; 43483104Sphantom } 43583104Sphantom 43683104Sphantom /* Set defult values for unspecified information. */ 43783104Sphantom if (*cs_precedes != 0) 43883104Sphantom *cs_precedes = 1; 43983104Sphantom if (*sep_by_space == CHAR_MAX) 44083104Sphantom *sep_by_space = 0; 44183104Sphantom if (*sign_posn == CHAR_MAX) 44283104Sphantom *sign_posn = 0; 44383104Sphantom} 44483104Sphantom 44583104Sphantomstatic int 44683104Sphantom__calc_left_pad(int flags, char *cur_symb) { 44783104Sphantom 44883104Sphantom char cs_precedes, sep_by_space, sign_posn, *signstr; 44983104Sphantom int left_chars = 0; 45083104Sphantom 45183104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 45283104Sphantom 45383104Sphantom if (cs_precedes != 0) { 45483104Sphantom left_chars += strlen(cur_symb); 45583104Sphantom if (sep_by_space != 0) 45683104Sphantom left_chars++; 45783104Sphantom } 45883104Sphantom 45983104Sphantom switch (sign_posn) { 46083104Sphantom case 1: 46183104Sphantom left_chars += strlen(signstr); 46283104Sphantom break; 46383104Sphantom case 3: 46483104Sphantom case 4: 46583104Sphantom if (cs_precedes != 0) 46683559Smike left_chars += strlen(signstr); 46783104Sphantom } 46883559Smike return (left_chars); 46983104Sphantom} 47083104Sphantom 47183104Sphantomstatic int 47283104Sphantomget_groups(int size, char *grouping) { 47383104Sphantom 47483104Sphantom int chars = 0; 47583104Sphantom 47683104Sphantom if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 47783559Smike return (0); 47883104Sphantom 47983104Sphantom while (size > (int)*grouping) { 48083104Sphantom chars++; 48183104Sphantom size -= (int)*grouping++; 48283104Sphantom /* no more grouping ? */ 48383104Sphantom if (*grouping == CHAR_MAX) 48483104Sphantom break; 48583104Sphantom /* rest grouping with same value ? */ 48683104Sphantom if (*grouping == 0) { 48783104Sphantom chars += (size - 1) / *(grouping - 1); 48883104Sphantom break; 48983104Sphantom } 49083104Sphantom } 49183559Smike return (chars); 49283104Sphantom} 49383104Sphantom 49483104Sphantom/* convert double to ASCII */ 49583104Sphantomstatic char * 49683104Sphantom__format_grouped_double(double value, int *flags, 49783104Sphantom int left_prec, int right_prec, int pad_char) { 49883104Sphantom 49983104Sphantom char *rslt; 50083104Sphantom char *avalue; 50183104Sphantom int avalue_size; 50283104Sphantom char fmt[32]; 50383104Sphantom 50483104Sphantom size_t bufsize; 50583104Sphantom char *bufend; 50683104Sphantom 50783104Sphantom int padded; 50883104Sphantom 50983104Sphantom struct lconv *lc = localeconv(); 51083104Sphantom char *grouping; 51183104Sphantom char decimal_point; 51283104Sphantom char thousands_sep; 51383104Sphantom 51483104Sphantom int groups = 0; 51583104Sphantom 51683104Sphantom grouping = lc->mon_grouping; 51783104Sphantom decimal_point = *lc->mon_decimal_point; 518112427Sache if (decimal_point == '\0') 51983104Sphantom decimal_point = *lc->decimal_point; 52083104Sphantom thousands_sep = *lc->mon_thousands_sep; 52183559Smike if (thousands_sep == '\0') 52283104Sphantom thousands_sep = *lc->thousands_sep; 52383104Sphantom 52483104Sphantom /* fill left_prec with default value */ 52583104Sphantom if (left_prec == -1) 52683104Sphantom left_prec = 0; 52783104Sphantom 52883104Sphantom /* fill right_prec with default value */ 52983104Sphantom if (right_prec == -1) { 53083104Sphantom if (*flags & USE_INTL_CURRENCY) 53183104Sphantom right_prec = lc->int_frac_digits; 53283104Sphantom else 53383104Sphantom right_prec = lc->frac_digits; 53483104Sphantom 53583104Sphantom if (right_prec == CHAR_MAX) /* POSIX locale ? */ 53683104Sphantom right_prec = 2; 53783104Sphantom } 53883104Sphantom 53983104Sphantom if (*flags & NEED_GROUPING) 54083104Sphantom left_prec += get_groups(left_prec, grouping); 54183104Sphantom 54283104Sphantom /* convert to string */ 54383559Smike snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 54483559Smike right_prec); 54583104Sphantom avalue_size = asprintf(&avalue, fmt, value); 54683104Sphantom if (avalue_size < 0) 54783559Smike return (NULL); 54883104Sphantom 54983104Sphantom /* make sure that we've enough space for result string */ 55083104Sphantom bufsize = strlen(avalue)*2+1; 551178175Sdelphij rslt = calloc(1, bufsize); 55283104Sphantom if (rslt == NULL) { 55383104Sphantom free(avalue); 55483559Smike return (NULL); 55583104Sphantom } 55683104Sphantom bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 55783104Sphantom 55883104Sphantom /* skip spaces at beggining */ 55983104Sphantom padded = 0; 56083104Sphantom while (avalue[padded] == ' ') { 56183104Sphantom padded++; 56283104Sphantom avalue_size--; 56383104Sphantom } 56483104Sphantom 56583104Sphantom if (right_prec > 0) { 56683104Sphantom bufend -= right_prec; 56783559Smike memcpy(bufend, avalue + avalue_size+padded-right_prec, 56883559Smike right_prec); 56983104Sphantom *--bufend = decimal_point; 57083104Sphantom avalue_size -= (right_prec + 1); 57183104Sphantom } 57283104Sphantom 57383104Sphantom if ((*flags & NEED_GROUPING) && 57483559Smike thousands_sep != '\0' && /* XXX: need investigation */ 57583559Smike *grouping != CHAR_MAX && 57683559Smike *grouping > 0) { 57783104Sphantom while (avalue_size > (int)*grouping) { 57883104Sphantom GRPCPY(*grouping); 57983104Sphantom GRPSEP; 58083104Sphantom grouping++; 58183104Sphantom 58283104Sphantom /* no more grouping ? */ 58383104Sphantom if (*grouping == CHAR_MAX) 58483104Sphantom break; 58583104Sphantom 58683104Sphantom /* rest grouping with same value ? */ 58783104Sphantom if (*grouping == 0) { 58883104Sphantom grouping--; 58983104Sphantom while (avalue_size > *grouping) { 59083104Sphantom GRPCPY(*grouping); 59183104Sphantom GRPSEP; 59283104Sphantom } 59383104Sphantom } 59483104Sphantom } 59583104Sphantom if (avalue_size != 0) 59683104Sphantom GRPCPY(avalue_size); 59783104Sphantom padded -= groups; 59883104Sphantom 59983104Sphantom } else { 60083104Sphantom bufend -= avalue_size; 60183104Sphantom memcpy(bufend, avalue+padded, avalue_size); 60283104Sphantom if (right_prec == 0) 60383104Sphantom padded--; /* decrease assumed $decimal_point */ 60483104Sphantom } 60583104Sphantom 60683104Sphantom /* do padding with pad_char */ 60783104Sphantom if (padded > 0) { 60883104Sphantom bufend -= padded; 60983104Sphantom memset(bufend, pad_char, padded); 61083104Sphantom } 61183104Sphantom 612104963Stjr bufsize = bufsize - (bufend - rslt) + 1; 61383104Sphantom memmove(rslt, bufend, bufsize); 61483104Sphantom free(avalue); 61583559Smike return (rslt); 61683104Sphantom} 617