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