15af32e75SAxel Dörfler /* Print size value using units for orders of magnitude.
25af32e75SAxel Dörfler Copyright (C) 1997,1998,1999,2000,2001,2002 Free Software Foundation, Inc.
35af32e75SAxel Dörfler This file is part of the GNU C Library.
45af32e75SAxel Dörfler Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
55af32e75SAxel Dörfler Based on a proposal by Larry McVoy <lm@sgi.com>.
65af32e75SAxel Dörfler
75af32e75SAxel Dörfler The GNU C Library is free software; you can redistribute it and/or
85af32e75SAxel Dörfler modify it under the terms of the GNU Lesser General Public
95af32e75SAxel Dörfler License as published by the Free Software Foundation; either
105af32e75SAxel Dörfler version 2.1 of the License, or (at your option) any later version.
115af32e75SAxel Dörfler
125af32e75SAxel Dörfler The GNU C Library is distributed in the hope that it will be useful,
135af32e75SAxel Dörfler but WITHOUT ANY WARRANTY; without even the implied warranty of
145af32e75SAxel Dörfler MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
155af32e75SAxel Dörfler Lesser General Public License for more details.
165af32e75SAxel Dörfler
175af32e75SAxel Dörfler You should have received a copy of the GNU Lesser General Public
185af32e75SAxel Dörfler License along with the GNU C Library; if not, write to the Free
195af32e75SAxel Dörfler Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
205af32e75SAxel Dörfler 02111-1307 USA. */
215af32e75SAxel Dörfler
22545259ffSMichael Lotz #include <libioP.h>
23545259ffSMichael Lotz
245af32e75SAxel Dörfler #include <stdio_private.h>
255af32e75SAxel Dörfler #include <ctype.h>
265af32e75SAxel Dörfler #include <ieee754.h>
275af32e75SAxel Dörfler #include <math.h>
285af32e75SAxel Dörfler #include <printf.h>
295af32e75SAxel Dörfler
305af32e75SAxel Dörfler
315af32e75SAxel Dörfler /* This defines make it possible to use the same code for GNU C library and
325af32e75SAxel Dörfler the GNU I/O library. */
335af32e75SAxel Dörfler #ifdef USE_IN_LIBIO
345af32e75SAxel Dörfler # define PUT(f, s, n) _IO_sputn (f, s, n)
355af32e75SAxel Dörfler # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
365af32e75SAxel Dörfler /* We use this file GNU C library and GNU I/O library. So make
375af32e75SAxel Dörfler names equal. */
385af32e75SAxel Dörfler # undef putc
395af32e75SAxel Dörfler # define putc(c, f) (wide \
405af32e75SAxel Dörfler ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
415af32e75SAxel Dörfler # define size_t _IO_size_t
425af32e75SAxel Dörfler # define FILE _IO_FILE
435af32e75SAxel Dörfler #else /* ! USE_IN_LIBIO */
445af32e75SAxel Dörfler # define PUT(f, s, n) fwrite (s, 1, n, f)
455af32e75SAxel Dörfler # define PAD(f, c, n) __printf_pad (f, c, n)
465af32e75SAxel Dörfler ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
475af32e75SAxel Dörfler #endif /* USE_IN_LIBIO */
48*9e419c30SAugustin Cavalier
495af32e75SAxel Dörfler /* Macros for doing the actual output. */
505af32e75SAxel Dörfler
515af32e75SAxel Dörfler #define outchar(ch) \
525af32e75SAxel Dörfler do \
535af32e75SAxel Dörfler { \
545af32e75SAxel Dörfler register const int outc = (ch); \
555af32e75SAxel Dörfler if (putc (outc, fp) == EOF) \
565af32e75SAxel Dörfler return -1; \
575af32e75SAxel Dörfler ++done; \
585af32e75SAxel Dörfler } while (0)
595af32e75SAxel Dörfler
605af32e75SAxel Dörfler #define PRINT(ptr, wptr, len) \
615af32e75SAxel Dörfler do \
625af32e75SAxel Dörfler { \
635af32e75SAxel Dörfler register size_t outlen = (len); \
645af32e75SAxel Dörfler if (len > 20) \
655af32e75SAxel Dörfler { \
665af32e75SAxel Dörfler if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
675af32e75SAxel Dörfler return -1; \
685af32e75SAxel Dörfler ptr += outlen; \
695af32e75SAxel Dörfler done += outlen; \
705af32e75SAxel Dörfler } \
715af32e75SAxel Dörfler else \
725af32e75SAxel Dörfler { \
735af32e75SAxel Dörfler if (wide) \
745af32e75SAxel Dörfler while (outlen-- > 0) \
755af32e75SAxel Dörfler outchar (*wptr++); \
765af32e75SAxel Dörfler else \
775af32e75SAxel Dörfler while (outlen-- > 0) \
785af32e75SAxel Dörfler outchar (*ptr++); \
795af32e75SAxel Dörfler } \
805af32e75SAxel Dörfler } while (0)
815af32e75SAxel Dörfler
825af32e75SAxel Dörfler #define PADN(ch, len) \
835af32e75SAxel Dörfler do \
845af32e75SAxel Dörfler { \
855af32e75SAxel Dörfler if (PAD (fp, ch, len) != len) \
865af32e75SAxel Dörfler return -1; \
875af32e75SAxel Dörfler done += len; \
885af32e75SAxel Dörfler } \
895af32e75SAxel Dörfler while (0)
90*9e419c30SAugustin Cavalier
91*9e419c30SAugustin Cavalier
925af32e75SAxel Dörfler int
printf_size(FILE * fp,const struct printf_info * info,const void * const * args)935af32e75SAxel Dörfler printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
945af32e75SAxel Dörfler {
955af32e75SAxel Dörfler /* Units for the both formats. */
965af32e75SAxel Dörfler #define BINARY_UNITS " kmgtpezy"
975af32e75SAxel Dörfler #define DECIMAL_UNITS " KMGTPEZY"
985af32e75SAxel Dörfler static const char units[2][sizeof (BINARY_UNITS)] =
995af32e75SAxel Dörfler {
1005af32e75SAxel Dörfler BINARY_UNITS, /* For binary format. */
1015af32e75SAxel Dörfler DECIMAL_UNITS /* For decimal format. */
1025af32e75SAxel Dörfler };
1035af32e75SAxel Dörfler const char *tag = units[isupper (info->spec) != 0];
1045af32e75SAxel Dörfler int divisor = isupper (info->spec) ? 1000 : 1024;
1055af32e75SAxel Dörfler
1065af32e75SAxel Dörfler /* The floating-point value to output. */
1075af32e75SAxel Dörfler union
1085af32e75SAxel Dörfler {
1095af32e75SAxel Dörfler union ieee754_double dbl;
1105af32e75SAxel Dörfler union ieee854_long_double ldbl;
1115af32e75SAxel Dörfler }
1125af32e75SAxel Dörfler fpnum;
1135af32e75SAxel Dörfler const void *ptr = &fpnum;
1145af32e75SAxel Dörfler
1155af32e75SAxel Dörfler int negative = 0;
1165af32e75SAxel Dörfler
1175af32e75SAxel Dörfler /* "NaN" or "Inf" for the special cases. */
1185af32e75SAxel Dörfler const char *special = NULL;
1195af32e75SAxel Dörfler const wchar_t *wspecial = NULL;
1205af32e75SAxel Dörfler
1215af32e75SAxel Dörfler struct printf_info fp_info;
1225af32e75SAxel Dörfler int done = 0;
1235af32e75SAxel Dörfler int wide = info->wide;
1245af32e75SAxel Dörfler
1255af32e75SAxel Dörfler
1265af32e75SAxel Dörfler /* Fetch the argument value. */
1275af32e75SAxel Dörfler #ifndef __NO_LONG_DOUBLE_MATH
1285af32e75SAxel Dörfler if (info->is_long_double && sizeof (long double) > sizeof (double))
1295af32e75SAxel Dörfler {
1305af32e75SAxel Dörfler fpnum.ldbl.d = *(const long double *) args[0];
1315af32e75SAxel Dörfler
1325af32e75SAxel Dörfler /* Check for special values: not a number or infinity. */
133*9e419c30SAugustin Cavalier if (isnan (fpnum.ldbl.d))
1345af32e75SAxel Dörfler {
1355af32e75SAxel Dörfler special = "nan";
1365af32e75SAxel Dörfler wspecial = L"nan";
1375af32e75SAxel Dörfler negative = 0;
1385af32e75SAxel Dörfler }
139*9e419c30SAugustin Cavalier else if (isinf (fpnum.ldbl.d))
1405af32e75SAxel Dörfler {
1415af32e75SAxel Dörfler special = "inf";
1425af32e75SAxel Dörfler wspecial = L"inf";
1435af32e75SAxel Dörfler
1445af32e75SAxel Dörfler negative = fpnum.ldbl.d < 0;
1455af32e75SAxel Dörfler }
1465af32e75SAxel Dörfler else
1475af32e75SAxel Dörfler while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
1485af32e75SAxel Dörfler {
1495af32e75SAxel Dörfler fpnum.ldbl.d /= divisor;
1505af32e75SAxel Dörfler ++tag;
1515af32e75SAxel Dörfler }
1525af32e75SAxel Dörfler }
1535af32e75SAxel Dörfler else
1545af32e75SAxel Dörfler #endif /* no long double */
1555af32e75SAxel Dörfler {
1565af32e75SAxel Dörfler fpnum.dbl.d = *(const double *) args[0];
1575af32e75SAxel Dörfler
1585af32e75SAxel Dörfler /* Check for special values: not a number or infinity. */
159*9e419c30SAugustin Cavalier if (isnan (fpnum.dbl.d))
1605af32e75SAxel Dörfler {
1615af32e75SAxel Dörfler special = "nan";
1625af32e75SAxel Dörfler wspecial = L"nan";
1635af32e75SAxel Dörfler negative = 0;
1645af32e75SAxel Dörfler }
165*9e419c30SAugustin Cavalier else if (isinf (fpnum.dbl.d))
1665af32e75SAxel Dörfler {
1675af32e75SAxel Dörfler special = "inf";
1685af32e75SAxel Dörfler wspecial = L"inf";
1695af32e75SAxel Dörfler
1705af32e75SAxel Dörfler negative = fpnum.dbl.d < 0;
1715af32e75SAxel Dörfler }
1725af32e75SAxel Dörfler else
1735af32e75SAxel Dörfler while (fpnum.dbl.d >= divisor && tag[1] != '\0')
1745af32e75SAxel Dörfler {
1755af32e75SAxel Dörfler fpnum.dbl.d /= divisor;
1765af32e75SAxel Dörfler ++tag;
1775af32e75SAxel Dörfler }
1785af32e75SAxel Dörfler }
1795af32e75SAxel Dörfler
1805af32e75SAxel Dörfler if (special)
1815af32e75SAxel Dörfler {
1827766c048Sohnx int width = info->prec > info->width ? info->prec : info->width;
1835af32e75SAxel Dörfler
1845af32e75SAxel Dörfler if (negative || info->showsign || info->space)
1855af32e75SAxel Dörfler --width;
1865af32e75SAxel Dörfler width -= 3;
1875af32e75SAxel Dörfler
1885af32e75SAxel Dörfler if (!info->left && width > 0)
1895af32e75SAxel Dörfler PADN (' ', width);
1905af32e75SAxel Dörfler
1915af32e75SAxel Dörfler if (negative)
1925af32e75SAxel Dörfler outchar ('-');
1935af32e75SAxel Dörfler else if (info->showsign)
1945af32e75SAxel Dörfler outchar ('+');
1955af32e75SAxel Dörfler else if (info->space)
1965af32e75SAxel Dörfler outchar (' ');
1975af32e75SAxel Dörfler
1985af32e75SAxel Dörfler PRINT (special, wspecial, 3);
1995af32e75SAxel Dörfler
2005af32e75SAxel Dörfler if (info->left && width > 0)
2015af32e75SAxel Dörfler PADN (' ', width);
2025af32e75SAxel Dörfler
2035af32e75SAxel Dörfler return done;
2045af32e75SAxel Dörfler }
2055af32e75SAxel Dörfler
2065af32e75SAxel Dörfler /* Prepare to print the number. We want to use `__printf_fp' so we
2075af32e75SAxel Dörfler have to prepare a `printf_info' structure. */
2085af32e75SAxel Dörfler fp_info.spec = 'f';
2095af32e75SAxel Dörfler fp_info.prec = info->prec < 0 ? 3 : info->prec;
2105af32e75SAxel Dörfler fp_info.is_long_double = info->is_long_double;
2115af32e75SAxel Dörfler fp_info.is_short = info->is_short;
2125af32e75SAxel Dörfler fp_info.is_long = info->is_long;
2135af32e75SAxel Dörfler fp_info.alt = info->alt;
2145af32e75SAxel Dörfler fp_info.space = info->space;
2155af32e75SAxel Dörfler fp_info.left = info->left;
2165af32e75SAxel Dörfler fp_info.showsign = info->showsign;
2175af32e75SAxel Dörfler fp_info.group = info->group;
2185af32e75SAxel Dörfler fp_info.extra = info->extra;
2195af32e75SAxel Dörfler fp_info.pad = info->pad;
2205af32e75SAxel Dörfler fp_info.wide = wide;
2215af32e75SAxel Dörfler
2225af32e75SAxel Dörfler if (fp_info.left && fp_info.pad == L' ')
2235af32e75SAxel Dörfler {
2245af32e75SAxel Dörfler /* We must do the padding ourself since the unit character must
2255af32e75SAxel Dörfler be placed before the padding spaces. */
2265af32e75SAxel Dörfler fp_info.width = 0;
2275af32e75SAxel Dörfler
2285af32e75SAxel Dörfler done = __printf_fp (fp, &fp_info, &ptr);
2295af32e75SAxel Dörfler if (done > 0)
2305af32e75SAxel Dörfler {
2315af32e75SAxel Dörfler outchar (*tag);
2325af32e75SAxel Dörfler if (info->width > done)
2335af32e75SAxel Dörfler PADN (' ', info->width - done);
2345af32e75SAxel Dörfler }
2355af32e75SAxel Dörfler }
2365af32e75SAxel Dörfler else
2375af32e75SAxel Dörfler {
2385af32e75SAxel Dörfler /* We can let __printf_fp do all the printing and just add our
2395af32e75SAxel Dörfler unit character afterwards. */
2405af32e75SAxel Dörfler fp_info.width = info->width - 1;
2415af32e75SAxel Dörfler
2425af32e75SAxel Dörfler done = __printf_fp (fp, &fp_info, &ptr);
2435af32e75SAxel Dörfler if (done > 0)
2445af32e75SAxel Dörfler outchar (*tag);
2455af32e75SAxel Dörfler }
2465af32e75SAxel Dörfler
2475af32e75SAxel Dörfler return done;
2485af32e75SAxel Dörfler }
249*9e419c30SAugustin Cavalier
2505af32e75SAxel Dörfler /* This is the function used by `vfprintf' to determine number and
2515af32e75SAxel Dörfler type of the arguments. */
2525af32e75SAxel Dörfler int
printf_size_info(const struct printf_info * info,size_t n,int * argtypes)2535af32e75SAxel Dörfler printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
2545af32e75SAxel Dörfler {
2555af32e75SAxel Dörfler /* We need only one double or long double argument. */
2565af32e75SAxel Dörfler if (n >= 1)
2575af32e75SAxel Dörfler argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
2585af32e75SAxel Dörfler
2595af32e75SAxel Dörfler return 1;
2605af32e75SAxel Dörfler }
261