19d430a59SAlexey Zelkin /*- 252d6b430SAlexey Zelkin * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> 39d430a59SAlexey Zelkin * All rights reserved. 49d430a59SAlexey Zelkin * 59d430a59SAlexey Zelkin * Redistribution and use in source and binary forms, with or without 69d430a59SAlexey Zelkin * modification, are permitted provided that the following conditions 79d430a59SAlexey Zelkin * are met: 89d430a59SAlexey Zelkin * 1. Redistributions of source code must retain the above copyright 99d430a59SAlexey Zelkin * notice, this list of conditions and the following disclaimer. 109d430a59SAlexey Zelkin * 2. Redistributions in binary form must reproduce the above copyright 119d430a59SAlexey Zelkin * notice, this list of conditions and the following disclaimer in the 129d430a59SAlexey Zelkin * documentation and/or other materials provided with the distribution. 139d430a59SAlexey Zelkin * 149d430a59SAlexey Zelkin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 159d430a59SAlexey Zelkin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 169d430a59SAlexey Zelkin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 179d430a59SAlexey Zelkin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 189d430a59SAlexey Zelkin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 199d430a59SAlexey Zelkin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 209d430a59SAlexey Zelkin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 219d430a59SAlexey Zelkin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 229d430a59SAlexey Zelkin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 239d430a59SAlexey Zelkin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 249d430a59SAlexey Zelkin * SUCH DAMAGE. 259d430a59SAlexey Zelkin * 269d430a59SAlexey Zelkin */ 279d430a59SAlexey Zelkin 28333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 29333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 309d430a59SAlexey Zelkin 319d430a59SAlexey Zelkin #include <sys/types.h> 329d430a59SAlexey Zelkin #include <ctype.h> 339d430a59SAlexey Zelkin #include <errno.h> 349d430a59SAlexey Zelkin #include <limits.h> 359d430a59SAlexey Zelkin #include <locale.h> 369d430a59SAlexey Zelkin #include <stdarg.h> 379d430a59SAlexey Zelkin #include <stdio.h> 389d430a59SAlexey Zelkin #include <stdlib.h> 399d430a59SAlexey Zelkin #include <string.h> 409d430a59SAlexey Zelkin 419d430a59SAlexey Zelkin /* internal flags */ 429d430a59SAlexey Zelkin #define NEED_GROUPING 0x01 /* print digits grouped (default) */ 439d430a59SAlexey Zelkin #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 449d430a59SAlexey Zelkin #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 459d430a59SAlexey Zelkin #define PARENTH_POSN 0x08 /* enclose negative amount in () */ 469d430a59SAlexey Zelkin #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 479d430a59SAlexey Zelkin #define LEFT_JUSTIFY 0x20 /* left justify */ 489d430a59SAlexey Zelkin #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 499d430a59SAlexey Zelkin #define IS_NEGATIVE 0x80 /* is argument value negative ? */ 509d430a59SAlexey Zelkin 519d430a59SAlexey Zelkin /* internal macros */ 52a5aecc77SMike Barcroft #define PRINT(CH) do { \ 539d430a59SAlexey Zelkin if (dst >= s + maxsize) \ 549d430a59SAlexey Zelkin goto e2big_error; \ 559d430a59SAlexey Zelkin *dst++ = CH; \ 56a5aecc77SMike Barcroft } while (0) 579d430a59SAlexey Zelkin 58a5aecc77SMike Barcroft #define PRINTS(STR) do { \ 599d430a59SAlexey Zelkin char *tmps = STR; \ 609d430a59SAlexey Zelkin while (*tmps != '\0') \ 619d430a59SAlexey Zelkin PRINT(*tmps++); \ 62a5aecc77SMike Barcroft } while (0) 639d430a59SAlexey Zelkin 64a5aecc77SMike Barcroft #define GET_NUMBER(VAR) do { \ 659d430a59SAlexey Zelkin VAR = 0; \ 669d430a59SAlexey Zelkin while (isdigit((unsigned char)*fmt)) { \ 679d430a59SAlexey Zelkin VAR *= 10; \ 689d430a59SAlexey Zelkin VAR += *fmt - '0'; \ 699d430a59SAlexey Zelkin fmt++; \ 709d430a59SAlexey Zelkin } \ 71a5aecc77SMike Barcroft } while (0) 72a5aecc77SMike Barcroft 73a5aecc77SMike Barcroft #define GRPCPY(howmany) do { \ 74a5aecc77SMike Barcroft int i = howmany; \ 75a5aecc77SMike Barcroft while (i-- > 0) { \ 76a5aecc77SMike Barcroft avalue_size--; \ 77a5aecc77SMike Barcroft *--bufend = *(avalue+avalue_size+padded); \ 78a5aecc77SMike Barcroft } \ 79a5aecc77SMike Barcroft } while (0) 80a5aecc77SMike Barcroft 81a5aecc77SMike Barcroft #define GRPSEP do { \ 82a5aecc77SMike Barcroft *--bufend = thousands_sep; \ 83a5aecc77SMike Barcroft groups++; \ 84a5aecc77SMike Barcroft } while (0) 859d430a59SAlexey Zelkin 869d430a59SAlexey Zelkin static void __setup_vars(int, char *, char *, char *, char **); 879d430a59SAlexey Zelkin static int __calc_left_pad(int, char *); 889d430a59SAlexey Zelkin static char *__format_grouped_double(double, int *, int, int, int); 899d430a59SAlexey Zelkin 909d430a59SAlexey Zelkin ssize_t 91883738f2SMike Barcroft strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 92883738f2SMike Barcroft ...) 939d430a59SAlexey Zelkin { 949d430a59SAlexey Zelkin va_list ap; 959d430a59SAlexey Zelkin char *dst; /* output destination pointer */ 969d430a59SAlexey Zelkin const char *fmt; /* current format poistion pointer */ 979d430a59SAlexey Zelkin struct lconv *lc; /* pointer to lconv structure */ 989d430a59SAlexey Zelkin char *asciivalue; /* formatted double pointer */ 999d430a59SAlexey Zelkin 1009d430a59SAlexey Zelkin int flags; /* formatting options */ 1019d430a59SAlexey Zelkin int pad_char; /* padding character */ 1029d430a59SAlexey Zelkin int pad_size; /* pad size */ 1039d430a59SAlexey Zelkin int width; /* field width */ 1049d430a59SAlexey Zelkin int left_prec; /* left precision */ 1059d430a59SAlexey Zelkin int right_prec; /* right precision */ 1069d430a59SAlexey Zelkin double value; /* just value */ 1079d430a59SAlexey Zelkin char space_char = ' '; /* space after currency */ 1089d430a59SAlexey Zelkin 1099d430a59SAlexey Zelkin char cs_precedes, /* values gathered from struct lconv */ 1109d430a59SAlexey Zelkin sep_by_space, 1119d430a59SAlexey Zelkin sign_posn, 1129d430a59SAlexey Zelkin *signstr, 1139d430a59SAlexey Zelkin *currency_symbol; 1149d430a59SAlexey Zelkin 1159d430a59SAlexey Zelkin char *tmpptr; /* temporary vars */ 1169d430a59SAlexey Zelkin int *ntmp; 1179d430a59SAlexey Zelkin 1189d430a59SAlexey Zelkin va_start(ap, format); 1199d430a59SAlexey Zelkin 1209d430a59SAlexey Zelkin lc = localeconv(); 1219d430a59SAlexey Zelkin dst = s; 1229d430a59SAlexey Zelkin fmt = format; 1239d430a59SAlexey Zelkin asciivalue = NULL; 1249d430a59SAlexey Zelkin currency_symbol = NULL; 1259d430a59SAlexey Zelkin pad_size = 0; 1269d430a59SAlexey Zelkin 1279d430a59SAlexey Zelkin while (*fmt) { 1289d430a59SAlexey Zelkin /* pass nonformating characters AS IS */ 129a5aecc77SMike Barcroft if (*fmt != '%') 1309d430a59SAlexey Zelkin goto literal; 1319d430a59SAlexey Zelkin 1329d430a59SAlexey Zelkin /* '%' found ! */ 1339d430a59SAlexey Zelkin 1349d430a59SAlexey Zelkin /* "%%" mean just '%' */ 1359d430a59SAlexey Zelkin if (*(fmt+1) == '%') { 1369d430a59SAlexey Zelkin fmt++; 1379d430a59SAlexey Zelkin literal: 1389d430a59SAlexey Zelkin PRINT(*fmt++); 1399d430a59SAlexey Zelkin continue; 1409d430a59SAlexey Zelkin } 1419d430a59SAlexey Zelkin 1429d430a59SAlexey Zelkin /* set up initial values */ 1439d430a59SAlexey Zelkin flags = (NEED_GROUPING|LOCALE_POSN); 1449d430a59SAlexey Zelkin pad_char = ' '; /* padding character is "space" */ 1459d430a59SAlexey Zelkin left_prec = -1; /* no left precision specified */ 1469d430a59SAlexey Zelkin right_prec = -1; /* no right precision specified */ 1479d430a59SAlexey Zelkin width = -1; /* no width specified */ 1489d430a59SAlexey Zelkin value = 0; /* we have no value to print now */ 1499d430a59SAlexey Zelkin 1509d430a59SAlexey Zelkin /* Flags */ 1519d430a59SAlexey Zelkin while (1) { 1529d430a59SAlexey Zelkin switch (*++fmt) { 1539d430a59SAlexey Zelkin case '=': /* fill character */ 1549d430a59SAlexey Zelkin pad_char = *++fmt; 1559d430a59SAlexey Zelkin if (pad_char == '\0') 1569d430a59SAlexey Zelkin goto format_error; 1579d430a59SAlexey Zelkin continue; 1589d430a59SAlexey Zelkin case '^': /* not group currency */ 1599d430a59SAlexey Zelkin flags &= ~(NEED_GROUPING); 1609d430a59SAlexey Zelkin continue; 1619d430a59SAlexey Zelkin case '+': /* use locale defined signs */ 1629d430a59SAlexey Zelkin if (flags & SIGN_POSN_USED) 1639d430a59SAlexey Zelkin goto format_error; 1649d430a59SAlexey Zelkin flags |= (SIGN_POSN_USED|LOCALE_POSN); 1659d430a59SAlexey Zelkin continue; 1669d430a59SAlexey Zelkin case '(': /* enclose negatives with () */ 1679d430a59SAlexey Zelkin if (flags & SIGN_POSN_USED) 1689d430a59SAlexey Zelkin goto format_error; 1699d430a59SAlexey Zelkin flags |= (SIGN_POSN_USED|PARENTH_POSN); 1709d430a59SAlexey Zelkin continue; 1719d430a59SAlexey Zelkin case '!': /* suppress currency symbol */ 1729d430a59SAlexey Zelkin flags |= SUPRESS_CURR_SYMBOL; 1739d430a59SAlexey Zelkin continue; 1749d430a59SAlexey Zelkin case '-': /* alignment (left) */ 1759d430a59SAlexey Zelkin flags |= LEFT_JUSTIFY; 1769d430a59SAlexey Zelkin continue; 1779d430a59SAlexey Zelkin default: 1789d430a59SAlexey Zelkin break; 1799d430a59SAlexey Zelkin } 1809d430a59SAlexey Zelkin break; 1819d430a59SAlexey Zelkin } 1829d430a59SAlexey Zelkin 1839d430a59SAlexey Zelkin /* field Width */ 1849d430a59SAlexey Zelkin if (isdigit((unsigned char)*fmt)) { 1859d430a59SAlexey Zelkin GET_NUMBER(width); 1869d430a59SAlexey Zelkin /* Do we have enough space to put number with 1879d430a59SAlexey Zelkin * required width ? 1889d430a59SAlexey Zelkin */ 1899d430a59SAlexey Zelkin if (dst + width >= s + maxsize) 1909d430a59SAlexey Zelkin goto e2big_error; 1919d430a59SAlexey Zelkin } 1929d430a59SAlexey Zelkin 193284d5622STim J. Robbins /* Left precision */ 194284d5622STim J. Robbins if (*fmt == '#') { 195284d5622STim J. Robbins if (!isdigit((unsigned char)*++fmt)) 196284d5622STim J. Robbins goto format_error; 197284d5622STim J. Robbins GET_NUMBER(left_prec); 198284d5622STim J. Robbins } 199284d5622STim J. Robbins 200284d5622STim J. Robbins /* Right precision */ 201284d5622STim J. Robbins if (*fmt == '.') { 202284d5622STim J. Robbins if (!isdigit((unsigned char)*++fmt)) 203284d5622STim J. Robbins goto format_error; 204284d5622STim J. Robbins GET_NUMBER(right_prec); 205284d5622STim J. Robbins } 206284d5622STim J. Robbins 2079d430a59SAlexey Zelkin /* Conversion Characters */ 2089d430a59SAlexey Zelkin switch (*fmt++) { 2099d430a59SAlexey Zelkin case 'i': /* use internaltion currency format */ 2109d430a59SAlexey Zelkin flags |= USE_INTL_CURRENCY; 2119d430a59SAlexey Zelkin break; 2129d430a59SAlexey Zelkin case 'n': /* use national currency format */ 2139d430a59SAlexey Zelkin flags &= ~(USE_INTL_CURRENCY); 2149d430a59SAlexey Zelkin break; 2159d430a59SAlexey Zelkin default: /* required character is missing or 2169d430a59SAlexey Zelkin premature EOS */ 2179d430a59SAlexey Zelkin goto format_error; 2189d430a59SAlexey Zelkin } 2199d430a59SAlexey Zelkin 2209d430a59SAlexey Zelkin if (flags & USE_INTL_CURRENCY) { 2219d430a59SAlexey Zelkin currency_symbol = strdup(lc->int_curr_symbol); 2229d430a59SAlexey Zelkin if (currency_symbol != NULL) 2239d430a59SAlexey Zelkin space_char = *(currency_symbol+3); 224a5aecc77SMike Barcroft } else 2259d430a59SAlexey Zelkin currency_symbol = strdup(lc->currency_symbol); 2269d430a59SAlexey Zelkin 2279d430a59SAlexey Zelkin if (currency_symbol == NULL) 2289d430a59SAlexey Zelkin goto end_error; /* ENOMEM. */ 2299d430a59SAlexey Zelkin 2309d430a59SAlexey Zelkin /* value itself */ 2319d430a59SAlexey Zelkin value = va_arg(ap, double); 2329d430a59SAlexey Zelkin 2339d430a59SAlexey Zelkin /* detect sign */ 2349d430a59SAlexey Zelkin if (value < 0) { 2359d430a59SAlexey Zelkin flags |= IS_NEGATIVE; 2369d430a59SAlexey Zelkin value = -value; 2379d430a59SAlexey Zelkin } 2389d430a59SAlexey Zelkin 2399d430a59SAlexey Zelkin /* fill left_prec with amount of padding chars */ 2409d430a59SAlexey Zelkin if (left_prec >= 0) { 2419d430a59SAlexey Zelkin pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 2429d430a59SAlexey Zelkin currency_symbol) - 2439d430a59SAlexey Zelkin __calc_left_pad(flags, currency_symbol); 2449d430a59SAlexey Zelkin if (pad_size < 0) 2459d430a59SAlexey Zelkin pad_size = 0; 2469d430a59SAlexey Zelkin } 2479d430a59SAlexey Zelkin 2489d430a59SAlexey Zelkin asciivalue = __format_grouped_double(value, &flags, 2499d430a59SAlexey Zelkin left_prec, right_prec, pad_char); 2509d430a59SAlexey Zelkin if (asciivalue == NULL) 2519d430a59SAlexey Zelkin goto end_error; /* errno already set */ 2529d430a59SAlexey Zelkin /* to ENOMEM by malloc() */ 2539d430a59SAlexey Zelkin 2549d430a59SAlexey Zelkin /* set some variables for later use */ 2559d430a59SAlexey Zelkin __setup_vars(flags, &cs_precedes, &sep_by_space, 2569d430a59SAlexey Zelkin &sign_posn, &signstr); 2579d430a59SAlexey Zelkin 2589d430a59SAlexey Zelkin /* 2599d430a59SAlexey Zelkin * Description of some LC_MONETARY's values: 2609d430a59SAlexey Zelkin * 2619d430a59SAlexey Zelkin * p_cs_precedes & n_cs_precedes 2629d430a59SAlexey Zelkin * 2639d430a59SAlexey Zelkin * = 1 - $currency_symbol precedes the value 2649d430a59SAlexey Zelkin * for a monetary quantity with a non-negative value 2659d430a59SAlexey Zelkin * = 0 - symbol succeeds the value 2669d430a59SAlexey Zelkin * 2679d430a59SAlexey Zelkin * p_sep_by_space & n_sep_by_space 2689d430a59SAlexey Zelkin * 2699d430a59SAlexey Zelkin * = 0 - no space separates $currency_symbol 2709d430a59SAlexey Zelkin * from the value for a monetary quantity with a 2719d430a59SAlexey Zelkin * non-negative value 2729d430a59SAlexey Zelkin * = 1 - space separates the symbol from the value 2739d430a59SAlexey Zelkin * = 2 - space separates the symbol and the sign string, 2749d430a59SAlexey Zelkin * if adjacent. 2759d430a59SAlexey Zelkin * 2769d430a59SAlexey Zelkin * p_sign_posn & n_sign_posn 2779d430a59SAlexey Zelkin * 2789d430a59SAlexey Zelkin * = 0 - parentheses enclose the quantity and the 2799d430a59SAlexey Zelkin * $currency_symbol 2809d430a59SAlexey Zelkin * = 1 - the sign string precedes the quantity and the 2819d430a59SAlexey Zelkin * $currency_symbol 2829d430a59SAlexey Zelkin * = 2 - the sign string succeeds the quantity and the 2839d430a59SAlexey Zelkin * $currency_symbol 2849d430a59SAlexey Zelkin * = 3 - the sign string precedes the $currency_symbol 2859d430a59SAlexey Zelkin * = 4 - the sign string succeeds the $currency_symbol 2869d430a59SAlexey Zelkin * 2879d430a59SAlexey Zelkin */ 2889d430a59SAlexey Zelkin 2899d430a59SAlexey Zelkin tmpptr = dst; 2909d430a59SAlexey Zelkin 2919d430a59SAlexey Zelkin while (pad_size-- > 0) 2929d430a59SAlexey Zelkin PRINT(' '); 2939d430a59SAlexey Zelkin 29440a48101STim J. Robbins if (sign_posn == 0 && (flags & IS_NEGATIVE)) 2959d430a59SAlexey Zelkin PRINT('('); 2969d430a59SAlexey Zelkin 2979d430a59SAlexey Zelkin if (cs_precedes == 1) { 2989d430a59SAlexey Zelkin if (sign_posn == 1 || sign_posn == 3) { 2999d430a59SAlexey Zelkin PRINTS(signstr); 3009d430a59SAlexey Zelkin if (sep_by_space == 2) /* XXX: ? */ 3019d430a59SAlexey Zelkin PRINT(' '); 3029d430a59SAlexey Zelkin } 3039d430a59SAlexey Zelkin 3049d430a59SAlexey Zelkin if (!(flags & SUPRESS_CURR_SYMBOL)) { 3059d430a59SAlexey Zelkin PRINTS(currency_symbol); 3069d430a59SAlexey Zelkin 3079d430a59SAlexey Zelkin if (sign_posn == 4) { 3089d430a59SAlexey Zelkin if (sep_by_space == 2) 3099d430a59SAlexey Zelkin PRINT(space_char); 3109d430a59SAlexey Zelkin PRINTS(signstr); 3119d430a59SAlexey Zelkin if (sep_by_space == 1) 3129d430a59SAlexey Zelkin PRINT(' '); 313a5aecc77SMike Barcroft } else if (sep_by_space == 1) 3149d430a59SAlexey Zelkin PRINT(space_char); 3159d430a59SAlexey Zelkin } 316a5aecc77SMike Barcroft } else if (sign_posn == 1) 3179d430a59SAlexey Zelkin PRINTS(signstr); 3189d430a59SAlexey Zelkin 3199d430a59SAlexey Zelkin PRINTS(asciivalue); 3209d430a59SAlexey Zelkin 3219d430a59SAlexey Zelkin if (cs_precedes == 0) { 3229d430a59SAlexey Zelkin if (sign_posn == 3) { 3239d430a59SAlexey Zelkin if (sep_by_space == 1) 3249d430a59SAlexey Zelkin PRINT(' '); 3259d430a59SAlexey Zelkin PRINTS(signstr); 3269d430a59SAlexey Zelkin } 3279d430a59SAlexey Zelkin 3289d430a59SAlexey Zelkin if (!(flags & SUPRESS_CURR_SYMBOL)) { 3299d430a59SAlexey Zelkin if ((sign_posn == 3 && sep_by_space == 2) 3309d430a59SAlexey Zelkin || (sep_by_space == 1 3319d430a59SAlexey Zelkin && (sign_posn = 0 3329d430a59SAlexey Zelkin || sign_posn == 1 3339d430a59SAlexey Zelkin || sign_posn == 2 3349d430a59SAlexey Zelkin || sign_posn == 4))) 3359d430a59SAlexey Zelkin PRINT(space_char); 3369d430a59SAlexey Zelkin PRINTS(currency_symbol); /* XXX: len */ 3379d430a59SAlexey Zelkin if (sign_posn == 4) { 3389d430a59SAlexey Zelkin if (sep_by_space == 2) 3399d430a59SAlexey Zelkin PRINT(' '); 3409d430a59SAlexey Zelkin PRINTS(signstr); 3419d430a59SAlexey Zelkin } 3429d430a59SAlexey Zelkin } 3439d430a59SAlexey Zelkin } 3449d430a59SAlexey Zelkin 3459d430a59SAlexey Zelkin if (sign_posn == 2) { 3469d430a59SAlexey Zelkin if (sep_by_space == 2) 3479d430a59SAlexey Zelkin PRINT(' '); 3489d430a59SAlexey Zelkin PRINTS(signstr); 3499d430a59SAlexey Zelkin } 3509d430a59SAlexey Zelkin 3519d430a59SAlexey Zelkin if (sign_posn == 0 && (flags & IS_NEGATIVE)) 3529d430a59SAlexey Zelkin PRINT(')'); 3539d430a59SAlexey Zelkin 3549d430a59SAlexey Zelkin if (dst - tmpptr < width) { 3559d430a59SAlexey Zelkin if (flags & LEFT_JUSTIFY) { 3569d430a59SAlexey Zelkin while (dst - tmpptr <= width) 3579d430a59SAlexey Zelkin PRINT(' '); 3589d430a59SAlexey Zelkin } else { 3599d430a59SAlexey Zelkin pad_size = dst-tmpptr; 360a5aecc77SMike Barcroft memmove(tmpptr + width-pad_size, tmpptr, 361a5aecc77SMike Barcroft pad_size); 3629d430a59SAlexey Zelkin memset(tmpptr, ' ', width-pad_size); 3639d430a59SAlexey Zelkin dst += width-pad_size; 3649d430a59SAlexey Zelkin } 3659d430a59SAlexey Zelkin } 3669d430a59SAlexey Zelkin } 3679d430a59SAlexey Zelkin 3689d430a59SAlexey Zelkin PRINT('\0'); 3699d430a59SAlexey Zelkin va_end(ap); 3709d430a59SAlexey Zelkin return (dst - s - 1); /* return size of put data except trailing '\0' */ 3719d430a59SAlexey Zelkin 3729d430a59SAlexey Zelkin e2big_error: 3739d430a59SAlexey Zelkin errno = E2BIG; 3749d430a59SAlexey Zelkin goto end_error; 3759d430a59SAlexey Zelkin 3769d430a59SAlexey Zelkin format_error: 3779d430a59SAlexey Zelkin errno = EINVAL; 3789d430a59SAlexey Zelkin 3799d430a59SAlexey Zelkin end_error: 3809d430a59SAlexey Zelkin if (asciivalue != NULL) 3819d430a59SAlexey Zelkin free(asciivalue); 3829d430a59SAlexey Zelkin if (currency_symbol != NULL) 3839d430a59SAlexey Zelkin free(currency_symbol); 3849d430a59SAlexey Zelkin va_end(ap); 3859d430a59SAlexey Zelkin return (-1); 3869d430a59SAlexey Zelkin } 3879d430a59SAlexey Zelkin 3889d430a59SAlexey Zelkin static void 3899d430a59SAlexey Zelkin __setup_vars(int flags, char *cs_precedes, char *sep_by_space, 3909d430a59SAlexey Zelkin char *sign_posn, char **signstr) { 3919d430a59SAlexey Zelkin 3929d430a59SAlexey Zelkin struct lconv *lc = localeconv(); 3939d430a59SAlexey Zelkin 3949d430a59SAlexey Zelkin if (flags & IS_NEGATIVE) { 3959d430a59SAlexey Zelkin *cs_precedes = lc->n_cs_precedes; 3969d430a59SAlexey Zelkin *sep_by_space = lc->n_sep_by_space; 3979d430a59SAlexey Zelkin *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 398a5aecc77SMike Barcroft *signstr = (lc->negative_sign == '\0') ? "-" 399a5aecc77SMike Barcroft : lc->negative_sign; 4009d430a59SAlexey Zelkin } else { 4019d430a59SAlexey Zelkin *cs_precedes = lc->p_cs_precedes; 4029d430a59SAlexey Zelkin *sep_by_space = lc->p_sep_by_space; 4039d430a59SAlexey Zelkin *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 4049d430a59SAlexey Zelkin *signstr = lc->positive_sign; 4059d430a59SAlexey Zelkin } 4069d430a59SAlexey Zelkin 4079d430a59SAlexey Zelkin /* Set defult values for unspecified information. */ 4089d430a59SAlexey Zelkin if (*cs_precedes != 0) 4099d430a59SAlexey Zelkin *cs_precedes = 1; 4109d430a59SAlexey Zelkin if (*sep_by_space == CHAR_MAX) 4119d430a59SAlexey Zelkin *sep_by_space = 0; 4129d430a59SAlexey Zelkin if (*sign_posn == CHAR_MAX) 4139d430a59SAlexey Zelkin *sign_posn = 0; 4149d430a59SAlexey Zelkin } 4159d430a59SAlexey Zelkin 4169d430a59SAlexey Zelkin static int 4179d430a59SAlexey Zelkin __calc_left_pad(int flags, char *cur_symb) { 4189d430a59SAlexey Zelkin 4199d430a59SAlexey Zelkin char cs_precedes, sep_by_space, sign_posn, *signstr; 4209d430a59SAlexey Zelkin int left_chars = 0; 4219d430a59SAlexey Zelkin 4229d430a59SAlexey Zelkin __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 4239d430a59SAlexey Zelkin 4249d430a59SAlexey Zelkin if (cs_precedes != 0) { 4259d430a59SAlexey Zelkin left_chars += strlen(cur_symb); 4269d430a59SAlexey Zelkin if (sep_by_space != 0) 4279d430a59SAlexey Zelkin left_chars++; 4289d430a59SAlexey Zelkin } 4299d430a59SAlexey Zelkin 4309d430a59SAlexey Zelkin switch (sign_posn) { 4319d430a59SAlexey Zelkin case 1: 4329d430a59SAlexey Zelkin left_chars += strlen(signstr); 4339d430a59SAlexey Zelkin break; 4349d430a59SAlexey Zelkin case 3: 4359d430a59SAlexey Zelkin case 4: 4369d430a59SAlexey Zelkin if (cs_precedes != 0) 4379d430a59SAlexey Zelkin left_chars += strlen(signstr); 4389d430a59SAlexey Zelkin } 439a5aecc77SMike Barcroft return (left_chars); 4409d430a59SAlexey Zelkin } 4419d430a59SAlexey Zelkin 4429d430a59SAlexey Zelkin static int 4439d430a59SAlexey Zelkin get_groups(int size, char *grouping) { 4449d430a59SAlexey Zelkin 4459d430a59SAlexey Zelkin int chars = 0; 4469d430a59SAlexey Zelkin 4479d430a59SAlexey Zelkin if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 448a5aecc77SMike Barcroft return (0); 4499d430a59SAlexey Zelkin 4509d430a59SAlexey Zelkin while (size > (int)*grouping) { 4519d430a59SAlexey Zelkin chars++; 4529d430a59SAlexey Zelkin size -= (int)*grouping++; 4539d430a59SAlexey Zelkin /* no more grouping ? */ 4549d430a59SAlexey Zelkin if (*grouping == CHAR_MAX) 4559d430a59SAlexey Zelkin break; 4569d430a59SAlexey Zelkin /* rest grouping with same value ? */ 4579d430a59SAlexey Zelkin if (*grouping == 0) { 4589d430a59SAlexey Zelkin chars += (size - 1) / *(grouping - 1); 4599d430a59SAlexey Zelkin break; 4609d430a59SAlexey Zelkin } 4619d430a59SAlexey Zelkin } 462a5aecc77SMike Barcroft return (chars); 4639d430a59SAlexey Zelkin } 4649d430a59SAlexey Zelkin 4659d430a59SAlexey Zelkin /* convert double to ASCII */ 4669d430a59SAlexey Zelkin static char * 4679d430a59SAlexey Zelkin __format_grouped_double(double value, int *flags, 4689d430a59SAlexey Zelkin int left_prec, int right_prec, int pad_char) { 4699d430a59SAlexey Zelkin 4709d430a59SAlexey Zelkin char *rslt; 4719d430a59SAlexey Zelkin char *avalue; 4729d430a59SAlexey Zelkin int avalue_size; 4739d430a59SAlexey Zelkin char fmt[32]; 4749d430a59SAlexey Zelkin 4759d430a59SAlexey Zelkin size_t bufsize; 4769d430a59SAlexey Zelkin char *bufend; 4779d430a59SAlexey Zelkin 4789d430a59SAlexey Zelkin int padded; 4799d430a59SAlexey Zelkin 4809d430a59SAlexey Zelkin struct lconv *lc = localeconv(); 4819d430a59SAlexey Zelkin char *grouping; 4829d430a59SAlexey Zelkin char decimal_point; 4839d430a59SAlexey Zelkin char thousands_sep; 4849d430a59SAlexey Zelkin 4859d430a59SAlexey Zelkin int groups = 0; 4869d430a59SAlexey Zelkin 4879d430a59SAlexey Zelkin grouping = lc->mon_grouping; 4889d430a59SAlexey Zelkin decimal_point = *lc->mon_decimal_point; 4899d430a59SAlexey Zelkin if (decimal_point == '\0') { 4909d430a59SAlexey Zelkin decimal_point = *lc->decimal_point; 4919d430a59SAlexey Zelkin if (decimal_point == '\0') 4929d430a59SAlexey Zelkin decimal_point = '.'; 4939d430a59SAlexey Zelkin } 4949d430a59SAlexey Zelkin thousands_sep = *lc->mon_thousands_sep; 495a5aecc77SMike Barcroft if (thousands_sep == '\0') 4969d430a59SAlexey Zelkin thousands_sep = *lc->thousands_sep; 4979d430a59SAlexey Zelkin 4989d430a59SAlexey Zelkin /* fill left_prec with default value */ 4999d430a59SAlexey Zelkin if (left_prec == -1) 5009d430a59SAlexey Zelkin left_prec = 0; 5019d430a59SAlexey Zelkin 5029d430a59SAlexey Zelkin /* fill right_prec with default value */ 5039d430a59SAlexey Zelkin if (right_prec == -1) { 5049d430a59SAlexey Zelkin if (*flags & USE_INTL_CURRENCY) 5059d430a59SAlexey Zelkin right_prec = lc->int_frac_digits; 5069d430a59SAlexey Zelkin else 5079d430a59SAlexey Zelkin right_prec = lc->frac_digits; 5089d430a59SAlexey Zelkin 5099d430a59SAlexey Zelkin if (right_prec == CHAR_MAX) /* POSIX locale ? */ 5109d430a59SAlexey Zelkin right_prec = 2; 5119d430a59SAlexey Zelkin } 5129d430a59SAlexey Zelkin 5139d430a59SAlexey Zelkin if (*flags & NEED_GROUPING) 5149d430a59SAlexey Zelkin left_prec += get_groups(left_prec, grouping); 5159d430a59SAlexey Zelkin 5169d430a59SAlexey Zelkin /* convert to string */ 517a5aecc77SMike Barcroft snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 518a5aecc77SMike Barcroft right_prec); 5199d430a59SAlexey Zelkin avalue_size = asprintf(&avalue, fmt, value); 5209d430a59SAlexey Zelkin if (avalue_size < 0) 521a5aecc77SMike Barcroft return (NULL); 5229d430a59SAlexey Zelkin 5239d430a59SAlexey Zelkin /* make sure that we've enough space for result string */ 5249d430a59SAlexey Zelkin bufsize = strlen(avalue)*2+1; 5259d430a59SAlexey Zelkin rslt = malloc(bufsize); 5269d430a59SAlexey Zelkin if (rslt == NULL) { 5279d430a59SAlexey Zelkin free(avalue); 528a5aecc77SMike Barcroft return (NULL); 5299d430a59SAlexey Zelkin } 5309d430a59SAlexey Zelkin memset(rslt, 0, bufsize); 5319d430a59SAlexey Zelkin bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 5329d430a59SAlexey Zelkin 5339d430a59SAlexey Zelkin /* skip spaces at beggining */ 5349d430a59SAlexey Zelkin padded = 0; 5359d430a59SAlexey Zelkin while (avalue[padded] == ' ') { 5369d430a59SAlexey Zelkin padded++; 5379d430a59SAlexey Zelkin avalue_size--; 5389d430a59SAlexey Zelkin } 5399d430a59SAlexey Zelkin 5409d430a59SAlexey Zelkin if (right_prec > 0) { 5419d430a59SAlexey Zelkin bufend -= right_prec; 542a5aecc77SMike Barcroft memcpy(bufend, avalue + avalue_size+padded-right_prec, 543a5aecc77SMike Barcroft right_prec); 5449d430a59SAlexey Zelkin *--bufend = decimal_point; 5459d430a59SAlexey Zelkin avalue_size -= (right_prec + 1); 5469d430a59SAlexey Zelkin } 5479d430a59SAlexey Zelkin 5489d430a59SAlexey Zelkin if ((*flags & NEED_GROUPING) && 5499d430a59SAlexey Zelkin thousands_sep != '\0' && /* XXX: need investigation */ 5509d430a59SAlexey Zelkin *grouping != CHAR_MAX && 5519d430a59SAlexey Zelkin *grouping > 0) { 5529d430a59SAlexey Zelkin while (avalue_size > (int)*grouping) { 5539d430a59SAlexey Zelkin GRPCPY(*grouping); 5549d430a59SAlexey Zelkin GRPSEP; 5559d430a59SAlexey Zelkin grouping++; 5569d430a59SAlexey Zelkin 5579d430a59SAlexey Zelkin /* no more grouping ? */ 5589d430a59SAlexey Zelkin if (*grouping == CHAR_MAX) 5599d430a59SAlexey Zelkin break; 5609d430a59SAlexey Zelkin 5619d430a59SAlexey Zelkin /* rest grouping with same value ? */ 5629d430a59SAlexey Zelkin if (*grouping == 0) { 5639d430a59SAlexey Zelkin grouping--; 5649d430a59SAlexey Zelkin while (avalue_size > *grouping) { 5659d430a59SAlexey Zelkin GRPCPY(*grouping); 5669d430a59SAlexey Zelkin GRPSEP; 5679d430a59SAlexey Zelkin } 5689d430a59SAlexey Zelkin } 5699d430a59SAlexey Zelkin } 5709d430a59SAlexey Zelkin if (avalue_size != 0) 5719d430a59SAlexey Zelkin GRPCPY(avalue_size); 5729d430a59SAlexey Zelkin padded -= groups; 5739d430a59SAlexey Zelkin 5749d430a59SAlexey Zelkin } else { 5759d430a59SAlexey Zelkin bufend -= avalue_size; 5769d430a59SAlexey Zelkin memcpy(bufend, avalue+padded, avalue_size); 5779d430a59SAlexey Zelkin if (right_prec == 0) 5789d430a59SAlexey Zelkin padded--; /* decrease assumed $decimal_point */ 5799d430a59SAlexey Zelkin } 5809d430a59SAlexey Zelkin 5819d430a59SAlexey Zelkin /* do padding with pad_char */ 5829d430a59SAlexey Zelkin if (padded > 0) { 5839d430a59SAlexey Zelkin bufend -= padded; 5849d430a59SAlexey Zelkin memset(bufend, pad_char, padded); 5859d430a59SAlexey Zelkin } 5869d430a59SAlexey Zelkin 5879d430a59SAlexey Zelkin bufsize = bufsize - (rslt - bufend); 5889d430a59SAlexey Zelkin memmove(rslt, bufend, bufsize); 5899d430a59SAlexey Zelkin free(avalue); 590a5aecc77SMike Barcroft return (rslt); 5919d430a59SAlexey Zelkin } 592