1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 Based on a proposal by Larry McVoy <lm@sgi.com>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 /* This defines make it possible to use the same code for GNU C library and
34 the GNU I/O library. */
36 # define PUT(f, s, n) _IO_sputn (f, s, n)
37 # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
38 /* We use this file GNU C library and GNU I/O library. So make
41 # define putc(c, f) (wide \
42 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
43 # define size_t _IO_size_t
44 # define FILE _IO_FILE
45 #else /* ! USE_IN_LIBIO */
46 # define PUT(f, s, n) fwrite (s, 1, n, f)
47 # define PAD(f, c, n) __printf_pad (f, c, n)
48 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
49 #endif /* USE_IN_LIBIO */
51 /* Macros for doing the actual output. */
56 register const int outc = (ch); \
57 if (putc (outc, fp) == EOF) \
62 #define PRINT(ptr, wptr, len) \
65 register size_t outlen = (len); \
68 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
76 while (outlen-- > 0) \
79 while (outlen-- > 0) \
84 #define PADN(ch, len) \
87 if (PAD (fp, ch, len) != len) \
93 /* Prototype for helper functions. */
94 extern int __printf_fp (FILE *fp
, const struct printf_info
*info
,
95 const void *const *args
);
99 printf_size (FILE *fp
, const struct printf_info
*info
, const void *const *args
)
101 /* Units for the both formats. */
102 #define BINARY_UNITS " kmgtpezy"
103 #define DECIMAL_UNITS " KMGTPEZY"
104 static const char units
[2][sizeof (BINARY_UNITS
)] =
106 BINARY_UNITS
, /* For binary format. */
107 DECIMAL_UNITS
/* For decimal format. */
109 const char *tag
= units
[isupper (info
->spec
) != 0];
110 int divisor
= isupper (info
->spec
) ? 1000 : 1024;
112 /* The floating-point value to output. */
115 union ieee754_double dbl
;
116 union ieee854_long_double ldbl
;
119 const void *ptr
= &fpnum
;
123 /* "NaN" or "Inf" for the special cases. */
124 const char *special
= NULL
;
125 const wchar_t *wspecial
= NULL
;
127 struct printf_info fp_info
;
129 int wide
= info
->wide
;
132 /* Fetch the argument value. */
133 #ifndef __NO_LONG_DOUBLE_MATH
134 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
136 fpnum
.ldbl
.d
= *(const long double *) args
[0];
138 /* Check for special values: not a number or infinity. */
139 if (INTUSE(__isnanl
) (fpnum
.ldbl
.d
))
145 else if (INTUSE(__isinfl
) (fpnum
.ldbl
.d
))
150 negative
= fpnum
.ldbl
.d
< 0;
153 while (fpnum
.ldbl
.d
>= divisor
&& tag
[1] != '\0')
155 fpnum
.ldbl
.d
/= divisor
;
160 #endif /* no long double */
162 fpnum
.dbl
.d
= *(const double *) args
[0];
164 /* Check for special values: not a number or infinity. */
165 if (INTUSE(__isnan
) (fpnum
.dbl
.d
))
171 else if (INTUSE(__isinf
) (fpnum
.dbl
.d
))
176 negative
= fpnum
.dbl
.d
< 0;
179 while (fpnum
.dbl
.d
>= divisor
&& tag
[1] != '\0')
181 fpnum
.dbl
.d
/= divisor
;
188 int width
= info
->prec
> width
? info
->prec
: width
;
190 if (negative
|| info
->showsign
|| info
->space
)
194 if (!info
->left
&& width
> 0)
199 else if (info
->showsign
)
201 else if (info
->space
)
204 PRINT (special
, wspecial
, 3);
206 if (info
->left
&& width
> 0)
212 /* Prepare to print the number. We want to use `__printf_fp' so we
213 have to prepare a `printf_info' structure. */
215 fp_info
.prec
= info
->prec
< 0 ? 3 : info
->prec
;
216 fp_info
.is_long_double
= info
->is_long_double
;
217 fp_info
.is_short
= info
->is_short
;
218 fp_info
.is_long
= info
->is_long
;
219 fp_info
.alt
= info
->alt
;
220 fp_info
.space
= info
->space
;
221 fp_info
.left
= info
->left
;
222 fp_info
.showsign
= info
->showsign
;
223 fp_info
.group
= info
->group
;
224 fp_info
.extra
= info
->extra
;
225 fp_info
.pad
= info
->pad
;
228 if (fp_info
.left
&& fp_info
.pad
== L
' ')
230 /* We must do the padding ourself since the unit character must
231 be placed before the padding spaces. */
234 done
= __printf_fp (fp
, &fp_info
, &ptr
);
238 if (info
->width
> done
)
239 PADN (' ', info
->width
- done
);
244 /* We can let __printf_fp do all the printing and just add our
245 unit character afterwards. */
246 fp_info
.width
= info
->width
- 1;
248 done
= __printf_fp (fp
, &fp_info
, &ptr
);
256 /* This is the function used by `vfprintf' to determine number and
257 type of the arguments. */
259 printf_size_info (const struct printf_info
*info
, size_t n
, int *argtypes
)
261 /* We need only one double or long double argument. */
263 argtypes
[0] = PA_DOUBLE
| (info
->is_long_double
? PA_FLAG_LONG_DOUBLE
: 0);