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