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