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