1 |
52 |
julius |
// Ripped out of latest ecos build from http://sources-redhat.mirrors.airband.net/ecos/releases/ecos-3.0b1/ecos-3.0beta1.i386linux.tar.bz2
|
2 |
|
|
// File: ecos-3.0b1/packages/language/c/libc/stdio/v3_0b1/src/output/vfnprintf.cxx
|
3 |
|
|
|
4 |
|
|
// Hacked to pieces so it would work with OpenRISC compiler, not using libc
|
5 |
|
|
//===========================================================================
|
6 |
|
|
//
|
7 |
|
|
// vfnprintf.c
|
8 |
|
|
//
|
9 |
|
|
// I/O routines for vfnprintf() for use with ANSI C library
|
10 |
|
|
//
|
11 |
|
|
//===========================================================================
|
12 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
13 |
|
|
// -------------------------------------------
|
14 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
15 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
16 |
|
|
//
|
17 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
18 |
|
|
// the terms of the GNU General Public License as published by the Free
|
19 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
20 |
|
|
// version.
|
21 |
|
|
//
|
22 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
23 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
24 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
25 |
|
|
// for more details.
|
26 |
|
|
//
|
27 |
|
|
// You should have received a copy of the GNU General Public License
|
28 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
29 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
30 |
|
|
//
|
31 |
|
|
// As a special exception, if other files instantiate templates or use
|
32 |
|
|
// macros or inline functions from this file, or you compile this file
|
33 |
|
|
// and link it with other works to produce a work based on this file,
|
34 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
35 |
|
|
// the GNU General Public License. However the source code for this file
|
36 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
37 |
|
|
// General Public License v2.
|
38 |
|
|
//
|
39 |
|
|
// This exception does not invalidate any other reasons why a work based
|
40 |
|
|
// on this file might be covered by the GNU General Public License.
|
41 |
|
|
// -------------------------------------------
|
42 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
43 |
|
|
//===========================================================================
|
44 |
|
|
//#####DESCRIPTIONBEGIN####
|
45 |
|
|
//
|
46 |
|
|
// Author(s): jlarmour
|
47 |
|
|
// Contributors:
|
48 |
|
|
// Date: 2000-04-20
|
49 |
|
|
// Purpose:
|
50 |
|
|
// Description:
|
51 |
|
|
// Usage:
|
52 |
|
|
//
|
53 |
|
|
//####DESCRIPTIONEND####
|
54 |
|
|
//
|
55 |
|
|
//===========================================================================
|
56 |
|
|
//
|
57 |
|
|
// This code is based on original code with the following copyright:
|
58 |
|
|
//
|
59 |
|
|
/*-
|
60 |
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
61 |
|
|
* All rights reserved.
|
62 |
|
|
*
|
63 |
|
|
* This code is derived from software contributed to Berkeley by
|
64 |
|
|
* Chris Torek.
|
65 |
|
|
*
|
66 |
|
|
* Redistribution and use in source and binary forms, with or without
|
67 |
|
|
* modification, are permitted provided that the following conditions
|
68 |
|
|
* are met:
|
69 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
70 |
|
|
* notice, this list of conditions and the following disclaimer.
|
71 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
72 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
73 |
|
|
* documentation and/or other materials provided with the distribution.
|
74 |
|
|
* 3. Neither the name of the University nor the names of its contributors
|
75 |
|
|
* may be used to endorse or promote products derived from this software
|
76 |
|
|
* without specific prior written permission.
|
77 |
|
|
*
|
78 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
79 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
80 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
81 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
82 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
83 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
84 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
85 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
86 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
87 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
88 |
|
|
* SUCH DAMAGE.
|
89 |
|
|
*/
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
// CONFIGURATION
|
93 |
|
|
|
94 |
|
|
//#include <pkgconf/libc_stdio.h> // Configuration header
|
95 |
|
|
//#include <pkgconf/libc_i18n.h> // Configuration header for mb support
|
96 |
|
|
|
97 |
|
|
// INCLUDES
|
98 |
|
|
|
99 |
|
|
#include <stdlib.h> // For mbtowc()
|
100 |
|
|
#include <stddef.h>
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
//#include <cyg/infra/cyg_type.h> // Common type definitions and support
|
104 |
|
|
#define CYG_MACRO_START do {
|
105 |
|
|
#define CYG_MACRO_END } while (0)
|
106 |
|
|
|
107 |
|
|
#define CYG_EMPTY_STATEMENT CYG_MACRO_START CYG_MACRO_END
|
108 |
|
|
|
109 |
|
|
#define CYG_UNUSED_PARAM( _type_, _name_ ) CYG_MACRO_START \
|
110 |
|
|
_type_ __tmp1 = (_name_); \
|
111 |
|
|
_type_ __tmp2 = __tmp1; \
|
112 |
|
|
__tmp1 = __tmp2; \
|
113 |
|
|
CYG_MACRO_END
|
114 |
|
|
|
115 |
|
|
#include <stdarg.h> // Variable argument definitions
|
116 |
|
|
//#include <stdio.h> // Standard header for all stdio files
|
117 |
|
|
#include <string.h> // memchr() and strlen() functions
|
118 |
|
|
//#include <cyg/libc/stdio/stream.hxx> // C library streams
|
119 |
|
|
|
120 |
|
|
#include "vfnprintf.h"
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
# define BUF 40
|
124 |
|
|
|
125 |
|
|
/*
|
126 |
|
|
* Actual printf innards.
|
127 |
|
|
*
|
128 |
|
|
* This code is large and complicated...
|
129 |
|
|
*/
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/*
|
133 |
|
|
* Macros for converting digits to letters and vice versa
|
134 |
|
|
*/
|
135 |
|
|
#define to_digit(c) ((c) - '0')
|
136 |
|
|
#define is_digit(c) ((unsigned)to_digit(c) <= 9)
|
137 |
|
|
#define to_char(n) ((n) + '0')
|
138 |
|
|
|
139 |
|
|
/*
|
140 |
|
|
* Flags used during conversion.
|
141 |
|
|
*/
|
142 |
|
|
#define ALT 0x001 /* alternate form */
|
143 |
|
|
#define HEXPREFIX 0x002 /* add 0x or 0X prefix */
|
144 |
|
|
#define LADJUST 0x004 /* left adjustment */
|
145 |
|
|
#define LONGDBL 0x008 /* long double; unimplemented */
|
146 |
|
|
#define LONGINT 0x010 /* long integer */
|
147 |
|
|
#define QUADINT 0x020 /* quad integer */
|
148 |
|
|
#define SHORTINT 0x040 /* short integer */
|
149 |
|
|
#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
|
150 |
|
|
#define FPT 0x100 /* Floating point number */
|
151 |
|
|
#define SIZET 0x200 /* size_t */
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
// Function which prints back to the buffer, ptr, len bytes
|
155 |
|
|
// returns 1 if it should finish up, otherwise 0 to continue
|
156 |
|
|
int print_back_to_string(char * ptr, int len, size_t * n, int * ret, char ** stream)
|
157 |
|
|
{
|
158 |
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
159 |
|
|
do {
|
160 |
|
|
int length = MIN( (int) len, *n - *ret - 1);
|
161 |
|
|
memcpy(*stream + *ret, ptr, length);
|
162 |
|
|
if (length < (int)len) {
|
163 |
|
|
*ret += length;
|
164 |
|
|
return 1; // finish up
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
} while(0);
|
168 |
|
|
|
169 |
|
|
return 0;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
//externC int
|
173 |
|
|
int
|
174 |
|
|
//vfnprintf ( FILE *stream, size_t n, const char *format, va_list arg) __THROW
|
175 |
|
|
vfnprintf ( char *stream, size_t n, const char *format, va_list arg)
|
176 |
|
|
{
|
177 |
|
|
char *fmt; /* format string */
|
178 |
|
|
int ch; /* character from fmt */
|
179 |
|
|
int x, y; /* handy integers (short term usage) */
|
180 |
|
|
char *cp; /* handy char pointer (short term usage) */
|
181 |
|
|
int flags; /* flags as above */
|
182 |
|
|
|
183 |
|
|
int ret; /* return value accumulator */
|
184 |
|
|
int width; /* width from format (%8d), or 0 */
|
185 |
|
|
int prec; /* precision from format (%.3d), or -1 */
|
186 |
|
|
char sign; /* sign prefix (' ', '+', '-', or \0) */
|
187 |
|
|
wchar_t wc;
|
188 |
|
|
|
189 |
|
|
#define quad_t long long
|
190 |
|
|
#define u_quad_t unsigned long long
|
191 |
|
|
|
192 |
|
|
u_quad_t _uquad; /* integer arguments %[diouxX] */
|
193 |
|
|
enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
|
194 |
|
|
int dprec; /* a copy of prec if [diouxX], 0 otherwise */
|
195 |
|
|
int fieldsz; /* field size expanded by sign, etc */
|
196 |
|
|
int realsz; /* field size expanded by dprec */
|
197 |
|
|
int size; /* size of converted field or string */
|
198 |
|
|
char *xdigs; /* digits for [xX] conversion */
|
199 |
|
|
#define NIOV 8
|
200 |
|
|
char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */
|
201 |
|
|
char ox[2]; /* space for 0x hex-prefix */
|
202 |
|
|
|
203 |
|
|
/*
|
204 |
|
|
* Choose PADSIZE to trade efficiency vs. size. If larger printf
|
205 |
|
|
* fields occur frequently, increase PADSIZE and make the initialisers
|
206 |
|
|
* below longer.
|
207 |
|
|
*/
|
208 |
|
|
#define PADSIZE 16 /* pad chunk size */
|
209 |
|
|
static char blanks[PADSIZE] =
|
210 |
|
|
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
|
211 |
|
|
static char zeroes[PADSIZE] =
|
212 |
|
|
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
|
213 |
|
|
|
214 |
|
|
/*
|
215 |
|
|
* BEWARE, these `goto error' on error, and PAD uses `n'.
|
216 |
|
|
*/
|
217 |
|
|
|
218 |
|
|
// We'll copy len bytes from (char*) ptr, into the output stream
|
219 |
|
|
// making sure we don't go over the end, so calculate length to be
|
220 |
|
|
// either the whole length we've been passed, or the whole length
|
221 |
|
|
// that is possible to write
|
222 |
|
|
// We finish if it was not possible to write the entire variable
|
223 |
|
|
// into the buffer, ie we had to write all we could, not all we
|
224 |
|
|
// wanted to.
|
225 |
|
|
/*
|
226 |
|
|
#define PRINT(ptr, len) \
|
227 |
|
|
CYG_MACRO_START \
|
228 |
|
|
int length = MIN( (int) len, n - ret - 1); \
|
229 |
|
|
char* begin_stream_write = stream; \
|
230 |
|
|
stream = memcpy(stream, ptr, length); \
|
231 |
|
|
length = (unsigned long) stream - (unsigned long) begin_stream_write; \
|
232 |
|
|
if (length < (int)len) { \
|
233 |
|
|
ret += length; \
|
234 |
|
|
goto done; \
|
235 |
|
|
} \
|
236 |
|
|
CYG_MACRO_END
|
237 |
|
|
*/
|
238 |
|
|
|
239 |
|
|
//PRINT(with, PADSIZE); \
|
240 |
|
|
//PRINT(with, x); \
|
241 |
|
|
|
242 |
|
|
#define PAD(howmany, with) \
|
243 |
|
|
CYG_MACRO_START \
|
244 |
|
|
if ((x = (howmany)) > 0) { \
|
245 |
|
|
while (x > PADSIZE) { \
|
246 |
|
|
if (print_back_to_string(with, PADSIZE, &n, &ret, &stream)) goto done; \
|
247 |
|
|
x -= PADSIZE; \
|
248 |
|
|
} \
|
249 |
|
|
if (print_back_to_string(with, x, &n, &ret, &stream))goto done; \
|
250 |
|
|
} \
|
251 |
|
|
CYG_MACRO_END
|
252 |
|
|
|
253 |
|
|
/*
|
254 |
|
|
* To extend shorts properly, we need both signed and unsigned
|
255 |
|
|
* argument extraction methods.
|
256 |
|
|
*/
|
257 |
|
|
|
258 |
|
|
#define SARG() \
|
259 |
|
|
(flags&QUADINT ? va_arg(arg, long long) : \
|
260 |
|
|
flags&LONGINT ? va_arg(arg, long) : \
|
261 |
|
|
flags&SHORTINT ? (long)(short)va_arg(arg, int) : \
|
262 |
|
|
flags&SIZET ? (long)va_arg(arg, size_t) : \
|
263 |
|
|
(long)va_arg(arg, int))
|
264 |
|
|
#define UARG() \
|
265 |
|
|
(flags&QUADINT ? va_arg(arg, unsigned long long) : \
|
266 |
|
|
flags&LONGINT ? va_arg(arg, unsigned long) : \
|
267 |
|
|
flags&SHORTINT ? (unsigned long)(unsigned short)va_arg(arg, int) : \
|
268 |
|
|
flags&SIZET ? va_arg(arg, size_t) : \
|
269 |
|
|
(unsigned long)va_arg(arg, unsigned int))
|
270 |
|
|
|
271 |
|
|
|
272 |
|
|
xdigs = NULL; // stop compiler whinging
|
273 |
|
|
fmt = (char *)format;
|
274 |
|
|
ret = 0;
|
275 |
|
|
|
276 |
|
|
/*
|
277 |
|
|
* Scan the format for conversions (`%' character).
|
278 |
|
|
*/
|
279 |
|
|
for (;;) {
|
280 |
|
|
cp = (char *)fmt; // char pointer - set to where we begin looking from
|
281 |
|
|
while ((x = ((wc = *fmt) != 0))) { // While, wc=next char and x is one while there's still chars left
|
282 |
|
|
fmt += x; // increment the pointer to the char
|
283 |
|
|
if (wc == '%') { // check if it's the beginning of
|
284 |
|
|
fmt--; // Decrement the char pointer, actually
|
285 |
|
|
break;
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
if ((y = fmt - cp) != 0) { // y is length of string to copy out just now
|
289 |
|
|
//PRINT(cp, y); // Copy macro
|
290 |
|
|
if(print_back_to_string(cp, y, &n, &ret, &stream)) goto done; // Copy macro
|
291 |
|
|
ret += y; // increment return chars
|
292 |
|
|
}
|
293 |
|
|
if ((x <= 0) || (ret >= (int)n)) // @@@ this check with n isn't good enough
|
294 |
|
|
goto done;
|
295 |
|
|
fmt++; /* skip over '%' */
|
296 |
|
|
|
297 |
|
|
flags = 0;
|
298 |
|
|
dprec = 0;
|
299 |
|
|
width = 0;
|
300 |
|
|
prec = -1;
|
301 |
|
|
sign = '\0';
|
302 |
|
|
|
303 |
|
|
rflag: ch = *fmt++;
|
304 |
|
|
reswitch: switch (ch) {
|
305 |
|
|
case ' ':
|
306 |
|
|
/*
|
307 |
|
|
* ``If the space and + flags both appear, the space
|
308 |
|
|
* flag will be ignored.''
|
309 |
|
|
* -- ANSI X3J11
|
310 |
|
|
*/
|
311 |
|
|
if (!sign)
|
312 |
|
|
sign = ' ';
|
313 |
|
|
goto rflag;
|
314 |
|
|
case '#':
|
315 |
|
|
flags |= ALT;
|
316 |
|
|
goto rflag;
|
317 |
|
|
case '*':
|
318 |
|
|
/*
|
319 |
|
|
* ``A negative field width argument is taken as a
|
320 |
|
|
* - flag followed by a positive field width.''
|
321 |
|
|
* -- ANSI X3J11
|
322 |
|
|
* They don't exclude field widths read from args.
|
323 |
|
|
*/
|
324 |
|
|
if ((width = va_arg(arg, int)) >= 0)
|
325 |
|
|
goto rflag;
|
326 |
|
|
width = -width;
|
327 |
|
|
/* FALLTHROUGH */
|
328 |
|
|
case '-':
|
329 |
|
|
flags |= LADJUST;
|
330 |
|
|
goto rflag;
|
331 |
|
|
case '+':
|
332 |
|
|
sign = '+';
|
333 |
|
|
goto rflag;
|
334 |
|
|
case '.':
|
335 |
|
|
if ((ch = *fmt++) == '*') {
|
336 |
|
|
x = va_arg(arg, int);
|
337 |
|
|
prec = x < 0 ? -1 : x;
|
338 |
|
|
goto rflag;
|
339 |
|
|
}
|
340 |
|
|
x = 0;
|
341 |
|
|
while (is_digit(ch)) {
|
342 |
|
|
x = 10 * x + to_digit(ch);
|
343 |
|
|
ch = *fmt++;
|
344 |
|
|
}
|
345 |
|
|
prec = x < 0 ? -1 : x;
|
346 |
|
|
goto reswitch;
|
347 |
|
|
case '0':
|
348 |
|
|
/*
|
349 |
|
|
* ``Note that 0 is taken as a flag, not as the
|
350 |
|
|
* beginning of a field width.''
|
351 |
|
|
* -- ANSI X3J11
|
352 |
|
|
*/
|
353 |
|
|
flags |= ZEROPAD;
|
354 |
|
|
goto rflag;
|
355 |
|
|
case '1': case '2': case '3': case '4':
|
356 |
|
|
case '5': case '6': case '7': case '8': case '9':
|
357 |
|
|
x = 0;
|
358 |
|
|
do {
|
359 |
|
|
x = 10 * x + to_digit(ch);
|
360 |
|
|
ch = *fmt++;
|
361 |
|
|
} while (is_digit(ch));
|
362 |
|
|
width = x;
|
363 |
|
|
goto reswitch;
|
364 |
|
|
case 'h':
|
365 |
|
|
flags |= SHORTINT;
|
366 |
|
|
goto rflag;
|
367 |
|
|
case 'l':
|
368 |
|
|
if (*fmt == 'l') {
|
369 |
|
|
fmt++;
|
370 |
|
|
flags |= QUADINT;
|
371 |
|
|
} else {
|
372 |
|
|
flags |= LONGINT;
|
373 |
|
|
}
|
374 |
|
|
goto rflag;
|
375 |
|
|
case 'q':
|
376 |
|
|
flags |= QUADINT;
|
377 |
|
|
goto rflag;
|
378 |
|
|
case 'c':
|
379 |
|
|
*(cp = buf) = va_arg(arg, int);
|
380 |
|
|
size = 1;
|
381 |
|
|
sign = '\0';
|
382 |
|
|
break;
|
383 |
|
|
case 'D':
|
384 |
|
|
flags |= LONGINT;
|
385 |
|
|
/*FALLTHROUGH*/
|
386 |
|
|
case 'd':
|
387 |
|
|
case 'i':
|
388 |
|
|
_uquad = SARG();
|
389 |
|
|
#ifndef _NO_LONGLONG
|
390 |
|
|
if ((quad_t)_uquad < 0)
|
391 |
|
|
#else
|
392 |
|
|
if ((long) _uquad < 0)
|
393 |
|
|
#endif
|
394 |
|
|
{
|
395 |
|
|
|
396 |
|
|
_uquad = -_uquad;
|
397 |
|
|
sign = '-';
|
398 |
|
|
}
|
399 |
|
|
base = DEC;
|
400 |
|
|
goto number;
|
401 |
|
|
|
402 |
|
|
case 'e':
|
403 |
|
|
case 'E':
|
404 |
|
|
case 'f':
|
405 |
|
|
case 'g':
|
406 |
|
|
case 'G':
|
407 |
|
|
// Output nothing at all
|
408 |
|
|
(void) va_arg(arg, double); // take off arg anyway
|
409 |
|
|
cp = "";
|
410 |
|
|
size = 0;
|
411 |
|
|
sign = '\0';
|
412 |
|
|
break;
|
413 |
|
|
|
414 |
|
|
case 'n':
|
415 |
|
|
#ifndef _NO_LONGLONG
|
416 |
|
|
if (flags & QUADINT)
|
417 |
|
|
*va_arg(arg, quad_t *) = ret;
|
418 |
|
|
else
|
419 |
|
|
#endif
|
420 |
|
|
if (flags & LONGINT)
|
421 |
|
|
*va_arg(arg, long *) = ret;
|
422 |
|
|
else if (flags & SHORTINT)
|
423 |
|
|
*va_arg(arg, short *) = ret;
|
424 |
|
|
else if (flags & SIZET)
|
425 |
|
|
*va_arg(arg, size_t *) = ret;
|
426 |
|
|
else
|
427 |
|
|
*va_arg(arg, int *) = ret;
|
428 |
|
|
continue; /* no output */
|
429 |
|
|
case 'O':
|
430 |
|
|
flags |= LONGINT;
|
431 |
|
|
/*FALLTHROUGH*/
|
432 |
|
|
case 'o':
|
433 |
|
|
_uquad = UARG();
|
434 |
|
|
base = OCT;
|
435 |
|
|
goto nosign;
|
436 |
|
|
case 'p':
|
437 |
|
|
/*
|
438 |
|
|
* ``The argument shall be a pointer to void. The
|
439 |
|
|
* value of the pointer is converted to a sequence
|
440 |
|
|
* of printable characters, in an implementation-
|
441 |
|
|
* defined manner.''
|
442 |
|
|
* -- ANSI X3J11
|
443 |
|
|
*/
|
444 |
|
|
/* NOSTRICT */
|
445 |
|
|
_uquad = (unsigned long)va_arg(arg, void *);
|
446 |
|
|
base = HEX;
|
447 |
|
|
xdigs = (char *)"0123456789abcdef";
|
448 |
|
|
flags |= HEXPREFIX;
|
449 |
|
|
ch = 'x';
|
450 |
|
|
goto nosign;
|
451 |
|
|
case 's':
|
452 |
|
|
if ((cp = va_arg(arg, char *)) == NULL)
|
453 |
|
|
cp = (char *)"(null)";
|
454 |
|
|
if (prec >= 0) {
|
455 |
|
|
/*
|
456 |
|
|
* can't use strlen; can only look for the
|
457 |
|
|
* NUL in the first `prec' characters, and
|
458 |
|
|
* strlen() will go further.
|
459 |
|
|
*/
|
460 |
|
|
char *p = (char *)memchr(cp, 0, prec);
|
461 |
|
|
|
462 |
|
|
if (p != NULL) {
|
463 |
|
|
size = p - cp;
|
464 |
|
|
if (size > prec)
|
465 |
|
|
size = prec;
|
466 |
|
|
} else
|
467 |
|
|
size = prec;
|
468 |
|
|
} else
|
469 |
|
|
size = strlen(cp);
|
470 |
|
|
sign = '\0';
|
471 |
|
|
break;
|
472 |
|
|
case 'U':
|
473 |
|
|
flags |= LONGINT;
|
474 |
|
|
/*FALLTHROUGH*/
|
475 |
|
|
case 'u':
|
476 |
|
|
_uquad = UARG();
|
477 |
|
|
base = DEC;
|
478 |
|
|
goto nosign;
|
479 |
|
|
case 'X':
|
480 |
|
|
xdigs = (char *)"0123456789ABCDEF";
|
481 |
|
|
goto hex;
|
482 |
|
|
case 'x':
|
483 |
|
|
xdigs = (char *)"0123456789abcdef";
|
484 |
|
|
hex: _uquad = UARG();
|
485 |
|
|
base = HEX;
|
486 |
|
|
/* leading 0x/X only if non-zero */
|
487 |
|
|
if (flags & ALT && _uquad != 0)
|
488 |
|
|
flags |= HEXPREFIX;
|
489 |
|
|
|
490 |
|
|
/* unsigned conversions */
|
491 |
|
|
nosign: sign = '\0';
|
492 |
|
|
/*
|
493 |
|
|
* ``... diouXx conversions ... if a precision is
|
494 |
|
|
* specified, the 0 flag will be ignored.''
|
495 |
|
|
* -- ANSI X3J11
|
496 |
|
|
*/
|
497 |
|
|
number: if ((dprec = prec) >= 0)
|
498 |
|
|
flags &= ~ZEROPAD;
|
499 |
|
|
|
500 |
|
|
/*
|
501 |
|
|
* ``The result of converting a zero value with an
|
502 |
|
|
* explicit precision of zero is no characters.''
|
503 |
|
|
* -- ANSI X3J11
|
504 |
|
|
*/
|
505 |
|
|
cp = buf + BUF;
|
506 |
|
|
if (_uquad != 0 || prec != 0) {
|
507 |
|
|
/*
|
508 |
|
|
* Unsigned mod is hard, and unsigned mod
|
509 |
|
|
* by a constant is easier than that by
|
510 |
|
|
* a variable; hence this switch.
|
511 |
|
|
*/
|
512 |
|
|
switch (base) {
|
513 |
|
|
case OCT:
|
514 |
|
|
do {
|
515 |
|
|
*--cp = to_char(_uquad & 7);
|
516 |
|
|
_uquad >>= 3;
|
517 |
|
|
} while (_uquad);
|
518 |
|
|
/* handle octal leading 0 */
|
519 |
|
|
if (flags & ALT && *cp != '0')
|
520 |
|
|
*--cp = '0';
|
521 |
|
|
break;
|
522 |
|
|
|
523 |
|
|
case DEC:
|
524 |
|
|
if (!(flags & QUADINT)) {
|
525 |
|
|
/* many numbers are 1 digit */
|
526 |
|
|
unsigned long v = (unsigned long)_uquad;
|
527 |
|
|
while (v >= 10) {
|
528 |
|
|
/* The following is usually faster than using a modulo */
|
529 |
|
|
unsigned long next = v / 10;
|
530 |
|
|
*--cp = to_char(v - (next * 10));
|
531 |
|
|
v = next;
|
532 |
|
|
}
|
533 |
|
|
*--cp = to_char(v);
|
534 |
|
|
}
|
535 |
|
|
else {
|
536 |
|
|
while (_uquad >= 10) {
|
537 |
|
|
/* The following is usually faster than using a modulo */
|
538 |
|
|
u_quad_t next = _uquad / 10;
|
539 |
|
|
*--cp = to_char(_uquad - (next * 10));
|
540 |
|
|
_uquad = next;
|
541 |
|
|
}
|
542 |
|
|
*--cp = to_char(_uquad);
|
543 |
|
|
}
|
544 |
|
|
break;
|
545 |
|
|
|
546 |
|
|
case HEX:
|
547 |
|
|
do {
|
548 |
|
|
*--cp = xdigs[_uquad & 15];
|
549 |
|
|
_uquad >>= 4;
|
550 |
|
|
} while (_uquad);
|
551 |
|
|
break;
|
552 |
|
|
|
553 |
|
|
default:
|
554 |
|
|
cp = (char *)"bug in vfprintf: bad base";
|
555 |
|
|
size = strlen(cp);
|
556 |
|
|
goto skipsize;
|
557 |
|
|
}
|
558 |
|
|
}
|
559 |
|
|
size = buf + BUF - cp;
|
560 |
|
|
skipsize:
|
561 |
|
|
break;
|
562 |
|
|
case 'z':
|
563 |
|
|
flags |= SIZET;
|
564 |
|
|
goto rflag;
|
565 |
|
|
default: /* "%?" prints ?, unless ? is NUL */
|
566 |
|
|
if (ch == '\0')
|
567 |
|
|
goto done;
|
568 |
|
|
/* pretend it was %c with argument ch */
|
569 |
|
|
cp = buf;
|
570 |
|
|
*cp = ch;
|
571 |
|
|
size = 1;
|
572 |
|
|
sign = '\0';
|
573 |
|
|
break;
|
574 |
|
|
}
|
575 |
|
|
|
576 |
|
|
/*
|
577 |
|
|
* All reasonable formats wind up here. At this point, `cp'
|
578 |
|
|
* points to a string which (if not flags&LADJUST) should be
|
579 |
|
|
* padded out to `width' places. If flags&ZEROPAD, it should
|
580 |
|
|
* first be prefixed by any sign or other prefix; otherwise,
|
581 |
|
|
* it should be blank padded before the prefix is emitted.
|
582 |
|
|
* After any left-hand padding and prefixing, emit zeroes
|
583 |
|
|
* required by a decimal [diouxX] precision, then print the
|
584 |
|
|
* string proper, then emit zeroes required by any leftover
|
585 |
|
|
* floating precision; finally, if LADJUST, pad with blanks.
|
586 |
|
|
*
|
587 |
|
|
* Compute actual size, so we know how much to pad.
|
588 |
|
|
* fieldsz excludes decimal prec; realsz includes it.
|
589 |
|
|
*/
|
590 |
|
|
#ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
|
591 |
|
|
fieldsz = size + fpprec;
|
592 |
|
|
#else
|
593 |
|
|
fieldsz = size;
|
594 |
|
|
#endif
|
595 |
|
|
if (sign)
|
596 |
|
|
fieldsz++;
|
597 |
|
|
else if (flags & HEXPREFIX)
|
598 |
|
|
fieldsz+= 2;
|
599 |
|
|
realsz = dprec > fieldsz ? dprec : fieldsz;
|
600 |
|
|
|
601 |
|
|
/* right-adjusting blank padding */
|
602 |
|
|
if ((flags & (LADJUST|ZEROPAD)) == 0) {
|
603 |
|
|
if (width - realsz > 0) {
|
604 |
|
|
PAD(width - realsz, blanks);
|
605 |
|
|
ret += width - realsz;
|
606 |
|
|
}
|
607 |
|
|
}
|
608 |
|
|
|
609 |
|
|
/* prefix */
|
610 |
|
|
if (sign) {
|
611 |
|
|
//PRINT(&sign, 1);
|
612 |
|
|
if(print_back_to_string(&sign, 1, &n, &ret, &stream))goto done;
|
613 |
|
|
ret++;
|
614 |
|
|
} else if (flags & HEXPREFIX) {
|
615 |
|
|
ox[0] = '0';
|
616 |
|
|
ox[1] = ch;
|
617 |
|
|
//PRINT(ox, 2);
|
618 |
|
|
if(print_back_to_string(ox, 2, &n, &ret, &stream))goto done;
|
619 |
|
|
ret += 2;
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
/* right-adjusting zero padding */
|
623 |
|
|
if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
|
624 |
|
|
if (width - realsz > 0) {
|
625 |
|
|
PAD(width - realsz, zeroes);
|
626 |
|
|
ret += width - realsz;
|
627 |
|
|
}
|
628 |
|
|
}
|
629 |
|
|
|
630 |
|
|
if (dprec - fieldsz > 0) {
|
631 |
|
|
/* leading zeroes from decimal precision */
|
632 |
|
|
PAD(dprec - fieldsz, zeroes);
|
633 |
|
|
ret += dprec - fieldsz;
|
634 |
|
|
}
|
635 |
|
|
|
636 |
|
|
/* the string or number proper */
|
637 |
|
|
//PRINT(cp, size);
|
638 |
|
|
if(print_back_to_string(cp,size, &n, &ret, &stream))goto done;
|
639 |
|
|
ret += size;
|
640 |
|
|
|
641 |
|
|
#ifdef CYGSEM_LIBC_STDIO_PRINTF_FLOATING_POINT
|
642 |
|
|
/* trailing f.p. zeroes */
|
643 |
|
|
PAD(fpprec, zeroes);
|
644 |
|
|
ret += fpprec;
|
645 |
|
|
#endif
|
646 |
|
|
|
647 |
|
|
/* left-adjusting padding (always blank) */
|
648 |
|
|
if (flags & LADJUST) {
|
649 |
|
|
if (width - realsz > 0) {
|
650 |
|
|
PAD(width - realsz, blanks);
|
651 |
|
|
ret += width - realsz;
|
652 |
|
|
}
|
653 |
|
|
}
|
654 |
|
|
|
655 |
|
|
}
|
656 |
|
|
|
657 |
|
|
done:
|
658 |
|
|
error:
|
659 |
|
|
return ret;// remove this error stuff (((Cyg_OutputStream *) stream)->get_error() ? EOF : ret);
|
660 |
|
|
/* NOTREACHED */
|
661 |
|
|
}
|
662 |
|
|
|
663 |
|
|
|
664 |
|
|
|
665 |
|
|
// EOF vfnprintf.c
|