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