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