strfmon.c revision 92986
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 92986 2002-03-22 21:53:29Z obrien $"); 3083104Sphantom 3183104Sphantom#include <sys/types.h> 3283104Sphantom#include <ctype.h> 3383104Sphantom#include <errno.h> 3483104Sphantom#include <limits.h> 3583104Sphantom#include <locale.h> 3683104Sphantom#if __STDC__ 3783104Sphantom#include <stdarg.h> 3883104Sphantom#else 3983104Sphantom#include <varargs.h> 4083104Sphantom#endif 4183104Sphantom#include <stdio.h> 4283104Sphantom#include <stdlib.h> 4383104Sphantom#include <string.h> 4483104Sphantom 4583104Sphantom/* internal flags */ 4683104Sphantom#define NEED_GROUPING 0x01 /* print digits grouped (default) */ 4783104Sphantom#define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 4883104Sphantom#define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 4983104Sphantom#define PARENTH_POSN 0x08 /* enclose negative amount in () */ 5083104Sphantom#define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 5183104Sphantom#define LEFT_JUSTIFY 0x20 /* left justify */ 5283104Sphantom#define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 5383104Sphantom#define IS_NEGATIVE 0x80 /* is argument value negative ? */ 5483104Sphantom 5583104Sphantom/* internal macros */ 5683559Smike#define PRINT(CH) do { \ 5783104Sphantom if (dst >= s + maxsize) \ 5883104Sphantom goto e2big_error; \ 5983104Sphantom *dst++ = CH; \ 6083559Smike} while (0) 6183104Sphantom 6283559Smike#define PRINTS(STR) do { \ 6383104Sphantom char *tmps = STR; \ 6483104Sphantom while (*tmps != '\0') \ 6583104Sphantom PRINT(*tmps++); \ 6683559Smike} while (0) 6783104Sphantom 6883559Smike#define GET_NUMBER(VAR) do { \ 6983104Sphantom VAR = 0; \ 7083104Sphantom while (isdigit((unsigned char)*fmt)) { \ 7183104Sphantom VAR *= 10; \ 7283104Sphantom VAR += *fmt - '0'; \ 7383104Sphantom fmt++; \ 7483104Sphantom } \ 7583559Smike} while (0) 7683104Sphantom 7783559Smike#define GRPCPY(howmany) do { \ 7883559Smike int i = howmany; \ 7983559Smike while (i-- > 0) { \ 8083559Smike avalue_size--; \ 8183559Smike *--bufend = *(avalue+avalue_size+padded); \ 8283559Smike } \ 8383559Smike} while (0) 8483559Smike 8583559Smike#define GRPSEP do { \ 8683559Smike *--bufend = thousands_sep; \ 8783559Smike groups++; \ 8883559Smike} while (0) 8983559Smike 9083104Sphantomstatic void __setup_vars(int, char *, char *, char *, char **); 9183104Sphantomstatic int __calc_left_pad(int, char *); 9283104Sphantomstatic char *__format_grouped_double(double, int *, int, int, int); 9383104Sphantom 9483104Sphantom#if __STDC__ 9583104Sphantomssize_t 9683104Sphantomstrfmon(char *s, size_t maxsize, const char *format, ...) 9783104Sphantom#else 9883104Sphantomssize_t 9983104Sphantomstrfmon(s, maxsize, format, va_alist) 10083104Sphantom char *s; 10183104Sphantom size_t maxsize; 10283104Sphantom const char *format; 10383104Sphantom va_dcl 10483104Sphantom#endif 10583104Sphantom{ 10683104Sphantom va_list ap; 10783104Sphantom char *dst; /* output destination pointer */ 10883104Sphantom const char *fmt; /* current format poistion pointer */ 10983104Sphantom struct lconv *lc; /* pointer to lconv structure */ 11083104Sphantom char *asciivalue; /* formatted double pointer */ 11183104Sphantom 11283104Sphantom int flags; /* formatting options */ 11383104Sphantom int pad_char; /* padding character */ 11483104Sphantom int pad_size; /* pad size */ 11583104Sphantom int width; /* field width */ 11683104Sphantom int left_prec; /* left precision */ 11783104Sphantom int right_prec; /* right precision */ 11883104Sphantom double value; /* just value */ 11983104Sphantom char space_char = ' '; /* space after currency */ 12083104Sphantom 12183104Sphantom char cs_precedes, /* values gathered from struct lconv */ 12283104Sphantom sep_by_space, 12383104Sphantom sign_posn, 12483104Sphantom *signstr, 12583104Sphantom *currency_symbol; 12683104Sphantom 12783104Sphantom char *tmpptr; /* temporary vars */ 12883104Sphantom int *ntmp; 12983104Sphantom 13083104Sphantom#if __STDC__ 13183104Sphantom va_start(ap, format); 13283104Sphantom#else 13383104Sphantom va_start(ap); 13483104Sphantom#endif 13583104Sphantom 13683104Sphantom lc = localeconv(); 13783104Sphantom dst = s; 13883104Sphantom fmt = format; 13983104Sphantom asciivalue = NULL; 14083104Sphantom currency_symbol = NULL; 14183104Sphantom pad_size = 0; 14283104Sphantom 14383104Sphantom while (*fmt) { 14483104Sphantom /* pass nonformating characters AS IS */ 14583559Smike if (*fmt != '%') 14683104Sphantom goto literal; 14783104Sphantom 14883104Sphantom /* '%' found ! */ 14983104Sphantom 15083104Sphantom /* "%%" mean just '%' */ 15183104Sphantom if (*(fmt+1) == '%') { 15283104Sphantom fmt++; 15383104Sphantom literal: 15483104Sphantom PRINT(*fmt++); 15583104Sphantom continue; 15683104Sphantom } 15783104Sphantom 15883104Sphantom /* set up initial values */ 15983104Sphantom flags = (NEED_GROUPING|LOCALE_POSN); 16083104Sphantom pad_char = ' '; /* padding character is "space" */ 16183104Sphantom left_prec = -1; /* no left precision specified */ 16283104Sphantom right_prec = -1; /* no right precision specified */ 16383104Sphantom width = -1; /* no width specified */ 16483104Sphantom value = 0; /* we have no value to print now */ 16583104Sphantom 16683104Sphantom /* Flags */ 16783104Sphantom while (1) { 16883104Sphantom switch (*++fmt) { 16983104Sphantom case '=': /* fill character */ 17083104Sphantom pad_char = *++fmt; 17183104Sphantom if (pad_char == '\0') 17283104Sphantom goto format_error; 17383104Sphantom continue; 17483104Sphantom case '^': /* not group currency */ 17583104Sphantom flags &= ~(NEED_GROUPING); 17683104Sphantom continue; 17783104Sphantom case '+': /* use locale defined signs */ 17883104Sphantom if (flags & SIGN_POSN_USED) 17983104Sphantom goto format_error; 18083104Sphantom flags |= (SIGN_POSN_USED|LOCALE_POSN); 18183104Sphantom continue; 18283104Sphantom case '(': /* enclose negatives with () */ 18383104Sphantom if (flags & SIGN_POSN_USED) 18483104Sphantom goto format_error; 18583104Sphantom flags |= (SIGN_POSN_USED|PARENTH_POSN); 18683104Sphantom continue; 18783104Sphantom case '!': /* suppress currency symbol */ 18883104Sphantom flags |= SUPRESS_CURR_SYMBOL; 18983104Sphantom continue; 19083104Sphantom case '-': /* alignment (left) */ 19183104Sphantom flags |= LEFT_JUSTIFY; 19283104Sphantom continue; 19383104Sphantom case '#': /* left || right precision */ 19483104Sphantom case '.': 19583104Sphantom if (*fmt == '#') 19683104Sphantom ntmp = &left_prec; 19783104Sphantom else 19883104Sphantom ntmp = &right_prec; 19983104Sphantom 20083104Sphantom if (!isdigit((unsigned char)*++fmt)) 20183104Sphantom goto format_error; 20283104Sphantom GET_NUMBER(*ntmp); 20383104Sphantom fmt--; 20483104Sphantom continue; 20583104Sphantom default: 20683104Sphantom break; 20783104Sphantom } 20883104Sphantom break; 20983104Sphantom } 21083104Sphantom 21183104Sphantom /* field Width */ 21283104Sphantom if (isdigit((unsigned char)*fmt)) { 21383104Sphantom GET_NUMBER(width); 21483104Sphantom /* Do we have enough space to put number with 21583104Sphantom * required width ? 21683104Sphantom */ 21783104Sphantom if (dst + width >= s + maxsize) 21883104Sphantom goto e2big_error; 21983104Sphantom } 22083104Sphantom 22183104Sphantom /* Conversion Characters */ 22283104Sphantom switch (*fmt++) { 22383104Sphantom case 'i': /* use internaltion currency format */ 22483104Sphantom flags |= USE_INTL_CURRENCY; 22583104Sphantom break; 22683104Sphantom case 'n': /* use national currency format */ 22783104Sphantom flags &= ~(USE_INTL_CURRENCY); 22883104Sphantom break; 22983104Sphantom default: /* required character is missing or 23083104Sphantom premature EOS */ 23183104Sphantom goto format_error; 23283104Sphantom } 23383104Sphantom 23483104Sphantom if (flags & USE_INTL_CURRENCY) { 23583104Sphantom currency_symbol = strdup(lc->int_curr_symbol); 23683104Sphantom if (currency_symbol != NULL) 23783104Sphantom space_char = *(currency_symbol+3); 23883559Smike } else 23983104Sphantom currency_symbol = strdup(lc->currency_symbol); 24083104Sphantom 24183104Sphantom if (currency_symbol == NULL) 24283104Sphantom goto end_error; /* ENOMEM. */ 24383104Sphantom 24483104Sphantom /* value itself */ 24583104Sphantom value = va_arg(ap, double); 24683104Sphantom 24783104Sphantom /* detect sign */ 24883104Sphantom if (value < 0) { 24983104Sphantom flags |= IS_NEGATIVE; 25083104Sphantom value = -value; 25183104Sphantom } 25283104Sphantom 25383104Sphantom /* fill left_prec with amount of padding chars */ 25483104Sphantom if (left_prec >= 0) { 25583104Sphantom pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 25683104Sphantom currency_symbol) - 25783104Sphantom __calc_left_pad(flags, currency_symbol); 25883104Sphantom if (pad_size < 0) 25983104Sphantom pad_size = 0; 26083104Sphantom } 26183104Sphantom 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 30883104Sphantom if (sign_posn == 0) { 30983559Smike if (flags & IS_NEGATIVE) 31083104Sphantom PRINT('('); 31183559Smike else 31283104Sphantom PRINT(' '); 31383104Sphantom } 31483104Sphantom 31583104Sphantom if (cs_precedes == 1) { 31683104Sphantom if (sign_posn == 1 || sign_posn == 3) { 31783104Sphantom PRINTS(signstr); 31883104Sphantom if (sep_by_space == 2) /* XXX: ? */ 31983104Sphantom PRINT(' '); 32083104Sphantom } 32183104Sphantom 32283104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 32383104Sphantom PRINTS(currency_symbol); 32483104Sphantom 32583104Sphantom if (sign_posn == 4) { 32683104Sphantom if (sep_by_space == 2) 32783104Sphantom PRINT(space_char); 32883559Smike PRINTS(signstr); 32983104Sphantom if (sep_by_space == 1) 33083104Sphantom PRINT(' '); 33183559Smike } else if (sep_by_space == 1) 33283559Smike PRINT(space_char); 33383104Sphantom } 33483559Smike } else if (sign_posn == 1) 33583559Smike PRINTS(signstr); 33683104Sphantom 33783104Sphantom PRINTS(asciivalue); 33883104Sphantom 33983104Sphantom if (cs_precedes == 0) { 34083104Sphantom if (sign_posn == 3) { 34183104Sphantom if (sep_by_space == 1) 34283104Sphantom PRINT(' '); 34383104Sphantom PRINTS(signstr); 34483104Sphantom } 34583104Sphantom 34683104Sphantom if (!(flags & SUPRESS_CURR_SYMBOL)) { 34783104Sphantom if ((sign_posn == 3 && sep_by_space == 2) 34883559Smike || (sep_by_space == 1 34983559Smike && (sign_posn = 0 35083559Smike || sign_posn == 1 35183559Smike || sign_posn == 2 35283559Smike || sign_posn == 4))) 35383559Smike PRINT(space_char); 35483104Sphantom PRINTS(currency_symbol); /* XXX: len */ 35583104Sphantom if (sign_posn == 4) { 35683104Sphantom if (sep_by_space == 2) 35783104Sphantom PRINT(' '); 35883104Sphantom PRINTS(signstr); 35983104Sphantom } 36083104Sphantom } 36183104Sphantom } 36283104Sphantom 36383104Sphantom if (sign_posn == 2) { 36483104Sphantom if (sep_by_space == 2) 36583104Sphantom PRINT(' '); 36683104Sphantom PRINTS(signstr); 36783104Sphantom } 36883104Sphantom 36983104Sphantom if (sign_posn == 0 && (flags & IS_NEGATIVE)) 37083104Sphantom PRINT(')'); 37183104Sphantom 37283104Sphantom if (dst - tmpptr < width) { 37383104Sphantom if (flags & LEFT_JUSTIFY) { 37483104Sphantom while (dst - tmpptr <= width) 37583104Sphantom PRINT(' '); 37683104Sphantom } else { 37783104Sphantom pad_size = dst-tmpptr; 37883559Smike memmove(tmpptr + width-pad_size, tmpptr, 37983559Smike pad_size); 38083104Sphantom memset(tmpptr, ' ', width-pad_size); 38183104Sphantom dst += width-pad_size; 38283104Sphantom } 38383104Sphantom } 38483104Sphantom } 38583104Sphantom 38683104Sphantom PRINT('\0'); 38783104Sphantom va_end(ap); 38883104Sphantom return (dst - s - 1); /* return size of put data except trailing '\0' */ 38983104Sphantom 39083104Sphantome2big_error: 39183104Sphantom errno = E2BIG; 39283104Sphantom goto end_error; 39383104Sphantom 39483104Sphantomformat_error: 39583104Sphantom errno = EINVAL; 39683104Sphantom 39783104Sphantomend_error: 39883104Sphantom if (asciivalue != NULL) 39983104Sphantom free(asciivalue); 40083104Sphantom if (currency_symbol != NULL) 40183104Sphantom free(currency_symbol); 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 41283104Sphantom if (flags & IS_NEGATIVE) { 41383104Sphantom *cs_precedes = lc->n_cs_precedes; 41483104Sphantom *sep_by_space = lc->n_sep_by_space; 41583559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 41683559Smike *signstr = (lc->negative_sign == '\0') ? "-" 41783559Smike : lc->negative_sign; 41883104Sphantom } else { 41983104Sphantom *cs_precedes = lc->p_cs_precedes; 42083104Sphantom *sep_by_space = lc->p_sep_by_space; 42183559Smike *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 42283104Sphantom *signstr = lc->positive_sign; 42383104Sphantom } 42483104Sphantom 42583104Sphantom /* Set defult values for unspecified information. */ 42683104Sphantom if (*cs_precedes != 0) 42783104Sphantom *cs_precedes = 1; 42883104Sphantom if (*sep_by_space == CHAR_MAX) 42983104Sphantom *sep_by_space = 0; 43083104Sphantom if (*sign_posn == CHAR_MAX) 43183104Sphantom *sign_posn = 0; 43283104Sphantom} 43383104Sphantom 43483104Sphantomstatic int 43583104Sphantom__calc_left_pad(int flags, char *cur_symb) { 43683104Sphantom 43783104Sphantom char cs_precedes, sep_by_space, sign_posn, *signstr; 43883104Sphantom int left_chars = 0; 43983104Sphantom 44083104Sphantom __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 44183104Sphantom 44283104Sphantom if (cs_precedes != 0) { 44383104Sphantom left_chars += strlen(cur_symb); 44483104Sphantom if (sep_by_space != 0) 44583104Sphantom left_chars++; 44683104Sphantom } 44783104Sphantom 44883104Sphantom switch (sign_posn) { 44983104Sphantom case 1: 45083104Sphantom left_chars += strlen(signstr); 45183104Sphantom break; 45283104Sphantom case 3: 45383104Sphantom case 4: 45483104Sphantom if (cs_precedes != 0) 45583559Smike left_chars += strlen(signstr); 45683104Sphantom } 45783559Smike return (left_chars); 45883104Sphantom} 45983104Sphantom 46083104Sphantomstatic int 46183104Sphantomget_groups(int size, char *grouping) { 46283104Sphantom 46383104Sphantom int chars = 0; 46483104Sphantom 46583104Sphantom if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 46683559Smike return (0); 46783104Sphantom 46883104Sphantom while (size > (int)*grouping) { 46983104Sphantom chars++; 47083104Sphantom size -= (int)*grouping++; 47183104Sphantom /* no more grouping ? */ 47283104Sphantom if (*grouping == CHAR_MAX) 47383104Sphantom break; 47483104Sphantom /* rest grouping with same value ? */ 47583104Sphantom if (*grouping == 0) { 47683104Sphantom chars += (size - 1) / *(grouping - 1); 47783104Sphantom break; 47883104Sphantom } 47983104Sphantom } 48083559Smike return (chars); 48183104Sphantom} 48283104Sphantom 48383104Sphantom/* convert double to ASCII */ 48483104Sphantomstatic char * 48583104Sphantom__format_grouped_double(double value, int *flags, 48683104Sphantom int left_prec, int right_prec, int pad_char) { 48783104Sphantom 48883104Sphantom char *rslt; 48983104Sphantom char *avalue; 49083104Sphantom int avalue_size; 49183104Sphantom char fmt[32]; 49283104Sphantom 49383104Sphantom size_t bufsize; 49483104Sphantom char *bufend; 49583104Sphantom 49683104Sphantom int padded; 49783104Sphantom 49883104Sphantom struct lconv *lc = localeconv(); 49983104Sphantom char *grouping; 50083104Sphantom char decimal_point; 50183104Sphantom char thousands_sep; 50283104Sphantom 50383104Sphantom int groups = 0; 50483104Sphantom 50583104Sphantom grouping = lc->mon_grouping; 50683104Sphantom decimal_point = *lc->mon_decimal_point; 50783104Sphantom if (decimal_point == '\0') { 50883104Sphantom decimal_point = *lc->decimal_point; 50983104Sphantom if (decimal_point == '\0') 51083104Sphantom decimal_point = '.'; 51183104Sphantom } 51283104Sphantom thousands_sep = *lc->mon_thousands_sep; 51383559Smike if (thousands_sep == '\0') 51483104Sphantom thousands_sep = *lc->thousands_sep; 51583104Sphantom 51683104Sphantom /* fill left_prec with default value */ 51783104Sphantom if (left_prec == -1) 51883104Sphantom left_prec = 0; 51983104Sphantom 52083104Sphantom /* fill right_prec with default value */ 52183104Sphantom if (right_prec == -1) { 52283104Sphantom if (*flags & USE_INTL_CURRENCY) 52383104Sphantom right_prec = lc->int_frac_digits; 52483104Sphantom else 52583104Sphantom right_prec = lc->frac_digits; 52683104Sphantom 52783104Sphantom if (right_prec == CHAR_MAX) /* POSIX locale ? */ 52883104Sphantom right_prec = 2; 52983104Sphantom } 53083104Sphantom 53183104Sphantom if (*flags & NEED_GROUPING) 53283104Sphantom left_prec += get_groups(left_prec, grouping); 53383104Sphantom 53483104Sphantom /* convert to string */ 53583559Smike snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 53683559Smike right_prec); 53783104Sphantom avalue_size = asprintf(&avalue, fmt, value); 53883104Sphantom if (avalue_size < 0) 53983559Smike return (NULL); 54083104Sphantom 54183104Sphantom /* make sure that we've enough space for result string */ 54283104Sphantom bufsize = strlen(avalue)*2+1; 54383104Sphantom rslt = malloc(bufsize); 54483104Sphantom if (rslt == NULL) { 54583104Sphantom free(avalue); 54683559Smike return (NULL); 54783104Sphantom } 54883104Sphantom memset(rslt, 0, bufsize); 54983104Sphantom bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 55083104Sphantom 55183104Sphantom /* skip spaces at beggining */ 55283104Sphantom padded = 0; 55383104Sphantom while (avalue[padded] == ' ') { 55483104Sphantom padded++; 55583104Sphantom avalue_size--; 55683104Sphantom } 55783104Sphantom 55883104Sphantom if (right_prec > 0) { 55983104Sphantom bufend -= right_prec; 56083559Smike memcpy(bufend, avalue + avalue_size+padded-right_prec, 56183559Smike right_prec); 56283104Sphantom *--bufend = decimal_point; 56383104Sphantom avalue_size -= (right_prec + 1); 56483104Sphantom } 56583104Sphantom 56683104Sphantom if ((*flags & NEED_GROUPING) && 56783559Smike thousands_sep != '\0' && /* XXX: need investigation */ 56883559Smike *grouping != CHAR_MAX && 56983559Smike *grouping > 0) { 57083104Sphantom while (avalue_size > (int)*grouping) { 57183104Sphantom GRPCPY(*grouping); 57283104Sphantom GRPSEP; 57383104Sphantom grouping++; 57483104Sphantom 57583104Sphantom /* no more grouping ? */ 57683104Sphantom if (*grouping == CHAR_MAX) 57783104Sphantom break; 57883104Sphantom 57983104Sphantom /* rest grouping with same value ? */ 58083104Sphantom if (*grouping == 0) { 58183104Sphantom grouping--; 58283104Sphantom while (avalue_size > *grouping) { 58383104Sphantom GRPCPY(*grouping); 58483104Sphantom GRPSEP; 58583104Sphantom } 58683104Sphantom } 58783104Sphantom } 58883104Sphantom if (avalue_size != 0) 58983104Sphantom GRPCPY(avalue_size); 59083104Sphantom padded -= groups; 59183104Sphantom 59283104Sphantom } else { 59383104Sphantom bufend -= avalue_size; 59483104Sphantom memcpy(bufend, avalue+padded, avalue_size); 59583104Sphantom if (right_prec == 0) 59683104Sphantom padded--; /* decrease assumed $decimal_point */ 59783104Sphantom } 59883104Sphantom 59983104Sphantom /* do padding with pad_char */ 60083104Sphantom if (padded > 0) { 60183104Sphantom bufend -= padded; 60283104Sphantom memset(bufend, pad_char, padded); 60383104Sphantom } 60483104Sphantom 60583559Smike bufsize = bufsize - (rslt - bufend); 60683104Sphantom memmove(rslt, bufend, bufsize); 60783104Sphantom free(avalue); 60883559Smike return (rslt); 60983104Sphantom} 610