Implement strfmon_l

Use latest code from FreeBSD

Signed-off by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2016-07-25 11:26:08 +02:00
parent 542b970d4e
commit 765d2c0bac
3 changed files with 64 additions and 36 deletions

View File

@ -1344,6 +1344,7 @@ strdup SIGFE
strerror SIGFE strerror SIGFE
strerror_r SIGFE strerror_r SIGFE
strfmon SIGFE strfmon SIGFE
strfmon_l SIGFE
strftime SIGFE strftime SIGFE
strftime_l SIGFE strftime_l SIGFE
strlcat NOSIGFE strlcat NOSIGFE

View File

@ -2,6 +2,11 @@
* Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
* All rights reserved. * All rights reserved.
* *
* Copyright (c) 2011 The FreeBSD Foundation
* All rights reserved.
* Portions of this software were developed by David Chisnall
* under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@ -25,12 +30,10 @@
* *
*/ */
#if 0 #define _GNU_SOURCE
__FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.19 2008/04/24 07:49:00 ru Exp $");
#endif
#include "winsup.h"
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
@ -41,13 +44,21 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.19 2008/04/24 07:49:00 ru
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef __CYGWIN__
#include "xlocale_private.h"
#else
#include "../locale/setlocale.h"
#define __get_locale() __get_current_locale()
#define FIX_LOCALE(__l)
#define localeconv_l __localeconv_l
#endif
/* internal flags */ /* internal flags */
#define NEED_GROUPING 0x01 /* print digits grouped (default) */ #define NEED_GROUPING 0x01 /* print digits grouped (default) */
#define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
#define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
#define PARENTH_POSN 0x08 /* enclose negative amount in () */ #define PARENTH_POSN 0x08 /* enclose negative amount in () */
#define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ #define SUPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */
#define LEFT_JUSTIFY 0x20 /* left justify */ #define LEFT_JUSTIFY 0x20 /* left justify */
#define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
#define IS_NEGATIVE 0x80 /* is argument value negative ? */ #define IS_NEGATIVE 0x80 /* is argument value negative ? */
@ -95,11 +106,10 @@ static void __setup_vars(int, char *, char *, char *, char **);
static int __calc_left_pad(int, char *); static int __calc_left_pad(int, char *);
static char *__format_grouped_double(double, int *, int, int, int); static char *__format_grouped_double(double, int *, int, int, int);
ssize_t static ssize_t
strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
...) const char * __restrict format, va_list ap)
{ {
va_list ap;
char *dst; /* output destination pointer */ char *dst; /* output destination pointer */
const char *fmt; /* current format poistion pointer */ const char *fmt; /* current format poistion pointer */
struct lconv *lc; /* pointer to lconv structure */ struct lconv *lc; /* pointer to lconv structure */
@ -122,10 +132,10 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
char *tmpptr; /* temporary vars */ char *tmpptr; /* temporary vars */
int sverrno; int sverrno;
FIX_LOCALE(loc);
va_start(ap, format);
lc = localeconv(); lc = localeconv_l(loc);
dst = s; dst = s;
fmt = format; fmt = format;
asciivalue = NULL; asciivalue = NULL;
@ -234,10 +244,8 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
free(currency_symbol); free(currency_symbol);
if (flags & USE_INTL_CURRENCY) { if (flags & USE_INTL_CURRENCY) {
currency_symbol = strdup(lc->int_curr_symbol); currency_symbol = strdup(lc->int_curr_symbol);
if (currency_symbol != NULL) { if (currency_symbol != NULL)
space_char = *(currency_symbol+3); space_char = *(currency_symbol+3);
*(currency_symbol+3) = '\0';
}
} else } else
currency_symbol = strdup(lc->currency_symbol); currency_symbol = strdup(lc->currency_symbol);
@ -296,9 +304,9 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
* *
* = 0 - parentheses enclose the quantity and the * = 0 - parentheses enclose the quantity and the
* $currency_symbol * $currency_symbol
* = 1 - the sign string precedes the quantity and the * = 1 - the sign string precedes the quantity and the
* $currency_symbol * $currency_symbol
* = 2 - the sign string succeeds the quantity and the * = 2 - the sign string succeeds the quantity and the
* $currency_symbol * $currency_symbol
* = 3 - the sign string precedes the $currency_symbol * = 3 - the sign string precedes the $currency_symbol
* = 4 - the sign string succeeds the $currency_symbol * = 4 - the sign string succeeds the $currency_symbol
@ -385,7 +393,6 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
} }
PRINT('\0'); PRINT('\0');
va_end(ap);
free(asciivalue); free(asciivalue);
free(currency_symbol); free(currency_symbol);
return (dst - s - 1); /* return size of put data except trailing '\0' */ return (dst - s - 1); /* return size of put data except trailing '\0' */
@ -404,39 +411,61 @@ end_error:
if (currency_symbol != NULL) if (currency_symbol != NULL)
free(currency_symbol); free(currency_symbol);
errno = sverrno; errno = sverrno;
va_end(ap);
return (-1); return (-1);
} }
ssize_t
strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format,
...)
{
size_t ret;
va_list ap;
va_start(ap, format);
ret = vstrfmon_l(s, maxsize, loc, format, ap);
va_end(ap);
return ret;
}
ssize_t
strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
...)
{
size_t ret;
va_list ap;
va_start(ap, format);
ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
va_end(ap);
return ret;
}
static void static void
__setup_vars(int flags, char *cs_precedes, char *sep_by_space, __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
char *sign_posn, char **signstr) { char *sign_posn, char **signstr) {
struct lconv *lc = localeconv(); struct lconv *lc = localeconv();
static char negative[] = "-";
if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
*cs_precedes = lc->int_n_cs_precedes; *cs_precedes = lc->int_n_cs_precedes;
*sep_by_space = lc->int_n_sep_by_space; *sep_by_space = lc->int_n_sep_by_space;
*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; *sign_posn = (char) ((flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn);
*signstr = (*lc->negative_sign == '\0') ? negative *signstr = (char *) ((lc->negative_sign[0] == '\0') ? "-"
: lc->negative_sign; : lc->negative_sign);
} else if (flags & USE_INTL_CURRENCY) { } else if (flags & USE_INTL_CURRENCY) {
*cs_precedes = lc->int_p_cs_precedes; *cs_precedes = lc->int_p_cs_precedes;
*sep_by_space = lc->int_p_sep_by_space; *sep_by_space = lc->int_p_sep_by_space;
*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; *sign_posn = (char) ((flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn);
*signstr = lc->positive_sign; *signstr = lc->positive_sign;
} else if (flags & IS_NEGATIVE) { } else if (flags & IS_NEGATIVE) {
*cs_precedes = lc->n_cs_precedes; *cs_precedes = lc->n_cs_precedes;
*sep_by_space = lc->n_sep_by_space; *sep_by_space = lc->n_sep_by_space;
*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; *sign_posn = (char) ((flags & PARENTH_POSN) ? 0 : lc->n_sign_posn);
*signstr = (*lc->negative_sign == '\0') ? negative *signstr = (char *) ((lc->negative_sign[0] == '\0') ? "-"
: lc->negative_sign; : lc->negative_sign);
} else { } else {
*cs_precedes = lc->p_cs_precedes; *cs_precedes = lc->p_cs_precedes;
*sep_by_space = lc->p_sep_by_space; *sep_by_space = lc->p_sep_by_space;
*sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; *sign_posn = (char) ((flags & PARENTH_POSN) ? 0 : lc->p_sign_posn);
*signstr = lc->positive_sign; *signstr = (char *) lc->positive_sign;
} }
/* Set defult values for unspecified information. */ /* Set defult values for unspecified information. */
@ -505,7 +534,6 @@ __format_grouped_double(double value, int *flags,
char *rslt; char *rslt;
char *avalue; char *avalue;
int avalue_size; int avalue_size;
char fmt[32];
size_t bufsize; size_t bufsize;
char *bufend; char *bufend;
@ -546,14 +574,13 @@ __format_grouped_double(double value, int *flags,
left_prec += get_groups(left_prec, grouping); left_prec += get_groups(left_prec, grouping);
/* convert to string */ /* convert to string */
snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
right_prec); right_prec, value);
avalue_size = asprintf(&avalue, fmt, value);
if (avalue_size < 0) if (avalue_size < 0)
return (NULL); return (NULL);
/* make sure that we've enough space for result string */ /* make sure that we've enough space for result string */
bufsize = strlen(avalue)*2+1; bufsize = avalue_size * 2 + 1;
rslt = calloc(1, bufsize); rslt = calloc(1, bufsize);
if (rslt == NULL) { if (rslt == NULL) {
free(avalue); free(avalue);
@ -561,7 +588,7 @@ __format_grouped_double(double value, int *flags,
} }
bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
/* skip spaces at beggining */ /* skip spaces at beginning */
padded = 0; padded = 0;
while (avalue[padded] == ' ') { while (avalue[padded] == ' ') {
padded++; padded++;

View File

@ -925,6 +925,7 @@ also IEEE Std 1003.1-2008 (POSIX.1-2008).</para>
strerror strerror
strerror_r strerror_r
strfmon strfmon
strfmon_l
strftime strftime
strftime_l strftime_l
strlen strlen
@ -1548,7 +1549,6 @@ also IEEE Std 1003.1-2008 (POSIX.1-2008).</para>
putmsg putmsg
setnetent setnetent
sigtimedwait sigtimedwait
strfmon_l
timer_getoverrun timer_getoverrun
ulimit ulimit
waitid waitid