int printf_size (FILE *fp, const struct printf_info *info, const void *const *args) { /* Units for the both formats. */ #define BINARY_UNITS " kmgtpezy" #define DECIMAL_UNITS " KMGTPEZY" static const char units[2][sizeof (BINARY_UNITS)] = { BINARY_UNITS, /* For binary format. */ DECIMAL_UNITS /* For decimal format. */ }; const char *tag = units[isupper (info->spec) != 0]; int divisor = isupper (info->spec) ? 1000 : 1024; /* The floating-point value to output. */ union { union ieee754_double dbl; union ieee854_long_double ldbl; } fpnum; const void *ptr = &fpnum; int negative = 0; /* "NaN" or "Inf" for the special cases. */ const char *special = NULL; const wchar_t *wspecial = NULL; struct printf_info fp_info; int done = 0; int wide = info->wide; /* Fetch the argument value. */ #ifndef __NO_LONG_DOUBLE_MATH if (info->is_long_double && sizeof (long double) > sizeof (double)) { fpnum.ldbl.d = *(const long double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnanl (fpnum.ldbl.d)) { special = "nan"; wspecial = L"nan"; negative = 0; } else if (__isinfl (fpnum.ldbl.d)) { special = "inf"; wspecial = L"inf"; negative = fpnum.ldbl.d < 0; } else while (fpnum.ldbl.d >= divisor && tag[1] != '\0') { fpnum.ldbl.d /= divisor; ++tag; } } else #endif /* no long double */ { fpnum.dbl.d = *(const double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnan (fpnum.dbl.d)) { special = "nan"; wspecial = L"nan"; negative = 0; } else if (__isinf (fpnum.dbl.d)) { special = "inf"; wspecial = L"inf"; negative = fpnum.dbl.d < 0; } else while (fpnum.dbl.d >= divisor && tag[1] != '\0') { fpnum.dbl.d /= divisor; ++tag; } } if (special) { int width = info->prec > width ? info->prec : info->width; if (negative || info->showsign || info->space) --width; width -= 3; if (!info->left && width > 0) PADN (' ', width); if (negative) outchar ('-'); else if (info->showsign) outchar ('+'); else if (info->space) outchar (' '); PRINT (special, wspecial, 3); if (info->left && width > 0) PADN (' ', width); return done; } /* Prepare to print the number. We want to use `__printf_fp' so we have to prepare a `printf_info' structure. */ fp_info.spec = 'f'; fp_info.prec = info->prec < 0 ? 3 : info->prec; fp_info.is_long_double = info->is_long_double; fp_info.is_short = info->is_short; fp_info.is_long = info->is_long; fp_info.alt = info->alt; fp_info.space = info->space; fp_info.left = info->left; fp_info.showsign = info->showsign; fp_info.group = info->group; fp_info.extra = info->extra; fp_info.pad = info->pad; fp_info.wide = wide; if (fp_info.left && fp_info.pad == L' ') { /* We must do the padding ourself since the unit character must be placed before the padding spaces. */ fp_info.width = 0; done = __printf_fp (fp, &fp_info, &ptr); if (done > 0) { outchar (*tag); if (info->width > done) PADN (' ', info->width - done); } } else { /* We can let __printf_fp do all the printing and just add our unit character afterwards. */ fp_info.width = info->width - 1; done = __printf_fp (fp, &fp_info, &ptr); if (done > 0) outchar (*tag); } return done; }
int __printf_size (FILE *fp, const struct printf_info *info, const void *const *args) { /* Units for the both formats. */ #define BINARY_UNITS " kmgtpezy" #define DECIMAL_UNITS " KMGTPEZY" static const char units[2][sizeof (BINARY_UNITS)] = { BINARY_UNITS, /* For binary format. */ DECIMAL_UNITS /* For decimal format. */ }; const char *tag = units[isupper (info->spec) != 0]; int divisor = isupper (info->spec) ? 1000 : 1024; /* The floating-point value to output. */ union { union ieee754_double dbl; union ieee854_long_double ldbl; } fpnum; const void *ptr = &fpnum; int fpnum_sign = 0; /* "NaN" or "Inf" for the special cases. */ const char *special = NULL; const wchar_t *wspecial = NULL; struct printf_info fp_info; int done = 0; #if __OPTION_POSIX_C_LANG_WIDE_CHAR int wide = info->wide; #else /* This should never be called on a wide-oriented stream when OPTION_POSIX_C_LANG_WIDE_CHAR is disabled, but the compiler can't be trusted to figure that out. */ const int wide = 0; #endif int res; /* Fetch the argument value. */ #ifndef __NO_LONG_DOUBLE_MATH if (info->is_long_double && sizeof (long double) > sizeof (double)) { fpnum.ldbl.d = *(const long double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnanl (fpnum.ldbl.d)) { special = "nan"; wspecial = L"nan"; // fpnum_sign = 0; Already zero } else if ((res = __isinfl (fpnum.ldbl.d))) { fpnum_sign = res; special = "inf"; wspecial = L"inf"; } else while (fpnum.ldbl.d >= divisor && tag[1] != '\0') { fpnum.ldbl.d /= divisor; ++tag; } } else #endif /* no long double */ { fpnum.dbl.d = *(const double *) args[0]; /* Check for special values: not a number or infinity. */ if (__isnan (fpnum.dbl.d)) { special = "nan"; wspecial = L"nan"; // fpnum_sign = 0; Already zero } else if ((res = __isinf (fpnum.dbl.d))) { fpnum_sign = res; special = "inf"; wspecial = L"inf"; } else while (fpnum.dbl.d >= divisor && tag[1] != '\0') { fpnum.dbl.d /= divisor; ++tag; } } if (special) { int width = info->prec > info->width ? info->prec : info->width; if (fpnum_sign < 0 || info->showsign || info->space) --width; width -= 3; if (!info->left && width > 0) PADN (' ', width); if (fpnum_sign < 0) outchar ('-'); else if (info->showsign) outchar ('+'); else if (info->space) outchar (' '); PRINT (special, wspecial, 3); if (info->left && width > 0) PADN (' ', width); return done; } /* Prepare to print the number. We want to use `__printf_fp' so we have to prepare a `printf_info' structure. */ fp_info = *info; fp_info.spec = 'f'; fp_info.prec = info->prec < 0 ? 3 : info->prec; fp_info.wide = wide; if (fp_info.left && fp_info.pad == L' ') { /* We must do the padding ourself since the unit character must be placed before the padding spaces. */ fp_info.width = 0; done = __printf_fp (fp, &fp_info, &ptr); if (done > 0) { outchar (*tag); if (info->width > done) PADN (' ', info->width - done); } } else { /* We can let __printf_fp do all the printing and just add our unit character afterwards. */ fp_info.width = info->width - 1; done = __printf_fp (fp, &fp_info, &ptr); if (done > 0) outchar (*tag); } return done; }