2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
7 * Copyright (c) 2011 The FreeBSD Foundation
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/types.h>
47 #include "xlocale_private.h"
50 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
51 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
52 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
53 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
54 #define SUPPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */
55 #define LEFT_JUSTIFY 0x20 /* left justify */
56 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
57 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
60 #define PRINT(CH) do { \
61 if (dst >= s + maxsize) \
66 #define PRINTS(STR) do { \
68 while (*tmps != '\0') \
72 #define GET_NUMBER(VAR, LOC) do { \
74 while (isdigit_l((unsigned char)*fmt, LOC)) { \
75 if (VAR > INT_MAX / 10) \
85 #define GRPCPY(howmany) do { \
89 *--bufend = *(avalue + avalue_size + padded); \
94 bufend -= thousands_sep_size; \
95 memcpy(bufend, thousands_sep, thousands_sep_size); \
99 static void __setup_vars(int, char *, char *, char *, char **, struct lconv *);
100 static int __calc_left_pad(int, char *, struct lconv *);
101 static char *__format_grouped_double(double, int *, int, int, int,
102 struct lconv *, locale_t);
105 vstrfmon_l(char *__restrict s, size_t maxsize, locale_t loc,
106 const char *__restrict format, 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 */
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 */
122 char cs_precedes, /* values gathered from struct lconv */
128 char *tmpptr; /* temporary vars */
133 lc = localeconv_l(loc);
137 currency_symbol = NULL;
140 /* pass nonformating characters AS IS */
146 /* "%%" mean just '%' */
147 if (*(fmt + 1) == '%') {
154 /* set up initial values */
155 flags = NEED_GROUPING | LOCALE_POSN;
156 pad_char = ' '; /* padding character is "space" */
157 pad_size = 0; /* no padding initially */
158 left_prec = -1; /* no left precision specified */
159 right_prec = -1; /* no right precision specified */
160 width = -1; /* no width specified */
165 case '=': /* fill character */
167 if (pad_char == '\0')
170 case '^': /* not group currency */
171 flags &= ~(NEED_GROUPING);
173 case '+': /* use locale defined signs */
174 if (flags & SIGN_POSN_USED)
176 flags |= (SIGN_POSN_USED | LOCALE_POSN);
178 case '(': /* enclose negatives with () */
179 if (flags & SIGN_POSN_USED)
181 flags |= (SIGN_POSN_USED | PARENTH_POSN);
183 case '!': /* suppress currency symbol */
184 flags |= SUPPRESS_CURR_SYMBOL;
186 case '-': /* alignment (left) */
187 flags |= LEFT_JUSTIFY;
196 if (isdigit_l((unsigned char)*fmt, loc)) {
197 GET_NUMBER(width, loc);
199 * Do we have enough space to put number with
202 if ((unsigned int)width >= maxsize - (dst - s))
208 if (!isdigit_l((unsigned char)*++fmt, loc))
210 GET_NUMBER(left_prec, loc);
211 if ((unsigned int)left_prec >= maxsize - (dst - s))
215 /* Right precision */
217 if (!isdigit_l((unsigned char)*++fmt, loc))
219 GET_NUMBER(right_prec, loc);
220 if ((unsigned int)right_prec >=
221 maxsize - (dst - s) - left_prec)
225 /* Conversion Characters */
227 case 'i': /* use international currency format */
228 flags |= USE_INTL_CURRENCY;
230 case 'n': /* use national currency format */
231 flags &= ~(USE_INTL_CURRENCY);
234 * required character is missing or
240 if (currency_symbol != NULL)
241 free(currency_symbol);
242 if (flags & USE_INTL_CURRENCY) {
243 currency_symbol = strdup(lc->int_curr_symbol);
244 if (currency_symbol != NULL &&
245 strlen(currency_symbol) > 3) {
246 space_char = currency_symbol[3];
247 currency_symbol[3] = '\0';
250 currency_symbol = strdup(lc->currency_symbol);
252 if (currency_symbol == NULL)
253 goto end_error; /* ENOMEM. */
256 value = va_arg(ap, double);
260 flags |= IS_NEGATIVE;
264 /* fill left_prec with amount of padding chars */
265 if (left_prec >= 0) {
266 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
267 currency_symbol, lc) -
268 __calc_left_pad(flags, currency_symbol, lc);
273 if (asciivalue != NULL)
275 asciivalue = __format_grouped_double(value, &flags, left_prec,
276 right_prec, pad_char, lc, loc);
277 if (asciivalue == NULL)
279 * errno already set to ENOMEM by
283 /* set some variables for later use */
284 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn,
288 * Description of some LC_MONETARY's values:
290 * p_cs_precedes & n_cs_precedes
292 * = 1 - $currency_symbol precedes the value
293 * for a monetary quantity with a non-negative value
294 * = 0 - symbol succeeds the value
296 * p_sep_by_space & n_sep_by_space
298 * = 0 - no space separates $currency_symbol
299 * from the value for a monetary quantity with a
301 * = 1 - space separates the symbol from the value
302 * = 2 - space separates the symbol and the sign string,
303 * if adjacent; otherwise, a space separates
304 * the sign string from the value
306 * p_sign_posn & n_sign_posn
308 * = 0 - parentheses enclose the quantity and the
310 * = 1 - the sign string precedes the quantity and the
312 * = 2 - the sign string succeeds the quantity and the
314 * = 3 - the sign string precedes the $currency_symbol
315 * = 4 - the sign string succeeds the $currency_symbol
320 while (pad_size-- > 0)
323 if (sign_posn == 0 && (flags & IS_NEGATIVE))
326 if (cs_precedes == 1) {
327 if (sign_posn == 1 || sign_posn == 3) {
329 if (sep_by_space == 2)
333 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
334 PRINTS(currency_symbol);
336 if (sign_posn == 4) {
337 if (sep_by_space == 2)
340 if (sep_by_space == 1)
342 } else if (sep_by_space == 1)
345 } else if (sign_posn == 1) {
347 if (sep_by_space == 2)
353 if (cs_precedes == 0) {
354 if (sign_posn == 3) {
355 if (sep_by_space == 1)
360 if (!(flags & SUPPRESS_CURR_SYMBOL)) {
361 if ((sign_posn == 3 && sep_by_space == 2) ||
362 (sep_by_space == 1 &&
363 (sign_posn == 0 || sign_posn == 1 ||
364 sign_posn == 2 || sign_posn == 4)))
366 PRINTS(currency_symbol);
367 if (sign_posn == 4) {
368 if (sep_by_space == 2)
375 if (sign_posn == 2) {
376 if (sep_by_space == 2)
381 if (sign_posn == 0) {
382 if (flags & IS_NEGATIVE)
384 else if (left_prec >= 0)
388 if (dst - tmpptr < width) {
389 if (flags & LEFT_JUSTIFY) {
390 while (dst - tmpptr < width)
393 pad_size = dst - tmpptr;
394 memmove(tmpptr + width - pad_size, tmpptr,
396 memset(tmpptr, ' ', width - pad_size);
397 dst += width - pad_size;
404 free(currency_symbol);
405 return (dst - s - 1); /* size of put data except trailing '\0' */
416 if (asciivalue != NULL)
418 if (currency_symbol != NULL)
419 free(currency_symbol);
425 __setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn,
426 char **signstr, struct lconv *lc)
428 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
429 *cs_precedes = lc->int_n_cs_precedes;
430 *sep_by_space = lc->int_n_sep_by_space;
431 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
432 *signstr = (lc->negative_sign[0] == '\0') ? "-" :
434 } else if (flags & USE_INTL_CURRENCY) {
435 *cs_precedes = lc->int_p_cs_precedes;
436 *sep_by_space = lc->int_p_sep_by_space;
437 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
438 *signstr = lc->positive_sign;
439 } else if (flags & IS_NEGATIVE) {
440 *cs_precedes = lc->n_cs_precedes;
441 *sep_by_space = lc->n_sep_by_space;
442 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
443 *signstr = (lc->negative_sign[0] == '\0') ? "-" :
446 *cs_precedes = lc->p_cs_precedes;
447 *sep_by_space = lc->p_sep_by_space;
448 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
449 *signstr = lc->positive_sign;
452 /* Set default values for unspecified information. */
453 if (*cs_precedes != 0)
455 if (*sep_by_space == CHAR_MAX)
457 if (*sign_posn == CHAR_MAX)
462 __calc_left_pad(int flags, char *cur_symb, struct lconv *lc)
464 char cs_precedes, sep_by_space, sign_posn, *signstr;
467 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr,
470 if (cs_precedes != 0) {
471 left_chars += strlen(cur_symb);
472 if (sep_by_space != 0)
478 if (flags & IS_NEGATIVE)
482 left_chars += strlen(signstr);
486 if (cs_precedes != 0)
487 left_chars += strlen(signstr);
493 get_groups(int size, char *grouping)
497 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
500 while (size > (int)*grouping) {
502 size -= (int)*grouping++;
503 /* no more grouping ? */
504 if (*grouping == CHAR_MAX)
506 /* rest grouping with same value ? */
507 if (*grouping == 0) {
508 chars += (size - 1) / *(grouping - 1);
515 /* convert double to locale-encoded string */
517 __format_grouped_double(double value, int *flags, int left_prec, int right_prec,
518 int pad_char, struct lconv *lc, locale_t loc)
531 const char *decimal_point;
532 const char *thousands_sep;
533 size_t decimal_point_size;
534 size_t thousands_sep_size;
538 grouping = lc->mon_grouping;
539 decimal_point = lc->mon_decimal_point;
540 if (*decimal_point == '\0')
541 decimal_point = lc->decimal_point;
542 thousands_sep = lc->mon_thousands_sep;
543 if (*thousands_sep == '\0')
544 thousands_sep = lc->thousands_sep;
546 decimal_point_size = strlen(decimal_point);
547 thousands_sep_size = strlen(thousands_sep);
549 /* fill left_prec with default value */
553 /* fill right_prec with default value */
554 if (right_prec == -1) {
555 if (*flags & USE_INTL_CURRENCY)
556 right_prec = lc->int_frac_digits;
558 right_prec = lc->frac_digits;
560 if (right_prec == CHAR_MAX) /* POSIX locale ? */
564 if (*flags & NEED_GROUPING)
565 left_prec += get_groups(left_prec, grouping);
567 /* convert to string */
568 avalue_size = asprintf_l(&avalue, loc, "%*.*f",
569 left_prec + right_prec + 1, right_prec, value);
573 /* make sure that we've enough space for result string */
574 bufsize = avalue_size * (1 + thousands_sep_size) + decimal_point_size +
576 rslt = calloc(1, bufsize);
581 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
583 /* skip spaces at beginning */
585 while (avalue[padded] == ' ') {
590 if (right_prec > 0) {
591 bufend -= right_prec;
592 memcpy(bufend, avalue + avalue_size + padded - right_prec,
594 bufend -= decimal_point_size;
595 memcpy(bufend, decimal_point, decimal_point_size);
596 avalue_size -= (right_prec + 1);
599 if ((*flags & NEED_GROUPING) && thousands_sep_size > 0 &&
600 *grouping != CHAR_MAX && *grouping > 0) {
601 while (avalue_size > (int)*grouping) {
606 /* no more grouping ? */
607 if (*grouping == CHAR_MAX)
610 /* rest grouping with same value ? */
611 if (*grouping == 0) {
613 while (avalue_size > *grouping) {
619 if (avalue_size != 0)
623 bufend -= avalue_size;
624 memcpy(bufend, avalue + padded, avalue_size);
625 /* decrease assumed $decimal_point */
627 padded -= decimal_point_size;
630 /* do padding with pad_char */
633 memset(bufend, pad_char, padded);
636 bufsize = rslt + bufsize - bufend;
637 memmove(rslt, bufend, bufsize);
643 strfmon(char *restrict s, size_t maxsize, const char *restrict format, ...)
648 va_start(ap, format);
649 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
656 strfmon_l(char *restrict s, size_t maxsize, locale_t loc,
657 const char *restrict format, ...)
662 va_start(ap, format);
663 ret = vstrfmon_l(s, maxsize, loc, format, ap);