1 /* $NetBSD: strfmon.c,v 1.3 2005/12/02 14:19:43 yamt 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.3 2005/12/02 14:19:43 yamt Exp $"); 36 #endif 37 #endif /* LIBC_SCCS and not lint */ 38 39 #if defined(__NetBSD__) 40 #include "namespace.h" 41 #include <monetary.h> 42 #endif 43 44 #include <sys/types.h> 45 #include <ctype.h> 46 #include <errno.h> 47 #include <limits.h> 48 #include <locale.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 /* internal flags */ 55 #define NEED_GROUPING 0x01 /* print digits grouped (default) */ 56 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 57 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 58 #define PARENTH_POSN 0x08 /* enclose negative amount in () */ 59 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 60 #define LEFT_JUSTIFY 0x20 /* left justify */ 61 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 62 #define IS_NEGATIVE 0x80 /* is argument value negative ? */ 63 64 /* internal macros */ 65 #define PRINT(CH) do { \ 66 if (dst >= s + maxsize) \ 67 goto e2big_error; \ 68 *dst++ = CH; \ 69 } while (/* CONSTCOND */ 0) 70 71 #define PRINTS(STR) do { \ 72 const char *tmps = STR; \ 73 while (*tmps != '\0') \ 74 PRINT(*tmps++); \ 75 } while (/* CONSTCOND */ 0) 76 77 #define GET_NUMBER(VAR) do { \ 78 VAR = 0; \ 79 while (isdigit((unsigned char)*fmt)) { \ 80 VAR *= 10; \ 81 VAR += *fmt - '0'; \ 82 fmt++; \ 83 } \ 84 } while (/* CONSTCOND */ 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 (/* CONSTCOND */ 0) 93 94 #define GRPSEP do { \ 95 *--bufend = thousands_sep; \ 96 groups++; \ 97 } while (/* CONSTCOND */ 0) 98 99 static void __setup_vars(int, char *, char *, char *, const char **); 100 static int __calc_left_pad(int, char *); 101 static char *__format_grouped_double(double, int *, int, int, int); 102 103 ssize_t 104 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 105 ...) 106 { 107 va_list ap; 108 char *dst; /* output destination pointer */ 109 const char *fmt; /* current format poistion pointer */ 110 struct lconv *lc; /* pointer to lconv structure */ 111 char *asciivalue; /* formatted double pointer */ 112 113 int flags; /* formatting options */ 114 int pad_char; /* padding character */ 115 int pad_size; /* pad size */ 116 int width; /* field width */ 117 int left_prec; /* left precision */ 118 int right_prec; /* right precision */ 119 double value; /* just value */ 120 char space_char = ' '; /* space after currency */ 121 122 char cs_precedes, /* values gathered from struct lconv */ 123 sep_by_space, 124 sign_posn, 125 *currency_symbol; 126 const char *signstr; 127 128 char *tmpptr; /* temporary vars */ 129 int sverrno; 130 131 va_start(ap, format); 132 133 lc = localeconv(); 134 dst = s; 135 fmt = format; 136 asciivalue = NULL; 137 currency_symbol = NULL; 138 pad_size = 0; 139 140 while (*fmt) { 141 /* pass nonformating characters AS IS */ 142 if (*fmt != '%') 143 goto literal; 144 145 /* '%' found ! */ 146 147 /* "%%" mean just '%' */ 148 if (*(fmt+1) == '%') { 149 fmt++; 150 literal: 151 PRINT(*fmt++); 152 continue; 153 } 154 155 /* set up initial values */ 156 flags = (NEED_GROUPING|LOCALE_POSN); 157 pad_char = ' '; /* padding character is "space" */ 158 left_prec = -1; /* no left precision specified */ 159 right_prec = -1; /* no right precision specified */ 160 width = -1; /* no width specified */ 161 value = 0; /* we have no value to print now */ 162 163 /* Flags */ 164 while (/* CONSTCOND */ 1) { 165 switch (*++fmt) { 166 case '=': /* fill character */ 167 pad_char = *++fmt; 168 if (pad_char == '\0') 169 goto format_error; 170 continue; 171 case '^': /* not group currency */ 172 flags &= ~(NEED_GROUPING); 173 continue; 174 case '+': /* use locale defined signs */ 175 if (flags & SIGN_POSN_USED) 176 goto format_error; 177 flags |= (SIGN_POSN_USED|LOCALE_POSN); 178 continue; 179 case '(': /* enclose negatives with () */ 180 if (flags & SIGN_POSN_USED) 181 goto format_error; 182 flags |= (SIGN_POSN_USED|PARENTH_POSN); 183 continue; 184 case '!': /* suppress currency symbol */ 185 flags |= SUPRESS_CURR_SYMBOL; 186 continue; 187 case '-': /* alignment (left) */ 188 flags |= LEFT_JUSTIFY; 189 continue; 190 default: 191 break; 192 } 193 break; 194 } 195 196 /* field Width */ 197 if (isdigit((unsigned char)*fmt)) { 198 GET_NUMBER(width); 199 /* Do we have enough space to put number with 200 * required width ? 201 */ 202 if (dst + width >= s + maxsize) 203 goto e2big_error; 204 } 205 206 /* Left precision */ 207 if (*fmt == '#') { 208 if (!isdigit((unsigned char)*++fmt)) 209 goto format_error; 210 GET_NUMBER(left_prec); 211 } 212 213 /* Right precision */ 214 if (*fmt == '.') { 215 if (!isdigit((unsigned char)*++fmt)) 216 goto format_error; 217 GET_NUMBER(right_prec); 218 } 219 220 /* Conversion Characters */ 221 switch (*fmt++) { 222 case 'i': /* use internaltion currency format */ 223 flags |= USE_INTL_CURRENCY; 224 break; 225 case 'n': /* use national currency format */ 226 flags &= ~(USE_INTL_CURRENCY); 227 break; 228 default: /* required character is missing or 229 premature EOS */ 230 goto format_error; 231 } 232 233 if (flags & USE_INTL_CURRENCY) { 234 currency_symbol = strdup(lc->int_curr_symbol); 235 if (currency_symbol != NULL) 236 space_char = *(currency_symbol+3); 237 } else 238 currency_symbol = strdup(lc->currency_symbol); 239 240 if (currency_symbol == NULL) 241 goto end_error; /* ENOMEM. */ 242 243 /* value itself */ 244 value = va_arg(ap, double); 245 246 /* detect sign */ 247 if (value < 0) { 248 flags |= IS_NEGATIVE; 249 value = -value; 250 } 251 252 /* fill left_prec with amount of padding chars */ 253 if (left_prec >= 0) { 254 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 255 currency_symbol) - 256 __calc_left_pad(flags, currency_symbol); 257 if (pad_size < 0) 258 pad_size = 0; 259 } 260 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 (size_t) pad_size); 375 memset(tmpptr, ' ', (size_t) 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 = malloc(bufsize); 551 if (rslt == NULL) { 552 free(avalue); 553 return (NULL); 554 } 555 memset(rslt, 0, bufsize); 556 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 557 558 /* skip spaces at beggining */ 559 padded = 0; 560 while (avalue[padded] == ' ') { 561 padded++; 562 avalue_size--; 563 } 564 565 if (right_prec > 0) { 566 bufend -= right_prec; 567 memcpy(bufend, avalue + avalue_size+padded-right_prec, 568 (size_t) right_prec); 569 *--bufend = decimal_point; 570 avalue_size -= (right_prec + 1); 571 } 572 573 if ((*flags & NEED_GROUPING) && 574 thousands_sep != '\0' && /* XXX: need investigation */ 575 *grouping != CHAR_MAX && 576 *grouping > 0) { 577 while (avalue_size > (int)*grouping) { 578 GRPCPY(*grouping); 579 GRPSEP; 580 grouping++; 581 582 /* no more grouping ? */ 583 if (*grouping == CHAR_MAX) 584 break; 585 586 /* rest grouping with same value ? */ 587 if (*grouping == 0) { 588 grouping--; 589 while (avalue_size > *grouping) { 590 GRPCPY(*grouping); 591 GRPSEP; 592 } 593 } 594 } 595 if (avalue_size != 0) 596 GRPCPY(avalue_size); 597 padded -= groups; 598 599 } else { 600 bufend -= avalue_size; 601 memcpy(bufend, avalue+padded, (size_t) avalue_size); 602 if (right_prec == 0) 603 padded--; /* decrease assumed $decimal_point */ 604 } 605 606 /* do padding with pad_char */ 607 if (padded > 0) { 608 bufend -= padded; 609 memset(bufend, pad_char, (size_t) padded); 610 } 611 612 bufsize = bufsize - (bufend - rslt) + 1; 613 memmove(rslt, bufend, bufsize); 614 free(avalue); 615 return (rslt); 616 } 617