1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Copyright (c) 2011 The FreeBSD Foundation 8 * All rights reserved. 9 * Portions of this software were developed by David Chisnall 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/types.h> 39 #include <ctype.h> 40 #include <errno.h> 41 #include <limits.h> 42 #include <locale.h> 43 #include <monetary.h> 44 #include <stdarg.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include "xlocale_private.h" 49 50 /* internal flags */ 51 #define NEED_GROUPING 0x01 /* print digits grouped (default) */ 52 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 53 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 54 #define PARENTH_POSN 0x08 /* enclose negative amount in () */ 55 #define SUPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */ 56 #define LEFT_JUSTIFY 0x20 /* left justify */ 57 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 58 #define IS_NEGATIVE 0x80 /* is argument value negative ? */ 59 60 /* internal macros */ 61 #define PRINT(CH) do { \ 62 if (dst >= s + maxsize) \ 63 goto e2big_error; \ 64 *dst++ = CH; \ 65 } while (0) 66 67 #define PRINTS(STR) do { \ 68 char *tmps = STR; \ 69 while (*tmps != '\0') \ 70 PRINT(*tmps++); \ 71 } while (0) 72 73 #define GET_NUMBER(VAR) do { \ 74 VAR = 0; \ 75 while (isdigit((unsigned char)*fmt)) { \ 76 if (VAR > INT_MAX / 10) \ 77 goto e2big_error; \ 78 VAR *= 10; \ 79 VAR += *fmt - '0'; \ 80 if (VAR < 0) \ 81 goto e2big_error; \ 82 fmt++; \ 83 } \ 84 } while (0) 85 86 #define GRPCPY(howmany) do { \ 87 int i = howmany; \ 88 while (i-- > 0) { \ 89 avalue_size--; \ 90 *--bufend = *(avalue+avalue_size+padded); \ 91 } \ 92 } while (0) 93 94 #define GRPSEP do { \ 95 *--bufend = thousands_sep; \ 96 groups++; \ 97 } while (0) 98 99 static void __setup_vars(int, char *, char *, char *, char **); 100 static int __calc_left_pad(int, char *); 101 static char *__format_grouped_double(double, int *, int, int, int); 102 103 static ssize_t 104 vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, 105 const char * __restrict format, va_list ap) 106 { 107 char *dst; /* output destination pointer */ 108 const char *fmt; /* current format poistion pointer */ 109 struct lconv *lc; /* pointer to lconv structure */ 110 char *asciivalue; /* formatted double pointer */ 111 112 int flags; /* formatting options */ 113 int pad_char; /* padding character */ 114 int pad_size; /* pad size */ 115 int width; /* field width */ 116 int left_prec; /* left precision */ 117 int right_prec; /* right precision */ 118 double value; /* just value */ 119 char space_char = ' '; /* space after currency */ 120 121 char cs_precedes, /* values gathered from struct lconv */ 122 sep_by_space, 123 sign_posn, 124 *signstr, 125 *currency_symbol; 126 127 char *tmpptr; /* temporary vars */ 128 int sverrno; 129 FIX_LOCALE(loc); 130 131 132 lc = localeconv_l(loc); 133 dst = s; 134 fmt = format; 135 asciivalue = NULL; 136 currency_symbol = NULL; 137 pad_size = 0; 138 139 while (*fmt) { 140 /* pass nonformating characters AS IS */ 141 if (*fmt != '%') 142 goto literal; 143 144 /* '%' found ! */ 145 146 /* "%%" mean just '%' */ 147 if (*(fmt+1) == '%') { 148 fmt++; 149 literal: 150 PRINT(*fmt++); 151 continue; 152 } 153 154 /* set up initial values */ 155 flags = (NEED_GROUPING|LOCALE_POSN); 156 pad_char = ' '; /* padding character is "space" */ 157 left_prec = -1; /* no left precision specified */ 158 right_prec = -1; /* no right precision specified */ 159 width = -1; /* no width specified */ 160 value = 0; /* we have no value to print now */ 161 162 /* Flags */ 163 while (1) { 164 switch (*++fmt) { 165 case '=': /* fill character */ 166 pad_char = *++fmt; 167 if (pad_char == '\0') 168 goto format_error; 169 continue; 170 case '^': /* not group currency */ 171 flags &= ~(NEED_GROUPING); 172 continue; 173 case '+': /* use locale defined signs */ 174 if (flags & SIGN_POSN_USED) 175 goto format_error; 176 flags |= (SIGN_POSN_USED|LOCALE_POSN); 177 continue; 178 case '(': /* enclose negatives with () */ 179 if (flags & SIGN_POSN_USED) 180 goto format_error; 181 flags |= (SIGN_POSN_USED|PARENTH_POSN); 182 continue; 183 case '!': /* suppress currency symbol */ 184 flags |= SUPRESS_CURR_SYMBOL; 185 continue; 186 case '-': /* alignment (left) */ 187 flags |= LEFT_JUSTIFY; 188 continue; 189 default: 190 break; 191 } 192 break; 193 } 194 195 /* field Width */ 196 if (isdigit((unsigned char)*fmt)) { 197 GET_NUMBER(width); 198 /* Do we have enough space to put number with 199 * required width ? 200 */ 201 if ((unsigned int)width >= maxsize - (dst - s)) 202 goto e2big_error; 203 } 204 205 /* Left precision */ 206 if (*fmt == '#') { 207 if (!isdigit((unsigned char)*++fmt)) 208 goto format_error; 209 GET_NUMBER(left_prec); 210 if ((unsigned int)left_prec >= maxsize - (dst - s)) 211 goto e2big_error; 212 } 213 214 /* Right precision */ 215 if (*fmt == '.') { 216 if (!isdigit((unsigned char)*++fmt)) 217 goto format_error; 218 GET_NUMBER(right_prec); 219 if ((unsigned int)right_prec >= maxsize - (dst - s) - 220 left_prec) 221 goto e2big_error; 222 } 223 224 /* Conversion Characters */ 225 switch (*fmt++) { 226 case 'i': /* use internaltion currency format */ 227 flags |= USE_INTL_CURRENCY; 228 break; 229 case 'n': /* use national currency format */ 230 flags &= ~(USE_INTL_CURRENCY); 231 break; 232 default: /* required character is missing or 233 premature EOS */ 234 goto format_error; 235 } 236 237 if (currency_symbol != NULL) 238 free(currency_symbol); 239 if (flags & USE_INTL_CURRENCY) { 240 currency_symbol = strdup(lc->int_curr_symbol); 241 if (currency_symbol != NULL) 242 space_char = *(currency_symbol+3); 243 } else 244 currency_symbol = strdup(lc->currency_symbol); 245 246 if (currency_symbol == NULL) 247 goto end_error; /* ENOMEM. */ 248 249 /* value itself */ 250 value = va_arg(ap, double); 251 252 /* detect sign */ 253 if (value < 0) { 254 flags |= IS_NEGATIVE; 255 value = -value; 256 } 257 258 /* fill left_prec with amount of padding chars */ 259 if (left_prec >= 0) { 260 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 261 currency_symbol) - 262 __calc_left_pad(flags, currency_symbol); 263 if (pad_size < 0) 264 pad_size = 0; 265 } 266 267 if (asciivalue != NULL) 268 free(asciivalue); 269 asciivalue = __format_grouped_double(value, &flags, 270 left_prec, right_prec, pad_char); 271 if (asciivalue == NULL) 272 goto end_error; /* errno already set */ 273 /* to ENOMEM by malloc() */ 274 275 /* set some variables for later use */ 276 __setup_vars(flags, &cs_precedes, &sep_by_space, 277 &sign_posn, &signstr); 278 279 /* 280 * Description of some LC_MONETARY's values: 281 * 282 * p_cs_precedes & n_cs_precedes 283 * 284 * = 1 - $currency_symbol precedes the value 285 * for a monetary quantity with a non-negative value 286 * = 0 - symbol succeeds the value 287 * 288 * p_sep_by_space & n_sep_by_space 289 * 290 * = 0 - no space separates $currency_symbol 291 * from the value for a monetary quantity with a 292 * non-negative value 293 * = 1 - space separates the symbol from the value 294 * = 2 - space separates the symbol and the sign string, 295 * if adjacent. 296 * 297 * p_sign_posn & n_sign_posn 298 * 299 * = 0 - parentheses enclose the quantity and the 300 * $currency_symbol 301 * = 1 - the sign string precedes the quantity and the 302 * $currency_symbol 303 * = 2 - the sign string succeeds the quantity and the 304 * $currency_symbol 305 * = 3 - the sign string precedes the $currency_symbol 306 * = 4 - the sign string succeeds the $currency_symbol 307 * 308 */ 309 310 tmpptr = dst; 311 312 while (pad_size-- > 0) 313 PRINT(' '); 314 315 if (sign_posn == 0 && (flags & IS_NEGATIVE)) 316 PRINT('('); 317 318 if (cs_precedes == 1) { 319 if (sign_posn == 1 || sign_posn == 3) { 320 PRINTS(signstr); 321 if (sep_by_space == 2) /* XXX: ? */ 322 PRINT(' '); 323 } 324 325 if (!(flags & SUPRESS_CURR_SYMBOL)) { 326 PRINTS(currency_symbol); 327 328 if (sign_posn == 4) { 329 if (sep_by_space == 2) 330 PRINT(space_char); 331 PRINTS(signstr); 332 if (sep_by_space == 1) 333 PRINT(' '); 334 } else if (sep_by_space == 1) 335 PRINT(space_char); 336 } 337 } else if (sign_posn == 1) 338 PRINTS(signstr); 339 340 PRINTS(asciivalue); 341 342 if (cs_precedes == 0) { 343 if (sign_posn == 3) { 344 if (sep_by_space == 1) 345 PRINT(' '); 346 PRINTS(signstr); 347 } 348 349 if (!(flags & SUPRESS_CURR_SYMBOL)) { 350 if ((sign_posn == 3 && sep_by_space == 2) 351 || (sep_by_space == 1 352 && (sign_posn == 0 353 || sign_posn == 1 354 || sign_posn == 2 355 || sign_posn == 4))) 356 PRINT(space_char); 357 PRINTS(currency_symbol); /* XXX: len */ 358 if (sign_posn == 4) { 359 if (sep_by_space == 2) 360 PRINT(' '); 361 PRINTS(signstr); 362 } 363 } 364 } 365 366 if (sign_posn == 2) { 367 if (sep_by_space == 2) 368 PRINT(' '); 369 PRINTS(signstr); 370 } 371 372 if (sign_posn == 0 && (flags & IS_NEGATIVE)) 373 PRINT(')'); 374 375 if (dst - tmpptr < width) { 376 if (flags & LEFT_JUSTIFY) { 377 while (dst - tmpptr < width) 378 PRINT(' '); 379 } else { 380 pad_size = dst-tmpptr; 381 memmove(tmpptr + width-pad_size, tmpptr, 382 pad_size); 383 memset(tmpptr, ' ', width-pad_size); 384 dst += width-pad_size; 385 } 386 } 387 } 388 389 PRINT('\0'); 390 free(asciivalue); 391 free(currency_symbol); 392 return (dst - s - 1); /* return size of put data except trailing '\0' */ 393 394 e2big_error: 395 errno = E2BIG; 396 goto end_error; 397 398 format_error: 399 errno = EINVAL; 400 401 end_error: 402 sverrno = errno; 403 if (asciivalue != NULL) 404 free(asciivalue); 405 if (currency_symbol != NULL) 406 free(currency_symbol); 407 errno = sverrno; 408 return (-1); 409 } 410 ssize_t 411 strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format, 412 ...) 413 { 414 size_t ret; 415 va_list ap; 416 va_start(ap, format); 417 ret = vstrfmon_l(s, maxsize, loc, format, ap); 418 va_end(ap); 419 return ret; 420 } 421 422 ssize_t 423 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 424 ...) 425 { 426 size_t ret; 427 va_list ap; 428 va_start(ap, format); 429 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap); 430 va_end(ap); 431 return ret; 432 } 433 434 435 static void 436 __setup_vars(int flags, char *cs_precedes, char *sep_by_space, 437 char *sign_posn, char **signstr) { 438 439 struct lconv *lc = localeconv(); 440 441 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 442 *cs_precedes = lc->int_n_cs_precedes; 443 *sep_by_space = lc->int_n_sep_by_space; 444 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 445 *signstr = (lc->negative_sign[0] == '\0') ? "-" 446 : lc->negative_sign; 447 } else if (flags & USE_INTL_CURRENCY) { 448 *cs_precedes = lc->int_p_cs_precedes; 449 *sep_by_space = lc->int_p_sep_by_space; 450 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 451 *signstr = lc->positive_sign; 452 } else if (flags & IS_NEGATIVE) { 453 *cs_precedes = lc->n_cs_precedes; 454 *sep_by_space = lc->n_sep_by_space; 455 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 456 *signstr = (lc->negative_sign[0] == '\0') ? "-" 457 : lc->negative_sign; 458 } else { 459 *cs_precedes = lc->p_cs_precedes; 460 *sep_by_space = lc->p_sep_by_space; 461 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 462 *signstr = lc->positive_sign; 463 } 464 465 /* Set defult values for unspecified information. */ 466 if (*cs_precedes != 0) 467 *cs_precedes = 1; 468 if (*sep_by_space == CHAR_MAX) 469 *sep_by_space = 0; 470 if (*sign_posn == CHAR_MAX) 471 *sign_posn = 0; 472 } 473 474 static int 475 __calc_left_pad(int flags, char *cur_symb) { 476 477 char cs_precedes, sep_by_space, sign_posn, *signstr; 478 int left_chars = 0; 479 480 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 481 482 if (cs_precedes != 0) { 483 left_chars += strlen(cur_symb); 484 if (sep_by_space != 0) 485 left_chars++; 486 } 487 488 switch (sign_posn) { 489 case 1: 490 left_chars += strlen(signstr); 491 break; 492 case 3: 493 case 4: 494 if (cs_precedes != 0) 495 left_chars += strlen(signstr); 496 } 497 return (left_chars); 498 } 499 500 static int 501 get_groups(int size, char *grouping) { 502 503 int chars = 0; 504 505 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 506 return (0); 507 508 while (size > (int)*grouping) { 509 chars++; 510 size -= (int)*grouping++; 511 /* no more grouping ? */ 512 if (*grouping == CHAR_MAX) 513 break; 514 /* rest grouping with same value ? */ 515 if (*grouping == 0) { 516 chars += (size - 1) / *(grouping - 1); 517 break; 518 } 519 } 520 return (chars); 521 } 522 523 /* convert double to ASCII */ 524 static char * 525 __format_grouped_double(double value, int *flags, 526 int left_prec, int right_prec, int pad_char) { 527 528 char *rslt; 529 char *avalue; 530 int avalue_size; 531 532 size_t bufsize; 533 char *bufend; 534 535 int padded; 536 537 struct lconv *lc = localeconv(); 538 char *grouping; 539 char decimal_point; 540 char thousands_sep; 541 542 int groups = 0; 543 544 grouping = lc->mon_grouping; 545 decimal_point = *lc->mon_decimal_point; 546 if (decimal_point == '\0') 547 decimal_point = *lc->decimal_point; 548 thousands_sep = *lc->mon_thousands_sep; 549 if (thousands_sep == '\0') 550 thousands_sep = *lc->thousands_sep; 551 552 /* fill left_prec with default value */ 553 if (left_prec == -1) 554 left_prec = 0; 555 556 /* fill right_prec with default value */ 557 if (right_prec == -1) { 558 if (*flags & USE_INTL_CURRENCY) 559 right_prec = lc->int_frac_digits; 560 else 561 right_prec = lc->frac_digits; 562 563 if (right_prec == CHAR_MAX) /* POSIX locale ? */ 564 right_prec = 2; 565 } 566 567 if (*flags & NEED_GROUPING) 568 left_prec += get_groups(left_prec, grouping); 569 570 /* convert to string */ 571 avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1, 572 right_prec, value); 573 if (avalue_size < 0) 574 return (NULL); 575 576 /* make sure that we've enough space for result string */ 577 bufsize = avalue_size * 2 + 1; 578 rslt = calloc(1, bufsize); 579 if (rslt == NULL) { 580 free(avalue); 581 return (NULL); 582 } 583 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 584 585 /* skip spaces at beginning */ 586 padded = 0; 587 while (avalue[padded] == ' ') { 588 padded++; 589 avalue_size--; 590 } 591 592 if (right_prec > 0) { 593 bufend -= right_prec; 594 memcpy(bufend, avalue + avalue_size+padded-right_prec, 595 right_prec); 596 *--bufend = decimal_point; 597 avalue_size -= (right_prec + 1); 598 } 599 600 if ((*flags & NEED_GROUPING) && 601 thousands_sep != '\0' && /* XXX: need investigation */ 602 *grouping != CHAR_MAX && 603 *grouping > 0) { 604 while (avalue_size > (int)*grouping) { 605 GRPCPY(*grouping); 606 GRPSEP; 607 grouping++; 608 609 /* no more grouping ? */ 610 if (*grouping == CHAR_MAX) 611 break; 612 613 /* rest grouping with same value ? */ 614 if (*grouping == 0) { 615 grouping--; 616 while (avalue_size > *grouping) { 617 GRPCPY(*grouping); 618 GRPSEP; 619 } 620 } 621 } 622 if (avalue_size != 0) 623 GRPCPY(avalue_size); 624 padded -= groups; 625 626 } else { 627 bufend -= avalue_size; 628 memcpy(bufend, avalue+padded, avalue_size); 629 if (right_prec == 0) 630 padded--; /* decrease assumed $decimal_point */ 631 } 632 633 /* do padding with pad_char */ 634 if (padded > 0) { 635 bufend -= padded; 636 memset(bufend, pad_char, padded); 637 } 638 639 bufsize = bufsize - (bufend - rslt) + 1; 640 memmove(rslt, bufend, bufsize); 641 free(avalue); 642 return (rslt); 643 } 644