/
vfprintf.c
2102 lines (1966 loc) · 56.1 KB
/
vfprintf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
FUNCTION
<<vfprintf>>, <<vprintf>>, <<vsprintf>>, <<vsnprintf>>, <<vasprintf>>, <<vasnprintf>>---format argument list
INDEX
vfprintf
INDEX
_vfprintf_r
INDEX
vprintf
INDEX
_vprintf_r
INDEX
vsprintf
INDEX
_vsprintf_r
INDEX
vsnprintf
INDEX
_vsnprintf_r
INDEX
vasprintf
INDEX
_vasprintf_r
INDEX
vasnprintf
INDEX
_vasnprintf_r
SYNOPSIS
#include <stdio.h>
#include <stdarg.h>
int vprintf(const char *<[fmt]>, va_list <[list]>);
int vfprintf(FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>);
int vsprintf(char *<[str]>, const char *<[fmt]>, va_list <[list]>);
int vsnprintf(char *<[str]>, size_t <[size]>, const char *<[fmt]>,
va_list <[list]>);
int vasprintf(char **<[strp]>, const char *<[fmt]>, va_list <[list]>);
char *vasnprintf(char *<[str]>, size_t *<[size]>, const char *<[fmt]>,
va_list <[list]>);
int _vprintf_r(struct _reent *<[reent]>, const char *<[fmt]>,
va_list <[list]>);
int _vfprintf_r(struct _reent *<[reent]>, FILE *<[fp]>,
const char *<[fmt]>, va_list <[list]>);
int _vsprintf_r(struct _reent *<[reent]>, char *<[str]>,
const char *<[fmt]>, va_list <[list]>);
int _vasprintf_r(struct _reent *<[reent]>, char **<[str]>,
const char *<[fmt]>, va_list <[list]>);
int _vsnprintf_r(struct _reent *<[reent]>, char *<[str]>,
size_t <[size]>, const char *<[fmt]>, va_list <[list]>);
char *_vasnprintf_r(struct _reent *<[reent]>, char *<[str]>,
size_t *<[size]>, const char *<[fmt]>, va_list <[list]>);
DESCRIPTION
<<vprintf>>, <<vfprintf>>, <<vasprintf>>, <<vsprintf>>, <<vsnprintf>>,
and <<vasnprintf>> are (respectively) variants of <<printf>>,
<<fprintf>>, <<asprintf>>, <<sprintf>>, <<snprintf>>, and
<<asnprintf>>. They differ only in allowing their caller to pass the
variable argument list as a <<va_list>> object (initialized by
<<va_start>>) rather than directly accepting a variable number of
arguments. The caller is responsible for calling <<va_end>>.
<<_vprintf_r>>, <<_vfprintf_r>>, <<_vasprintf_r>>, <<_vsprintf_r>>,
<<_vsnprintf_r>>, and <<_vasnprintf_r>> are reentrant versions of the
above.
RETURNS
The return values are consistent with the corresponding functions.
PORTABILITY
ANSI C requires <<vprintf>>, <<vfprintf>>, <<vsprintf>>, and
<<vsnprintf>>. The remaining functions are newlib extensions.
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
#if defined(LIBC_SCCS) && !defined(lint)
/*static char *sccsid = "from: @(#)vfprintf.c 5.50 (Berkeley) 12/16/92";*/
static char *rcsid = "$Id$";
#endif /* LIBC_SCCS and not lint */
/*
* Actual printf innards.
*
* This code is large and complicated...
*/
#include <newlib.h>
#ifdef INTEGER_ONLY
# define VFPRINTF vfiprintf
# ifdef STRING_ONLY
# define _VFPRINTF_R _svfiprintf_r
# else
# define _VFPRINTF_R _vfiprintf_r
# endif
#else
# define VFPRINTF vfprintf
# ifdef STRING_ONLY
# define _VFPRINTF_R _svfprintf_r
# else
# define _VFPRINTF_R _vfprintf_r
# endif
# ifndef NO_FLOATING_POINT
# define FLOATING_POINT
# endif
#endif
#define _NO_POS_ARGS
#ifdef _WANT_IO_POS_ARGS
# undef _NO_POS_ARGS
#endif
#include <_ansi.h>
#include <reent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#include <wchar.h>
#include <sys/lock.h>
#include <stdarg.h>
#include "local.h"
#include "../stdlib/local.h"
#include "fvwrite.h"
#include "vfieeefp.h"
/* Currently a test is made to see if long double processing is warranted.
This could be changed in the future should the _ldtoa_r code be
preferred over _dtoa_r. */
#define _NO_LONGDBL
#if defined _WANT_IO_LONG_DOUBLE && (LDBL_MANT_DIG > DBL_MANT_DIG)
#undef _NO_LONGDBL
#endif
#define _NO_LONGLONG
#if defined _WANT_IO_LONG_LONG \
&& (defined __GNUC__ || __STDC_VERSION__ >= 199901L)
# undef _NO_LONGLONG
#endif
#ifdef STRING_ONLY
# ifdef _FVWRITE_IN_STREAMIO
# define __SPRINT __ssprint_r
int __ssprint_r (struct _reent *, FILE *, register struct __suio *);
# else
# define __SPRINT __ssputs_r
int __ssputs_r (struct _reent *, FILE *, const char *, size_t);
# endif
#else /* !STRING_ONLY */
# ifdef _FVWRITE_IN_STREAMIO
# define __SPRINT __sprint_r
int __sprint_r (struct _reent *, FILE *, register struct __suio *);
# else
# define __SPRINT __sfputs_r
int __sfputs_r (struct _reent *, FILE *, const char *buf, size_t);
# endif
#ifdef _UNBUF_STREAM_OPT
/*
* Helper function for `fprintf to unbuffered unix file': creates a
* temporary buffer. We only work on write-only files; this avoids
* worries about ungetc buffers and so forth.
*
* Make sure to avoid inlining.
*/
_NOINLINE_STATIC int
__sbprintf (struct _reent *rptr,
register FILE *fp,
const char *fmt,
va_list ap)
{
int ret;
FILE fake;
unsigned char buf[BUFSIZ];
/* copy the important variables */
fake._flags = fp->_flags & ~__SNBF;
fake._flags2 = fp->_flags2;
fake._file = fp->_file;
fake._cookie = fp->_cookie;
fake._write = fp->_write;
/* set up the buffer */
fake._bf._base = fake._p = buf;
fake._bf._size = fake._w = sizeof (buf);
fake._lbfsize = 0; /* not actually used, but Just In Case */
#ifndef __SINGLE_THREAD__
__lock_init_recursive (fake._lock);
#endif
/* do the work, then copy any error status */
ret = _VFPRINTF_R (rptr, &fake, fmt, ap);
if (ret >= 0 && _fflush_r (rptr, &fake))
ret = EOF;
if (fake._flags & __SERR)
fp->_flags |= __SERR;
#ifndef __SINGLE_THREAD__
__lock_close_recursive (fake._lock);
#endif
return (ret);
}
#endif /* _UNBUF_STREAM_OPT */
#endif /* !STRING_ONLY */
#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS)
# include <locale.h>
#endif
#ifdef FLOATING_POINT
# include <math.h>
/* For %La, an exponent of 15 bits occupies the exponent character, a
sign, and up to 5 digits. */
# define MAXEXPLEN 7
# define DEFPREC 6
# ifdef _NO_LONGDBL
extern char *_dtoa_r (struct _reent *, double, int,
int, int *, int *, char **);
# define _PRINTF_FLOAT_TYPE double
# define _DTOA_R _dtoa_r
# define FREXP frexp
# else /* !_NO_LONGDBL */
extern char *_ldtoa_r (struct _reent *, _LONG_DOUBLE, int,
int, int *, int *, char **);
extern int _ldcheck (_LONG_DOUBLE *);
# define _PRINTF_FLOAT_TYPE _LONG_DOUBLE
# define _DTOA_R _ldtoa_r
# define FREXP frexpl
# endif /* !_NO_LONGDBL */
static char *cvt(struct _reent *, _PRINTF_FLOAT_TYPE, int, int, char *, int *,
int, int *, char *);
static int exponent(char *, int, int);
#endif /* FLOATING_POINT */
/* BUF must be big enough for the maximum %#llo (assuming long long is
at most 64 bits, this would be 23 characters), the maximum
multibyte character %C, and the maximum default precision of %La
(assuming long double is at most 128 bits with 113 bits of
mantissa, this would be 29 characters). %e, %f, and %g use
reentrant storage shared with mprec. All other formats that use
buf get by with fewer characters. Making BUF slightly bigger
reduces the need for malloc in %.*a and %S, when large precision or
long strings are processed.
The bigger size of 100 bytes is used on systems which allow number
strings using the locale's grouping character. Since that's a multibyte
value, we should use a conservative value.
*/
#ifdef _WANT_IO_C99_FORMATS
#define BUF 100
#else
#define BUF 40
#endif
#if defined _MB_CAPABLE && MB_LEN_MAX > BUF
# undef BUF
# define BUF MB_LEN_MAX
#endif
#ifndef _NO_LONGLONG
# define quad_t long long
# define u_quad_t unsigned long long
#else
# define quad_t long
# define u_quad_t unsigned long
#endif
typedef quad_t * quad_ptr_t;
typedef void *void_ptr_t;
typedef char * char_ptr_t;
typedef long * long_ptr_t;
typedef int * int_ptr_t;
typedef short * short_ptr_t;
#ifndef _NO_POS_ARGS
# ifdef NL_ARGMAX
# define MAX_POS_ARGS NL_ARGMAX
# else
# define MAX_POS_ARGS 32
# endif
union arg_val
{
int val_int;
u_int val_u_int;
long val_long;
u_long val_u_long;
float val_float;
double val_double;
_LONG_DOUBLE val__LONG_DOUBLE;
int_ptr_t val_int_ptr_t;
short_ptr_t val_short_ptr_t;
long_ptr_t val_long_ptr_t;
char_ptr_t val_char_ptr_t;
quad_ptr_t val_quad_ptr_t;
void_ptr_t val_void_ptr_t;
quad_t val_quad_t;
u_quad_t val_u_quad_t;
wint_t val_wint_t;
};
static union arg_val *
get_arg (struct _reent *data, int n, char *fmt,
va_list *ap, int *numargs, union arg_val *args,
int *arg_type, char **last_fmt);
#endif /* !_NO_POS_ARGS */
/*
* Macros for converting digits to letters and vice versa
*/
#define to_digit(c) ((c) - '0')
#define is_digit(c) ((unsigned)to_digit (c) <= 9)
#define to_char(n) ((n) + '0')
/*
* Flags used during conversion.
*/
#define ALT 0x001 /* alternate form */
#define HEXPREFIX 0x002 /* add 0x or 0X prefix */
#define LADJUST 0x004 /* left adjustment */
#define LONGDBL 0x008 /* long double */
#define LONGINT 0x010 /* long integer */
#ifndef _NO_LONGLONG
# define QUADINT 0x020 /* quad integer */
#else /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so
that %lld behaves the same as %ld, not as %d, as expected if:
sizeof (long long) = sizeof long > sizeof int */
# define QUADINT LONGINT
#endif
#define SHORTINT 0x040 /* short integer */
#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
#define FPT 0x100 /* Floating point number */
#ifdef _WANT_IO_C99_FORMATS
# define CHARINT 0x200 /* char as integer */
#else /* define as 0, to make SARG and UARG occupy fewer instructions */
# define CHARINT 0
#endif
#ifdef _WANT_IO_C99_FORMATS
# define GROUPING 0x400 /* use grouping ("'" flag) */
#endif
int _VFPRINTF_R (struct _reent *, FILE *, const char *, va_list);
#ifndef STRING_ONLY
int
VFPRINTF (FILE * fp,
const char *fmt0,
va_list ap)
{
int result;
result = _VFPRINTF_R (_REENT, fp, fmt0, ap);
return result;
}
#endif /* STRING_ONLY */
int
_VFPRINTF_R (struct _reent *data,
FILE * fp,
const char *fmt0,
va_list ap)
{
register char *fmt; /* format string */
register int ch; /* character from fmt */
register int n, m; /* handy integers (short term usage) */
register char *cp; /* handy char pointer (short term usage) */
register int flags; /* flags as above */
char *fmt_anchor; /* current format spec being processed */
#ifndef _NO_POS_ARGS
int N; /* arg number */
int arg_index; /* index into args processed directly */
int numargs; /* number of varargs read */
char *saved_fmt; /* saved fmt pointer */
union arg_val args[MAX_POS_ARGS];
int arg_type[MAX_POS_ARGS];
int is_pos_arg; /* is current format positional? */
int old_is_pos_arg; /* is current format positional? */
#endif
int ret; /* return value accumulator */
int width; /* width from format (%8d), or 0 */
int prec; /* precision from format (%.3d), or -1 */
char sign; /* sign prefix (' ', '+', '-', or \0) */
#ifdef _WANT_IO_C99_FORMATS
/* locale specific numeric grouping */
char *thousands_sep = NULL;
size_t thsnd_len = 0;
const char *grouping = NULL;
#endif
#ifdef FLOATING_POINT
char *decimal_point = _localeconv_r (data)->decimal_point;
size_t decp_len = strlen (decimal_point);
char softsign; /* temporary negative sign for floats */
union { int i; _PRINTF_FLOAT_TYPE fp; } _double_ = {0};
# define _fpvalue (_double_.fp)
int expt; /* integer value of exponent */
int expsize = 0; /* character count for expstr */
char expstr[MAXEXPLEN]; /* buffer for exponent string */
int lead; /* sig figs before decimal or group sep */
#endif /* FLOATING_POINT */
#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS)
int ndig = 0; /* actual number of digits returned by cvt */
#endif
#if defined (FLOATING_POINT) && defined (_WANT_IO_C99_FORMATS)
int nseps; /* number of group separators with ' */
int nrepeats; /* number of repeats of the last group */
#endif
u_quad_t _uquad; /* integer arguments %[diouxX] */
enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
int dprec; /* a copy of prec if [diouxX], 0 otherwise */
int realsz; /* field size expanded by dprec */
int size; /* size of converted field or string */
char *xdigs = NULL; /* digits for [xX] conversion */
#ifdef _FVWRITE_IN_STREAMIO
#define NIOV 8
struct __suio uio; /* output information: summary */
struct __siov iov[NIOV];/* ... and individual io vectors */
register struct __siov *iovp;/* for PRINT macro */
#endif
char buf[BUF]; /* space for %c, %S, %[diouxX], %[aA] */
char ox[2]; /* space for 0x hex-prefix */
#ifdef _MB_CAPABLE
wchar_t wc;
mbstate_t state; /* mbtowc calls from library must not change state */
#endif
char *malloc_buf = NULL;/* handy pointer for malloced buffers */
/*
* Choose PADSIZE to trade efficiency vs. size. If larger printf
* fields occur frequently, increase PADSIZE and make the initialisers
* below longer.
*/
#define PADSIZE 16 /* pad chunk size */
static const char blanks[PADSIZE] =
{' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
static const char zeroes[PADSIZE] =
{'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
#ifdef _MB_CAPABLE
memset (&state, '\0', sizeof (state));
#endif
/*
* BEWARE, these `goto error' on error, and PAD uses `n'.
*/
#ifdef _FVWRITE_IN_STREAMIO
#define PRINT(ptr, len) { \
iovp->iov_base = (ptr); \
iovp->iov_len = (len); \
uio.uio_resid += (len); \
iovp++; \
if (++uio.uio_iovcnt >= NIOV) { \
if (__SPRINT(data, fp, &uio)) \
goto error; \
iovp = iov; \
} \
}
#define PAD(howmany, with) { \
if ((n = (howmany)) > 0) { \
while (n > PADSIZE) { \
PRINT (with, PADSIZE); \
n -= PADSIZE; \
} \
PRINT (with, n); \
} \
}
#define PRINTANDPAD(p, ep, len, with) { \
int n = (ep) - (p); \
if (n > (len)) \
n = (len); \
if (n > 0) \
PRINT((p), n); \
PAD((len) - (n > 0 ? n : 0), (with)); \
}
#define FLUSH() { \
if (uio.uio_resid && __SPRINT(data, fp, &uio)) \
goto error; \
uio.uio_iovcnt = 0; \
iovp = iov; \
}
#else
#define PRINT(ptr, len) { \
if (__SPRINT (data, fp, (ptr), (len)) == EOF) \
goto error; \
}
#define PAD(howmany, with) { \
if ((n = (howmany)) > 0) { \
while (n > PADSIZE) { \
PRINT (with, PADSIZE); \
n -= PADSIZE; \
} \
PRINT (with, n); \
} \
}
#define PRINTANDPAD(p, ep, len, with) { \
int n = (ep) - (p); \
if (n > (len)) \
n = (len); \
if (n > 0) \
PRINT((p), n); \
PAD((len) - (n > 0 ? n : 0), (with)); \
}
#define FLUSH()
#endif
/* Macros to support positional arguments */
#ifndef _NO_POS_ARGS
# define GET_ARG(n, ap, type) \
(is_pos_arg \
? (n < numargs \
? args[n].val_##type \
: get_arg (data, n, fmt_anchor, &ap, &numargs, args, \
arg_type, &saved_fmt)->val_##type) \
: (arg_index++ < numargs \
? args[n].val_##type \
: (numargs < MAX_POS_ARGS \
? args[numargs++].val_##type = va_arg (ap, type) \
: va_arg (ap, type))))
#else
# define GET_ARG(n, ap, type) (va_arg (ap, type))
#endif
/*
* To extend shorts properly, we need both signed and unsigned
* argument extraction methods.
*/
#ifndef _NO_LONGLONG
#define SARG() \
(flags&QUADINT ? GET_ARG (N, ap, quad_t) : \
flags&LONGINT ? GET_ARG (N, ap, long) : \
flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \
flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \
(long)GET_ARG (N, ap, int))
#define UARG() \
(flags&QUADINT ? GET_ARG (N, ap, u_quad_t) : \
flags&LONGINT ? GET_ARG (N, ap, u_long) : \
flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \
flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \
(u_long)GET_ARG (N, ap, u_int))
#else
#define SARG() \
(flags&LONGINT ? GET_ARG (N, ap, long) : \
flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \
flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \
(long)GET_ARG (N, ap, int))
#define UARG() \
(flags&LONGINT ? GET_ARG (N, ap, u_long) : \
flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \
flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \
(u_long)GET_ARG (N, ap, u_int))
#endif
#ifndef STRING_ONLY
/* Initialize std streams if not dealing with sprintf family. */
CHECK_INIT (data, fp);
_newlib_flockfile_start (fp);
if (ORIENT(fp, -1) != -1) {
_newlib_flockfile_exit (fp);
return (EOF);
}
/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
if (cantwrite (data, fp)) {
_newlib_flockfile_exit (fp);
return (EOF);
}
#ifdef _UNBUF_STREAM_OPT
/* optimise fprintf(stderr) (and other unbuffered Unix files) */
if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
fp->_file >= 0) {
_newlib_flockfile_exit (fp);
return (__sbprintf (data, fp, fmt0, ap));
}
#endif
#else /* STRING_ONLY */
/* Create initial buffer if we are called by asprintf family. */
if (fp->_flags & __SMBF && !fp->_bf._base)
{
fp->_bf._base = fp->_p = _malloc_r (data, 64);
if (!fp->_p)
{
_REENT_ERRNO(data) = ENOMEM;
return EOF;
}
fp->_bf._size = 64;
}
#endif /* STRING_ONLY */
fmt = (char *)fmt0;
#ifdef _FVWRITE_IN_STREAMIO
uio.uio_iov = iovp = iov;
uio.uio_resid = 0;
uio.uio_iovcnt = 0;
#endif
ret = 0;
#ifndef _NO_POS_ARGS
arg_index = 0;
saved_fmt = NULL;
arg_type[0] = -1;
numargs = 0;
is_pos_arg = 0;
#endif
/*
* Scan the format for conversions (`%' character).
*/
for (;;) {
cp = fmt;
#ifdef _MB_CAPABLE
while ((n = __MBTOWC (data, &wc, fmt, MB_CUR_MAX,
&state)) != 0) {
if (n < 0) {
/* Wave invalid chars through. */
memset (&state, 0, sizeof state);
n = 1;
}
else if (wc == '%')
break;
fmt += n;
}
#else
while (*fmt != '\0' && *fmt != '%')
fmt += 1;
#endif
if ((m = fmt - cp) != 0) {
PRINT (cp, m);
ret += m;
}
#ifdef _MB_CAPABLE
if (n <= 0)
goto done;
#else
if (*fmt == '\0')
goto done;
#endif
fmt_anchor = fmt;
fmt++; /* skip over '%' */
flags = 0;
dprec = 0;
width = 0;
prec = -1;
sign = '\0';
#ifdef FLOATING_POINT
lead = 0;
#ifdef _WANT_IO_C99_FORMATS
nseps = nrepeats = 0;
#endif
#endif
#ifndef _NO_POS_ARGS
N = arg_index;
is_pos_arg = 0;
#endif
rflag: ch = *fmt++;
reswitch: switch (ch) {
#ifdef _WANT_IO_C99_FORMATS
case '\'':
thousands_sep = _localeconv_r (data)->thousands_sep;
thsnd_len = strlen (thousands_sep);
grouping = _localeconv_r (data)->grouping;
if (thsnd_len > 0 && grouping && *grouping)
flags |= GROUPING;
goto rflag;
#endif
case ' ':
/*
* ``If the space and + flags both appear, the space
* flag will be ignored.''
* -- ANSI X3J11
*/
if (!sign)
sign = ' ';
goto rflag;
case '#':
flags |= ALT;
goto rflag;
case '*':
#ifndef _NO_POS_ARGS
/* we must check for positional arg used for dynamic width */
n = N;
old_is_pos_arg = is_pos_arg;
is_pos_arg = 0;
if (is_digit (*fmt)) {
char *old_fmt = fmt;
n = 0;
ch = *fmt++;
do {
n = 10 * n + to_digit (ch);
ch = *fmt++;
} while (is_digit (ch));
if (ch == '$') {
if (n <= MAX_POS_ARGS) {
n -= 1;
is_pos_arg = 1;
}
else
goto error;
}
else {
fmt = old_fmt;
goto rflag;
}
}
#endif /* !_NO_POS_ARGS */
/*
* ``A negative field width argument is taken as a
* - flag followed by a positive field width.''
* -- ANSI X3J11
* They don't exclude field widths read from args.
*/
width = GET_ARG (n, ap, int);
#ifndef _NO_POS_ARGS
is_pos_arg = old_is_pos_arg;
#endif
if (width >= 0)
goto rflag;
width = -width;
/* FALLTHROUGH */
case '-':
flags |= LADJUST;
goto rflag;
case '+':
sign = '+';
goto rflag;
case '.':
if ((ch = *fmt++) == '*') {
#ifndef _NO_POS_ARGS
/* we must check for positional arg used for dynamic width */
n = N;
old_is_pos_arg = is_pos_arg;
is_pos_arg = 0;
if (is_digit (*fmt)) {
char *old_fmt = fmt;
n = 0;
ch = *fmt++;
do {
n = 10 * n + to_digit (ch);
ch = *fmt++;
} while (is_digit (ch));
if (ch == '$') {
if (n <= MAX_POS_ARGS) {
n -= 1;
is_pos_arg = 1;
}
else
goto error;
}
else {
fmt = old_fmt;
goto rflag;
}
}
#endif /* !_NO_POS_ARGS */
prec = GET_ARG (n, ap, int);
#ifndef _NO_POS_ARGS
is_pos_arg = old_is_pos_arg;
#endif
if (prec < 0)
prec = -1;
goto rflag;
}
n = 0;
while (is_digit (ch)) {
n = 10 * n + to_digit (ch);
ch = *fmt++;
}
prec = n < 0 ? -1 : n;
goto reswitch;
case '0':
/*
* ``Note that 0 is taken as a flag, not as the
* beginning of a field width.''
* -- ANSI X3J11
*/
flags |= ZEROPAD;
goto rflag;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
n = 0;
do {
n = 10 * n + to_digit (ch);
ch = *fmt++;
} while (is_digit (ch));
#ifndef _NO_POS_ARGS
if (ch == '$') {
if (n <= MAX_POS_ARGS) {
N = n - 1;
is_pos_arg = 1;
goto rflag;
}
else
goto error;
}
#endif /* !_NO_POS_ARGS */
width = n;
goto reswitch;
#ifdef FLOATING_POINT
case 'L':
flags |= LONGDBL;
goto rflag;
#endif
case 'h':
#ifdef _WANT_IO_C99_FORMATS
if (*fmt == 'h') {
fmt++;
flags |= CHARINT;
} else
#endif
flags |= SHORTINT;
goto rflag;
case 'l':
#if defined _WANT_IO_C99_FORMATS || !defined _NO_LONGLONG
if (*fmt == 'l') {
fmt++;
flags |= QUADINT;
} else
#endif
flags |= LONGINT;
goto rflag;
case 'q': /* extension */
flags |= QUADINT;
goto rflag;
#ifdef _WANT_IO_C99_FORMATS
case 'j':
if (sizeof (intmax_t) == sizeof (long))
flags |= LONGINT;
else
flags |= QUADINT;
goto rflag;
case 'z':
if (sizeof (size_t) < sizeof (int))
/* POSIX states size_t is 16 or more bits, as is short. */
flags |= SHORTINT;
else if (sizeof (size_t) == sizeof (int))
/* no flag needed */;
else if (sizeof (size_t) <= sizeof (long))
flags |= LONGINT;
else
/* POSIX states that at least one programming
environment must support size_t no wider than
long, but that means other environments can
have size_t as wide as long long. */
flags |= QUADINT;
goto rflag;
case 't':
if (sizeof (ptrdiff_t) < sizeof (int))
/* POSIX states ptrdiff_t is 16 or more bits, as
is short. */
flags |= SHORTINT;
else if (sizeof (ptrdiff_t) == sizeof (int))
/* no flag needed */;
else if (sizeof (ptrdiff_t) <= sizeof (long))
flags |= LONGINT;
else
/* POSIX states that at least one programming
environment must support ptrdiff_t no wider than
long, but that means other environments can
have ptrdiff_t as wide as long long. */
flags |= QUADINT;
goto rflag;
case 'C':
#endif /* _WANT_IO_C99_FORMATS */
case 'c':
cp = buf;
#ifdef _MB_CAPABLE
if (ch == 'C' || (flags & LONGINT)) {
mbstate_t ps;
memset ((void *)&ps, '\0', sizeof (mbstate_t));
if ((size = (int)_wcrtomb_r (data, cp,
(wchar_t)GET_ARG (N, ap, wint_t),
&ps)) == -1) {
fp->_flags |= __SERR;
goto error;
}
}
else
#endif /* _MB_CAPABLE */
{
*cp = GET_ARG (N, ap, int);
size = 1;
}
sign = '\0';
break;
case 'D': /* extension */
flags |= LONGINT;
/*FALLTHROUGH*/
case 'd':
case 'i':
_uquad = SARG ();
#ifndef _NO_LONGLONG
if ((quad_t)_uquad < 0)
#else
if ((long) _uquad < 0)
#endif
{
_uquad = -_uquad;
sign = '-';
}
base = DEC;
goto number;
#ifdef FLOATING_POINT
# ifdef _WANT_IO_C99_FORMATS
case 'a':
case 'A':
case 'F':
# endif
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
# ifdef _NO_LONGDBL
if (flags & LONGDBL) {
_fpvalue = (double) GET_ARG (N, ap, _LONG_DOUBLE);
} else {
_fpvalue = GET_ARG (N, ap, double);
}
/* do this before tricky precision changes
If the output is infinite or NaN, leading
zeros are not permitted. Otherwise, scanf
could not read what printf wrote.
*/
if (isinf (_fpvalue)) {
if (_fpvalue < 0)
sign = '-';
if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */
cp = "INF";
else
cp = "inf";
size = 3;
flags &= ~ZEROPAD;
break;
}
if (isnan (_fpvalue)) {
if (signbit (_fpvalue))
sign = '-';
if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */
cp = "NAN";
else
cp = "nan";
size = 3;
flags &= ~ZEROPAD;
break;
}