git: 2f427cbb3557 - stable/14 - strfmon: style fixes
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sat, 16 Dec 2023 04:18:36 UTC
The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=2f427cbb3557fbcf19da6ecf0f5dfd14105eff2e commit 2f427cbb3557fbcf19da6ecf0f5dfd14105eff2e Author: Jose Luis Duran <jlduran@gmail.com> AuthorDate: 2023-11-30 23:28:21 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2023-12-16 04:17:51 +0000 strfmon: style fixes (cherry picked from commit 56a0d5444d6f39302f3476b61c1b81ed39abe589) --- lib/libc/stdlib/strfmon.c | 197 +++++++++++++++++++++++----------------------- 1 file changed, 99 insertions(+), 98 deletions(-) diff --git a/lib/libc/stdlib/strfmon.c b/lib/libc/stdlib/strfmon.c index ade1deaffca9..4b0389345eb6 100644 --- a/lib/libc/stdlib/strfmon.c +++ b/lib/libc/stdlib/strfmon.c @@ -33,6 +33,7 @@ */ #include <sys/types.h> + #include <ctype.h> #include <errno.h> #include <limits.h> @@ -57,7 +58,7 @@ /* internal macros */ #define PRINT(CH) do { \ - if (dst >= s + maxsize) \ + if (dst >= s + maxsize) \ goto e2big_error; \ *dst++ = CH; \ } while (0) @@ -68,9 +69,9 @@ PRINT(*tmps++); \ } while (0) -#define GET_NUMBER(VAR, LOC) do { \ +#define GET_NUMBER(VAR, LOC) do { \ VAR = 0; \ - while (isdigit_l((unsigned char)*fmt, LOC)) { \ + while (isdigit_l((unsigned char)*fmt, LOC)) { \ if (VAR > INT_MAX / 10) \ goto e2big_error; \ VAR *= 10; \ @@ -101,22 +102,22 @@ static char *__format_grouped_double(double, int *, int, int, int, struct lconv *, locale_t); static ssize_t -vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, - const char * __restrict format, va_list ap) +vstrfmon_l(char *__restrict s, size_t maxsize, locale_t loc, + const char *__restrict format, va_list ap) { char *dst; /* output destination pointer */ const char *fmt; /* current format poistion pointer */ struct lconv *lc; /* pointer to lconv structure */ char *asciivalue; /* formatted double pointer */ - int flags; /* formatting options */ - int pad_char; /* padding character */ - int pad_size; /* pad size */ - int width; /* field width */ - int left_prec; /* left precision */ - int right_prec; /* right precision */ - double value; /* just value */ - char space_char = ' '; /* space after currency */ + int flags; /* formatting options */ + int pad_char; /* padding character */ + int pad_size; /* pad size */ + int width; /* field width */ + int left_prec; /* left precision */ + int right_prec; /* right precision */ + double value; /* just value */ + char space_char = ' '; /* space after currency */ char cs_precedes, /* values gathered from struct lconv */ sep_by_space, @@ -126,6 +127,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, char *tmpptr; /* temporary vars */ int sverrno; + FIX_LOCALE(loc); lc = localeconv_l(loc); @@ -134,7 +136,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, asciivalue = NULL; currency_symbol = NULL; - while (*fmt) { + while (*fmt != 0) { /* pass nonformating characters AS IS */ if (*fmt != '%') goto literal; @@ -144,13 +146,13 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, /* "%%" mean just '%' */ if (*(fmt + 1) == '%') { fmt++; - literal: +literal: PRINT(*fmt++); continue; } /* set up initial values */ - flags = (NEED_GROUPING|LOCALE_POSN); + flags = NEED_GROUPING | LOCALE_POSN; pad_char = ' '; /* padding character is "space" */ pad_size = 0; /* no padding initially */ left_prec = -1; /* no left precision specified */ @@ -160,32 +162,32 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, /* Flags */ while (1) { switch (*++fmt) { - case '=': /* fill character */ - pad_char = *++fmt; - if (pad_char == '\0') - goto format_error; - continue; - case '^': /* not group currency */ - flags &= ~(NEED_GROUPING); - continue; - case '+': /* use locale defined signs */ - if (flags & SIGN_POSN_USED) - goto format_error; - flags |= (SIGN_POSN_USED|LOCALE_POSN); - continue; - case '(': /* enclose negatives with () */ - if (flags & SIGN_POSN_USED) - goto format_error; - flags |= (SIGN_POSN_USED|PARENTH_POSN); - continue; - case '!': /* suppress currency symbol */ - flags |= SUPPRESS_CURR_SYMBOL; - continue; - case '-': /* alignment (left) */ - flags |= LEFT_JUSTIFY; - continue; - default: - break; + case '=': /* fill character */ + pad_char = *++fmt; + if (pad_char == '\0') + goto format_error; + continue; + case '^': /* not group currency */ + flags &= ~(NEED_GROUPING); + continue; + case '+': /* use locale defined signs */ + if (flags & SIGN_POSN_USED) + goto format_error; + flags |= (SIGN_POSN_USED | LOCALE_POSN); + continue; + case '(': /* enclose negatives with () */ + if (flags & SIGN_POSN_USED) + goto format_error; + flags |= (SIGN_POSN_USED | PARENTH_POSN); + continue; + case '!': /* suppress currency symbol */ + flags |= SUPPRESS_CURR_SYMBOL; + continue; + case '-': /* alignment (left) */ + flags |= LEFT_JUSTIFY; + continue; + default: + break; } break; } @@ -193,7 +195,8 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, /* field Width */ if (isdigit_l((unsigned char)*fmt, loc)) { GET_NUMBER(width, loc); - /* Do we have enough space to put number with + /* + * Do we have enough space to put number with * required width ? */ if ((unsigned int)width >= maxsize - (dst - s)) @@ -214,22 +217,24 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, if (!isdigit_l((unsigned char)*++fmt, loc)) goto format_error; GET_NUMBER(right_prec, loc); - if ((unsigned int)right_prec >= maxsize - (dst - s) - - left_prec) + if ((unsigned int)right_prec >= + maxsize - (dst - s) - left_prec) goto e2big_error; } /* Conversion Characters */ switch (*fmt++) { - case 'i': /* use international currency format */ - flags |= USE_INTL_CURRENCY; - break; - case 'n': /* use national currency format */ - flags &= ~(USE_INTL_CURRENCY); - break; - default: /* required character is missing or - premature EOS */ - goto format_error; + case 'i': /* use international currency format */ + flags |= USE_INTL_CURRENCY; + break; + case 'n': /* use national currency format */ + flags &= ~(USE_INTL_CURRENCY); + break; + default: /* + * required character is missing or + * premature EOS + */ + goto format_error; } if (currency_symbol != NULL) @@ -245,7 +250,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, currency_symbol = strdup(lc->currency_symbol); if (currency_symbol == NULL) - goto end_error; /* ENOMEM. */ + goto end_error; /* ENOMEM. */ /* value itself */ value = va_arg(ap, double); @@ -267,15 +272,17 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, if (asciivalue != NULL) free(asciivalue); - asciivalue = __format_grouped_double(value, &flags, - left_prec, right_prec, pad_char, lc, loc); + asciivalue = __format_grouped_double(value, &flags, left_prec, + right_prec, pad_char, lc, loc); if (asciivalue == NULL) - goto end_error; /* errno already set */ - /* to ENOMEM by malloc() */ + goto end_error; /* + * errno already set to ENOMEM by + * malloc() + */ /* set some variables for later use */ - __setup_vars(flags, &cs_precedes, &sep_by_space, - &sign_posn, &signstr, lc); + __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, + &signstr, lc); /* * Description of some LC_MONETARY's values: @@ -306,7 +313,6 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, * $currency_symbol * = 3 - the sign string precedes the $currency_symbol * = 4 - the sign string succeeds the $currency_symbol - * */ tmpptr = dst; @@ -352,12 +358,10 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, } if (!(flags & SUPPRESS_CURR_SYMBOL)) { - if ((sign_posn == 3 && sep_by_space == 2) - || (sep_by_space == 1 - && (sign_posn == 0 - || sign_posn == 1 - || sign_posn == 2 - || sign_posn == 4))) + if ((sign_posn == 3 && sep_by_space == 2) || + (sep_by_space == 1 && + (sign_posn == 0 || sign_posn == 1 || + sign_posn == 2 || sign_posn == 4))) PRINT(space_char); PRINTS(currency_symbol); if (sign_posn == 4) { @@ -398,7 +402,7 @@ vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc, PRINT('\0'); free(asciivalue); free(currency_symbol); - return (dst - s - 1); /* return size of put data except trailing '\0' */ + return (dst - s - 1); /* size of put data except trailing '\0' */ e2big_error: errno = E2BIG; @@ -418,15 +422,15 @@ end_error: } static void -__setup_vars(int flags, char *cs_precedes, char *sep_by_space, - char *sign_posn, char **signstr, struct lconv *lc) +__setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn, + char **signstr, struct lconv *lc) { if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { *cs_precedes = lc->int_n_cs_precedes; *sep_by_space = lc->int_n_sep_by_space; *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; - *signstr = (lc->negative_sign[0] == '\0') ? "-" - : lc->negative_sign; + *signstr = (lc->negative_sign[0] == '\0') ? "-" : + lc->negative_sign; } else if (flags & USE_INTL_CURRENCY) { *cs_precedes = lc->int_p_cs_precedes; *sep_by_space = lc->int_p_sep_by_space; @@ -436,8 +440,8 @@ __setup_vars(int flags, char *cs_precedes, char *sep_by_space, *cs_precedes = lc->n_cs_precedes; *sep_by_space = lc->n_sep_by_space; *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; - *signstr = (lc->negative_sign[0] == '\0') ? "-" - : lc->negative_sign; + *signstr = (lc->negative_sign[0] == '\0') ? "-" : + lc->negative_sign; } else { *cs_precedes = lc->p_cs_precedes; *sep_by_space = lc->p_sep_by_space; @@ -460,8 +464,8 @@ __calc_left_pad(int flags, char *cur_symb, struct lconv *lc) char cs_precedes, sep_by_space, sign_posn, *signstr; int left_chars = 0; - __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, - &signstr, lc); + __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr, + lc); if (cs_precedes != 0) { left_chars += strlen(cur_symb); @@ -470,17 +474,17 @@ __calc_left_pad(int flags, char *cur_symb, struct lconv *lc) } switch (sign_posn) { - case 0: - if (flags & IS_NEGATIVE) - left_chars++; - break; - case 1: + case 0: + if (flags & IS_NEGATIVE) + left_chars++; + break; + case 1: + left_chars += strlen(signstr); + break; + case 3: + case 4: + if (cs_precedes != 0) left_chars += strlen(signstr); - break; - case 3: - case 4: - if (cs_precedes != 0) - left_chars += strlen(signstr); } return (left_chars); } @@ -510,8 +514,8 @@ get_groups(int size, char *grouping) /* convert double to locale-encoded string */ static char * -__format_grouped_double(double value, int *flags, - int left_prec, int right_prec, int pad_char, struct lconv *lc, locale_t loc) +__format_grouped_double(double value, int *flags, int left_prec, int right_prec, + int pad_char, struct lconv *lc, locale_t loc) { char *rslt; @@ -529,7 +533,7 @@ __format_grouped_double(double value, int *flags, size_t decimal_point_size; size_t thousands_sep_size; - int groups = 0; + int groups = 0; grouping = lc->mon_grouping; decimal_point = lc->mon_decimal_point; @@ -592,10 +596,8 @@ __format_grouped_double(double value, int *flags, avalue_size -= (right_prec + 1); } - if ((*flags & NEED_GROUPING) && - thousands_sep_size > 0 && - *grouping != CHAR_MAX && - *grouping > 0) { + if ((*flags & NEED_GROUPING) && thousands_sep_size > 0 && + *grouping != CHAR_MAX && *grouping > 0) { while (avalue_size > (int)*grouping) { GRPCPY(*grouping); GRPSEP; @@ -638,8 +640,7 @@ __format_grouped_double(double value, int *flags, } ssize_t -strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, - ...) +strfmon(char *__restrict s, size_t maxsize, const char *__restrict format, ...) { ssize_t ret; va_list ap; @@ -652,8 +653,8 @@ strfmon(char * __restrict s, size_t maxsize, const char * __restrict format, } ssize_t -strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, - const char * __restrict format, ...) +strfmon_l(char *__restrict s, size_t maxsize, locale_t loc, + const char *__restrict format, ...) { ssize_t ret; va_list ap;