Make unistd.h pre-c((-safe.
[glibc.git] / stdio-common / printf_size.c
blob957de2f571999fb8a92df5d569a2d47dd9de3b93
1 /* Print size value using units for orders of magnitude.
2 Copyright (C) 1997-2002, 2004, 2006 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
20 02111-1307 USA. */
22 #include <ctype.h>
23 #include <ieee754.h>
24 #include <math.h>
25 #include <printf.h>
26 #include <libioP.h>
29 /* This defines make it possible to use the same code for GNU C library and
30 the GNU I/O library. */
31 #define PUT(f, s, n) _IO_sputn (f, s, n)
32 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
33 /* We use this file GNU C library and GNU I/O library. So make
34 names equal. */
35 #undef putc
36 #define putc(c, f) (wide \
37 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
38 #define size_t _IO_size_t
39 #define FILE _IO_FILE
41 /* Macros for doing the actual output. */
43 #define outchar(ch) \
44 do \
45 { \
46 register const int outc = (ch); \
47 if (putc (outc, fp) == EOF) \
48 return -1; \
49 ++done; \
50 } while (0)
52 #define PRINT(ptr, wptr, len) \
53 do \
54 { \
55 register size_t outlen = (len); \
56 if (len > 20) \
57 { \
58 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
59 return -1; \
60 ptr += outlen; \
61 done += outlen; \
62 } \
63 else \
64 { \
65 if (wide) \
66 while (outlen-- > 0) \
67 outchar (*wptr++); \
68 else \
69 while (outlen-- > 0) \
70 outchar (*ptr++); \
71 } \
72 } while (0)
74 #define PADN(ch, len) \
75 do \
76 { \
77 if (PAD (fp, ch, len) != len) \
78 return -1; \
79 done += len; \
80 } \
81 while (0)
83 /* Prototype for helper functions. */
84 extern int __printf_fp (FILE *fp, const struct printf_info *info,
85 const void *const *args);
88 int
89 __printf_size (FILE *fp, const struct printf_info *info,
90 const void *const *args)
92 /* Units for the both formats. */
93 #define BINARY_UNITS " kmgtpezy"
94 #define DECIMAL_UNITS " KMGTPEZY"
95 static const char units[2][sizeof (BINARY_UNITS)] =
97 BINARY_UNITS, /* For binary format. */
98 DECIMAL_UNITS /* For decimal format. */
100 const char *tag = units[isupper (info->spec) != 0];
101 int divisor = isupper (info->spec) ? 1000 : 1024;
103 /* The floating-point value to output. */
104 union
106 union ieee754_double dbl;
107 union ieee854_long_double ldbl;
109 fpnum;
110 const void *ptr = &fpnum;
112 int negative = 0;
114 /* "NaN" or "Inf" for the special cases. */
115 const char *special = NULL;
116 const wchar_t *wspecial = NULL;
118 struct printf_info fp_info;
119 int done = 0;
120 int wide = info->wide;
123 /* Fetch the argument value. */
124 #ifndef __NO_LONG_DOUBLE_MATH
125 if (info->is_long_double && sizeof (long double) > sizeof (double))
127 fpnum.ldbl.d = *(const long double *) args[0];
129 /* Check for special values: not a number or infinity. */
130 if (__isnanl (fpnum.ldbl.d))
132 special = "nan";
133 wspecial = L"nan";
134 negative = 0;
136 else if (__isinfl (fpnum.ldbl.d))
138 special = "inf";
139 wspecial = L"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 wspecial = L"nan";
160 negative = 0;
162 else if (__isinf (fpnum.dbl.d))
164 special = "inf";
165 wspecial = L"inf";
167 negative = fpnum.dbl.d < 0;
169 else
170 while (fpnum.dbl.d >= divisor && tag[1] != '\0')
172 fpnum.dbl.d /= divisor;
173 ++tag;
177 if (special)
179 int width = info->prec > info->width ? info->prec : info->width;
181 if (negative || info->showsign || info->space)
182 --width;
183 width -= 3;
185 if (!info->left && width > 0)
186 PADN (' ', width);
188 if (negative)
189 outchar ('-');
190 else if (info->showsign)
191 outchar ('+');
192 else if (info->space)
193 outchar (' ');
195 PRINT (special, wspecial, 3);
197 if (info->left && width > 0)
198 PADN (' ', width);
200 return done;
203 /* Prepare to print the number. We want to use `__printf_fp' so we
204 have to prepare a `printf_info' structure. */
205 fp_info = *info;
206 fp_info.spec = 'f';
207 fp_info.prec = info->prec < 0 ? 3 : info->prec;
208 fp_info.wide = wide;
210 if (fp_info.left && fp_info.pad == L' ')
212 /* We must do the padding ourself since the unit character must
213 be placed before the padding spaces. */
214 fp_info.width = 0;
216 done = __printf_fp (fp, &fp_info, &ptr);
217 if (done > 0)
219 outchar (*tag);
220 if (info->width > done)
221 PADN (' ', info->width - done);
224 else
226 /* We can let __printf_fp do all the printing and just add our
227 unit character afterwards. */
228 fp_info.width = info->width - 1;
230 done = __printf_fp (fp, &fp_info, &ptr);
231 if (done > 0)
232 outchar (*tag);
235 return done;
237 ldbl_strong_alias (__printf_size, printf_size);
239 /* This is the function used by `vfprintf' to determine number and
240 type of the arguments. */
242 printf_size_info (const struct printf_info *info, size_t n, int *argtypes)
244 /* We need only one double or long double argument. */
245 if (n >= 1)
246 argtypes[0] = PA_DOUBLE | (info->is_long_double ? PA_FLAG_LONG_DOUBLE : 0);
248 return 1;