]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/printf_size.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / stdio-common / printf_size.c
CommitLineData
3e5f5557 1/* Print size value using units for orders of magnitude.
b168057a 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3e5f5557
UD
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>.
6
7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
3e5f5557
UD
11
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
41bdb6e2 15 Lesser General Public License for more details.
3e5f5557 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
3e5f5557
UD
20
21#include <ctype.h>
22#include <ieee754.h>
23#include <math.h>
24#include <printf.h>
14c35863 25#include <libioP.h>
3e5f5557
UD
26
27
28/* This defines make it possible to use the same code for GNU C library and
29 the GNU I/O library. */
14c35863 30#define PUT(f, s, n) _IO_sputn (f, s, n)
d18ea0c5 31#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
3e5f5557
UD
32/* We use this file GNU C library and GNU I/O library. So make
33 names equal. */
14c35863
UD
34#undef putc
35#define putc(c, f) (wide \
36 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
37#define size_t _IO_size_t
38#define FILE _IO_FILE
3e5f5557
UD
39\f
40/* Macros for doing the actual output. */
41
42#define outchar(ch) \
43 do \
44 { \
2e09a79a 45 const int outc = (ch); \
3e5f5557
UD
46 if (putc (outc, fp) == EOF) \
47 return -1; \
48 ++done; \
49 } while (0)
50
a1d84548 51#define PRINT(ptr, wptr, len) \
3e5f5557
UD
52 do \
53 { \
2e09a79a 54 size_t outlen = (len); \
3e5f5557
UD
55 if (len > 20) \
56 { \
a1d84548 57 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
3e5f5557
UD
58 return -1; \
59 ptr += outlen; \
60 done += outlen; \
61 } \
62 else \
63 { \
a1d84548
UD
64 if (wide) \
65 while (outlen-- > 0) \
66 outchar (*wptr++); \
67 else \
68 while (outlen-- > 0) \
69 outchar (*ptr++); \
3e5f5557
UD
70 } \
71 } while (0)
72
73#define PADN(ch, len) \
74 do \
75 { \
76 if (PAD (fp, ch, len) != len) \
77 return -1; \
78 done += len; \
79 } \
80 while (0)
81\f
82/* Prototype for helper functions. */
83extern int __printf_fp (FILE *fp, const struct printf_info *info,
84 const void *const *args);
85
86\f
87int
c6251f03
RM
88__printf_size (FILE *fp, const struct printf_info *info,
89 const void *const *args)
3e5f5557
UD
90{
91 /* Units for the both formats. */
779ae82e
UD
92#define BINARY_UNITS " kmgtpezy"
93#define DECIMAL_UNITS " KMGTPEZY"
94 static const char units[2][sizeof (BINARY_UNITS)] =
3e5f5557 95 {
779ae82e
UD
96 BINARY_UNITS, /* For binary format. */
97 DECIMAL_UNITS /* For decimal format. */
3e5f5557
UD
98 };
99 const char *tag = units[isupper (info->spec) != 0];
100 int divisor = isupper (info->spec) ? 1000 : 1024;
101
102 /* The floating-point value to output. */
103 union
104 {
105 union ieee754_double dbl;
1b6adf88 106 long double ldbl;
3e5f5557
UD
107 }
108 fpnum;
109 const void *ptr = &fpnum;
110
187da0ae 111 int fpnum_sign = 0;
3e5f5557
UD
112
113 /* "NaN" or "Inf" for the special cases. */
114 const char *special = NULL;
a1d84548 115 const wchar_t *wspecial = NULL;
3e5f5557
UD
116
117 struct printf_info fp_info;
118 int done = 0;
a1d84548 119 int wide = info->wide;
187da0ae 120 int res;
3e5f5557
UD
121
122 /* Fetch the argument value. */
f98b4bbd 123#ifndef __NO_LONG_DOUBLE_MATH
3e5f5557
UD
124 if (info->is_long_double && sizeof (long double) > sizeof (double))
125 {
1b6adf88 126 fpnum.ldbl = *(const long double *) args[0];
3e5f5557
UD
127
128 /* Check for special values: not a number or infinity. */
1b6adf88 129 if (__isnanl (fpnum.ldbl))
3e5f5557
UD
130 {
131 special = "nan";
a1d84548 132 wspecial = L"nan";
187da0ae 133 // fpnum_sign = 0; Already zero
3e5f5557 134 }
1b6adf88 135 else if ((res = __isinfl (fpnum.ldbl)))
3e5f5557 136 {
187da0ae 137 fpnum_sign = res;
3e5f5557 138 special = "inf";
a1d84548 139 wspecial = L"inf";
3e5f5557
UD
140 }
141 else
1b6adf88 142 while (fpnum.ldbl >= divisor && tag[1] != '\0')
3e5f5557 143 {
1b6adf88 144 fpnum.ldbl /= divisor;
3e5f5557
UD
145 ++tag;
146 }
147 }
148 else
f98b4bbd 149#endif /* no long double */
3e5f5557
UD
150 {
151 fpnum.dbl.d = *(const double *) args[0];
152
153 /* Check for special values: not a number or infinity. */
76f2646f 154 if (__isnan (fpnum.dbl.d))
3e5f5557
UD
155 {
156 special = "nan";
a1d84548 157 wspecial = L"nan";
187da0ae 158 // fpnum_sign = 0; Already zero
3e5f5557 159 }
187da0ae 160 else if ((res = __isinf (fpnum.dbl.d)))
3e5f5557 161 {
187da0ae 162 fpnum_sign = res;
3e5f5557 163 special = "inf";
a1d84548 164 wspecial = L"inf";
3e5f5557
UD
165 }
166 else
167 while (fpnum.dbl.d >= divisor && tag[1] != '\0')
168 {
169 fpnum.dbl.d /= divisor;
170 ++tag;
171 }
172 }
173
174 if (special)
175 {
fa5753ee 176 int width = info->prec > info->width ? info->prec : info->width;
3e5f5557 177
187da0ae 178 if (fpnum_sign < 0 || info->showsign || info->space)
3e5f5557
UD
179 --width;
180 width -= 3;
181
182 if (!info->left && width > 0)
183 PADN (' ', width);
184
187da0ae 185 if (fpnum_sign < 0)
3e5f5557
UD
186 outchar ('-');
187 else if (info->showsign)
188 outchar ('+');
189 else if (info->space)
190 outchar (' ');
191
a1d84548 192 PRINT (special, wspecial, 3);
3e5f5557
UD
193
194 if (info->left && width > 0)
195 PADN (' ', width);
196
197 return done;
198 }
199
200 /* Prepare to print the number. We want to use `__printf_fp' so we
201 have to prepare a `printf_info' structure. */
7c11c4a1 202 fp_info = *info;
3e5f5557
UD
203 fp_info.spec = 'f';
204 fp_info.prec = info->prec < 0 ? 3 : info->prec;
a1d84548 205 fp_info.wide = wide;
3e5f5557
UD
206
207 if (fp_info.left && fp_info.pad == L' ')
208 {
209 /* We must do the padding ourself since the unit character must
210 be placed before the padding spaces. */
211 fp_info.width = 0;
212
213 done = __printf_fp (fp, &fp_info, &ptr);
214 if (done > 0)
215 {
216 outchar (*tag);
217 if (info->width > done)
218 PADN (' ', info->width - done);
219 }
220 }
221 else
222 {
223 /* We can let __printf_fp do all the printing and just add our
224 unit character afterwards. */
225 fp_info.width = info->width - 1;
226
227 done = __printf_fp (fp, &fp_info, &ptr);
228 if (done > 0)
229 outchar (*tag);
230 }
231
232 return done;
233}
c6251f03 234ldbl_strong_alias (__printf_size, printf_size);
3e5f5557
UD
235\f
236/* This is the function used by `vfprintf' to determine number and
237 type of the arguments. */
238int
239printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
240{
241 /* We need only one double or long double argument. */
242 if (n >= 1)
243 argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
244
245 return 1;
246}