Update.
[glibc.git] / stdio-common / printf_size.c
blob34581067dcb398f22010310967e8a56a2e9dd14a
1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997, 1998 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 Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <ctype.h>
23 #include <ieee754.h>
24 #include <math.h>
25 #include <printf.h>
26 #ifdef USE_IN_LIBIO
27 # include <libioP.h>
28 #else
29 # include <stdio.h>
30 #endif
33 /* This defines make it possible to use the same code for GNU C library and
34 the GNU I/O library. */
35 #ifdef USE_IN_LIBIO
36 # define PUT(f, s, n) _IO_sputn (f, s, n)
37 # define PAD(f, c, n) _IO_padn (f, c, n)
38 /* We use this file GNU C library and GNU I/O library. So make
39 names equal. */
40 # undef putc
41 # define putc(c, f) _IO_putc_unlocked (c, f)
42 # define size_t _IO_size_t
43 # define FILE _IO_FILE
44 #else /* ! USE_IN_LIBIO */
45 # define PUT(f, s, n) fwrite (s, 1, n, f)
46 # define PAD(f, c, n) __printf_pad (f, c, n)
47 ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
48 #endif /* USE_IN_LIBIO */
50 /* Macros for doing the actual output. */
52 #define outchar(ch) \
53 do \
54 { \
55 register const int outc = (ch); \
56 if (putc (outc, fp) == EOF) \
57 return -1; \
58 ++done; \
59 } while (0)
61 #define PRINT(ptr, len) \
62 do \
63 { \
64 register size_t outlen = (len); \
65 if (len > 20) \
66 { \
67 if (PUT (fp, ptr, outlen) != outlen) \
68 return -1; \
69 ptr += outlen; \
70 done += outlen; \
71 } \
72 else \
73 { \
74 while (outlen-- > 0) \
75 outchar (*ptr++); \
76 } \
77 } while (0)
79 #define PADN(ch, len) \
80 do \
81 { \
82 if (PAD (fp, ch, len) != len) \
83 return -1; \
84 done += len; \
85 } \
86 while (0)
88 /* Prototype for helper functions. */
89 extern int __printf_fp (FILE *fp, const struct printf_info *info,
90 const void *const *args);
93 int
94 printf_size (FILE *fp, const struct printf_info *info, const void *const *args)
96 /* Units for the both formats. */
97 #define BINARY_UNITS " kmgtpezy"
98 #define DECIMAL_UNITS " KMGTPEZY"
99 static const char units[2][sizeof (BINARY_UNITS)] =
101 BINARY_UNITS, /* For binary format. */
102 DECIMAL_UNITS /* For decimal format. */
104 const char *tag = units[isupper (info->spec) != 0];
105 int divisor = isupper (info->spec) ? 1000 : 1024;
107 /* The floating-point value to output. */
108 union
110 union ieee754_double dbl;
111 union ieee854_long_double ldbl;
113 fpnum;
114 const void *ptr = &fpnum;
116 int negative = 0;
118 /* "NaN" or "Inf" for the special cases. */
119 const char *special = NULL;
121 struct printf_info fp_info;
122 int done = 0;
125 /* Fetch the argument value. */
126 #ifndef __NO_LONG_DOUBLE_MATH
127 if (info->is_long_double && sizeof (long double) > sizeof (double))
129 fpnum.ldbl.d = *(const long double *) args[0];
131 /* Check for special values: not a number or infinity. */
132 if (__isnanl (fpnum.ldbl.d))
134 special = "nan";
135 negative = 0;
137 else if (__isinfl (fpnum.ldbl.d))
139 special = "inf";
141 negative = fpnum.ldbl.d < 0;
143 else
144 while (fpnum.ldbl.d >= divisor && tag[1] != '\0')
146 fpnum.ldbl.d /= divisor;
147 ++tag;
150 else
151 #endif /* no long double */
153 fpnum.dbl.d = *(const double *) args[0];
155 /* Check for special values: not a number or infinity. */
156 if (__isnan (fpnum.dbl.d))
158 special = "nan";
159 negative = 0;
161 else if (__isinf (fpnum.dbl.d))
163 special = "inf";
165 negative = fpnum.dbl.d < 0;
167 else
168 while (fpnum.dbl.d >= divisor && tag[1] != '\0')
170 fpnum.dbl.d /= divisor;
171 ++tag;
175 if (special)
177 int width = info->prec > info->width ? info->prec : info->width;
179 if (negative || info->showsign || info->space)
180 --width;
181 width -= 3;
183 if (!info->left && width > 0)
184 PADN (' ', width);
186 if (negative)
187 outchar ('-');
188 else if (info->showsign)
189 outchar ('+');
190 else if (info->space)
191 outchar (' ');
193 PRINT (special, 3);
195 if (info->left && width > 0)
196 PADN (' ', width);
198 return done;
201 /* Prepare to print the number. We want to use `__printf_fp' so we
202 have to prepare a `printf_info' structure. */
203 fp_info.spec = 'f';
204 fp_info.prec = info->prec < 0 ? 3 : info->prec;
205 fp_info.is_long_double = info->is_long_double;
206 fp_info.is_short = info->is_short;
207 fp_info.is_long = info->is_long;
208 fp_info.alt = info->alt;
209 fp_info.space = info->space;
210 fp_info.left = info->left;
211 fp_info.showsign = info->showsign;
212 fp_info.group = info->group;
213 fp_info.extra = info->extra;
214 fp_info.pad = info->pad;
216 if (fp_info.left && fp_info.pad == L' ')
218 /* We must do the padding ourself since the unit character must
219 be placed before the padding spaces. */
220 fp_info.width = 0;
222 done = __printf_fp (fp, &fp_info, &ptr);
223 if (done > 0)
225 outchar (*tag);
226 if (info->width > done)
227 PADN (' ', info->width - done);
230 else
232 /* We can let __printf_fp do all the printing and just add our
233 unit character afterwards. */
234 fp_info.width = info->width - 1;
236 done = __printf_fp (fp, &fp_info, &ptr);
237 if (done > 0)
238 outchar (*tag);
241 return done;
244 /* This is the function used by `vfprintf' to determine number and
245 type of the arguments. */
247 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
249 /* We need only one double or long double argument. */
250 if (n >= 1)
251 argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
253 return 1;