~ubuntu-branches/ubuntu/utopic/eglibc/utopic

« back to all changes in this revision

Viewing changes to .pc/ubuntu/local-CVE-2012-3406.diff/stdio-common/vfprintf.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-10-26 05:14:58 UTC
  • mfrom: (1.5.1) (4.4.22 experimental)
  • Revision ID: package-import@ubuntu.com-20121026051458-oryotr4i03ob5pab
Tags: 2.16-0ubuntu1
* Merge with unreleased 2.16 in Debian experimental, remaining changes:
  - Drop the Breaks line from libc6, which refers to a Debian transition
  - Remove the libc6 recommends on libc6-i686, which we don't build
  - Enable libc6{,-dev}-armel on armhf and libc6{-dev}-armhf on armel
  - Ship update-locale and validlocale in /usr/sbin in libc-bin
  - Don't build locales or locales-all in Ubuntu, we rely on langpacks
  - Heavily mangle the way we do service restarting on major upgrades
  - Use different MIN_KERNEL_SUPPORTED versions than Debian, due to
    buildd needs.  This should be universally bumped to 3.2.0 once all
    our buildds (including the PPA guests) are running precise kernels
  - Build i386 variants as -march=i686, build amd64 with -O3, and build
    ppc64 variants (both 64-bit and 32-bit) with -O3 -fno-tree-vectorize
  - Re-enable unsubmitted-ldconfig-cache-abi.diff and rebuild the cache
    on upgrades from previous versions that used a different constant
  - debian/patches/any/local-CVE-2012-3406.diff: switch to malloc when
    array grows too large to handle via alloca extension (CVE-2012-3406)
  - Build generic i386/i686 flavour with -mno-tls-direct-seg-refs
* Changes added/dropped with this merge while reducing our delta:
  - Stop building glibc docs from the eglibc source, and instead make
    the glibc-docs stub have a hard dependency on glibc-doc-reference
  - Remove outdated conflicts against ancient versions of ia32-libs
  - Drop the tzdata dependency from libc6, it's in required and minimal
  - Use gcc-4.7/g++-4.7 by default on all our supported architectures
  - Save our historical changelog as changelog.ubuntu in the source
  - Drop nscd's libaudit build-dep for now, as libaudit is in universe
  - Drop the unnecessary Breaks from libc6 to locales and locales-all
  - Ship xen's ld.so.conf.d snippet as /etc/ld.so.conf.d/libc6-xen.conf
* Disable hard failures on the test suite for the first upload to raring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 1991-2012   Free Software Foundation, Inc.
 
2
   This file is part of the GNU C Library.
 
3
 
 
4
   The GNU C Library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Lesser General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
   The GNU C Library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Lesser General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Lesser General Public
 
15
   License along with the GNU C Library; if not, see
 
16
   <http://www.gnu.org/licenses/>.  */
 
17
 
 
18
#include <ctype.h>
 
19
#include <limits.h>
 
20
#include <printf.h>
 
21
#include <stdarg.h>
 
22
#include <stdint.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <wchar.h>
 
27
#include <bits/libc-lock.h>
 
28
#include <sys/param.h>
 
29
#include <_itoa.h>
 
30
#include <locale/localeinfo.h>
 
31
#include <stdio.h>
 
32
#include <gnu/option-groups.h>
 
33
 
 
34
/* This code is shared between the standard stdio implementation found
 
35
   in GNU C library and the libio implementation originally found in
 
36
   GNU libg++.
 
37
 
 
38
   Beside this it is also shared between the normal and wide character
 
39
   implementation as defined in ISO/IEC 9899:1990/Amendment 1:1995.  */
 
40
 
 
41
 
 
42
#include <libioP.h>
 
43
#define FILE            _IO_FILE
 
44
#undef va_list
 
45
#define va_list _IO_va_list
 
46
#undef BUFSIZ
 
47
#define BUFSIZ          _IO_BUFSIZ
 
48
#define ARGCHECK(S, Format) \
 
49
  do                                                                          \
 
50
    {                                                                         \
 
51
      /* Check file argument for consistence.  */                             \
 
52
      CHECK_FILE (S, -1);                                                     \
 
53
      if (S->_flags & _IO_NO_WRITES)                                          \
 
54
        {                                                                     \
 
55
          S->_flags |= _IO_ERR_SEEN;                                          \
 
56
          __set_errno (EBADF);                                                \
 
57
          return -1;                                                          \
 
58
        }                                                                     \
 
59
      if (Format == NULL)                                                     \
 
60
        {                                                                     \
 
61
          MAYBE_SET_EINVAL;                                                   \
 
62
          return -1;                                                          \
 
63
        }                                                                     \
 
64
    } while (0)
 
65
#define UNBUFFERED_P(S) ((S)->_IO_file_flags & _IO_UNBUFFERED)
 
66
 
 
67
#define done_add(val) \
 
68
  do {                                                                        \
 
69
    unsigned int _val = val;                                                  \
 
70
    assert ((unsigned int) done < (unsigned int) INT_MAX);                    \
 
71
    if (__builtin_expect (INT_MAX - done < _val, 0))                          \
 
72
      {                                                                       \
 
73
        done = -1;                                                            \
 
74
         __set_errno (EOVERFLOW);                                             \
 
75
        goto all_done;                                                        \
 
76
      }                                                                       \
 
77
    done += _val;                                                             \
 
78
  } while (0)
 
79
 
 
80
#ifndef COMPILE_WPRINTF
 
81
# define vfprintf       _IO_vfprintf_internal
 
82
# define CHAR_T         char
 
83
# define UCHAR_T        unsigned char
 
84
# define INT_T          int
 
85
# define L_(Str)        Str
 
86
# define ISDIGIT(Ch)    ((unsigned int) ((Ch) - '0') < 10)
 
87
# define STR_LEN(Str)   strlen (Str)
 
88
 
 
89
# define PUT(F, S, N)   _IO_sputn ((F), (S), (N))
 
90
# define PAD(Padchar) \
 
91
  if (width > 0)                                                              \
 
92
    done_add (_IO_padn (s, (Padchar), width))
 
93
# define PUTC(C, F)     _IO_putc_unlocked (C, F)
 
94
# define ORIENT         if (_IO_vtable_offset (s) == 0 && _IO_fwide (s, -1) != -1)\
 
95
                          return -1
 
96
#else
 
97
# define vfprintf       _IO_vfwprintf
 
98
# define CHAR_T         wchar_t
 
99
/* This is a hack!!!  There should be a type uwchar_t.  */
 
100
# define UCHAR_T        unsigned int /* uwchar_t */
 
101
# define INT_T          wint_t
 
102
# define L_(Str)        L##Str
 
103
# define ISDIGIT(Ch)    ((unsigned int) ((Ch) - L'0') < 10)
 
104
# define STR_LEN(Str)   __wcslen (Str)
 
105
 
 
106
# include <_itowa.h>
 
107
 
 
108
# define PUT(F, S, N)   _IO_sputn ((F), (S), (N))
 
109
# define PAD(Padchar) \
 
110
  if (width > 0)                                                              \
 
111
    done_add (_IO_wpadn (s, (Padchar), width))
 
112
# define PUTC(C, F)     _IO_putwc_unlocked (C, F)
 
113
# define ORIENT         if (_IO_fwide (s, 1) != 1) return -1
 
114
 
 
115
# undef _itoa
 
116
# define _itoa(Val, Buf, Base, Case) _itowa (Val, Buf, Base, Case)
 
117
# define _itoa_word(Val, Buf, Base, Case) _itowa_word (Val, Buf, Base, Case)
 
118
# undef EOF
 
119
# define EOF WEOF
 
120
#endif
 
121
 
 
122
#if __OPTION_POSIX_C_LANG_WIDE_CHAR
 
123
# define MULTIBYTE_SUPPORT (1)
 
124
#else
 
125
# define MULTIBYTE_SUPPORT (0)
 
126
#endif
 
127
 
 
128
#if __OPTION_EGLIBC_LOCALE_CODE
 
129
# define LOCALE_SUPPORT (1)
 
130
#else
 
131
# define LOCALE_SUPPORT (0)
 
132
#endif
 
133
 
 
134
#include "_i18n_number.h"
 
135
 
 
136
/* Include the shared code for parsing the format string.  */
 
137
#include "printf-parse.h"
 
138
 
 
139
 
 
140
#define outchar(Ch)                                                           \
 
141
  do                                                                          \
 
142
    {                                                                         \
 
143
      register const INT_T outc = (Ch);                                       \
 
144
      if (PUTC (outc, s) == EOF || done == INT_MAX)                           \
 
145
        {                                                                     \
 
146
          done = -1;                                                          \
 
147
          goto all_done;                                                      \
 
148
        }                                                                     \
 
149
      ++done;                                                                 \
 
150
    }                                                                         \
 
151
  while (0)
 
152
 
 
153
#define outstring(String, Len)                                                \
 
154
  do                                                                          \
 
155
    {                                                                         \
 
156
      assert ((size_t) done <= (size_t) INT_MAX);                             \
 
157
      if ((size_t) PUT (s, (String), (Len)) != (size_t) (Len))                \
 
158
        {                                                                     \
 
159
          done = -1;                                                          \
 
160
          goto all_done;                                                      \
 
161
        }                                                                     \
 
162
      if (__builtin_expect (INT_MAX - done < (Len), 0))                       \
 
163
      {                                                                       \
 
164
        done = -1;                                                            \
 
165
         __set_errno (EOVERFLOW);                                             \
 
166
        goto all_done;                                                        \
 
167
      }                                                                       \
 
168
      done += (Len);                                                          \
 
169
    }                                                                         \
 
170
  while (0)
 
171
 
 
172
/* For handling long_double and longlong we use the same flag.  If
 
173
   `long' and `long long' are effectively the same type define it to
 
174
   zero.  */
 
175
#if LONG_MAX == LONG_LONG_MAX
 
176
# define is_longlong 0
 
177
#else
 
178
# define is_longlong is_long_double
 
179
#endif
 
180
 
 
181
/* If `long' and `int' is effectively the same type we don't have to
 
182
   handle `long separately.  */
 
183
#if INT_MAX == LONG_MAX
 
184
# define is_long_num    0
 
185
#else
 
186
# define is_long_num    is_long
 
187
#endif
 
188
 
 
189
 
 
190
/* Global variables.  */
 
191
static const CHAR_T null[] = L_("(null)");
 
192
 
 
193
 
 
194
/* Helper function to provide temporary buffering for unbuffered streams.  */
 
195
static int buffered_vfprintf (FILE *stream, const CHAR_T *fmt, va_list)
 
196
     __THROW __attribute__ ((noinline)) internal_function;
 
197
 
 
198
/* Handle unknown format specifier.  */
 
199
static int printf_unknown (FILE *, const struct printf_info *,
 
200
                           const void *const *) __THROW;
 
201
 
 
202
/* Group digits of number string.  */
 
203
#ifdef COMPILE_WPRINTF
 
204
static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, wchar_t)
 
205
     __THROW internal_function;
 
206
#else
 
207
static CHAR_T *group_number (CHAR_T *, CHAR_T *, const char *, const char *)
 
208
     __THROW internal_function;
 
209
#endif
 
210
 
 
211
 
 
212
/* The function itself.  */
 
213
int
 
214
vfprintf (FILE *s, const CHAR_T *format, va_list ap)
 
215
{
 
216
  /* The character used as thousands separator.  */
 
217
#ifdef COMPILE_WPRINTF
 
218
  wchar_t thousands_sep = L'\0';
 
219
#else
 
220
  const char *thousands_sep = NULL;
 
221
#endif
 
222
 
 
223
  /* The string describing the size of groups of digits.  */
 
224
  const char *grouping;
 
225
 
 
226
  /* Place to accumulate the result.  */
 
227
  int done;
 
228
 
 
229
  /* Current character in format string.  */
 
230
  const UCHAR_T *f;
 
231
 
 
232
  /* End of leading constant string.  */
 
233
  const UCHAR_T *lead_str_end;
 
234
 
 
235
  /* Points to next format specifier.  */
 
236
  const UCHAR_T *end_of_spec;
 
237
 
 
238
  /* Buffer intermediate results.  */
 
239
  CHAR_T work_buffer[1000];
 
240
  CHAR_T *workstart = NULL;
 
241
  CHAR_T *workend;
 
242
 
 
243
  /* We have to save the original argument pointer.  */
 
244
  va_list ap_save;
 
245
 
 
246
  /* Count number of specifiers we already processed.  */
 
247
  int nspecs_done;
 
248
 
 
249
  /* For the %m format we may need the current `errno' value.  */
 
250
  int save_errno = errno;
 
251
 
 
252
  /* 1 if format is in read-only memory, -1 if it is in writable memory,
 
253
     0 if unknown.  */
 
254
  int readonly_format = 0;
 
255
 
 
256
  /* For the argument descriptions, which may be allocated on the heap.  */
 
257
  void *args_malloced = NULL;
 
258
 
 
259
  /* This table maps a character into a number representing a
 
260
     class.  In each step there is a destination label for each
 
261
     class.  */
 
262
  static const uint8_t jump_table[] =
 
263
  {
 
264
    /* ' ' */  1,            0,            0, /* '#' */  4,
 
265
               0, /* '%' */ 14,            0, /* '\''*/  6,
 
266
               0,            0, /* '*' */  7, /* '+' */  2,
 
267
               0, /* '-' */  3, /* '.' */  9,            0,
 
268
    /* '0' */  5, /* '1' */  8, /* '2' */  8, /* '3' */  8,
 
269
    /* '4' */  8, /* '5' */  8, /* '6' */  8, /* '7' */  8,
 
270
    /* '8' */  8, /* '9' */  8,            0,            0,
 
271
               0,            0,            0,            0,
 
272
               0, /* 'A' */ 26,            0, /* 'C' */ 25,
 
273
               0, /* 'E' */ 19, /* F */   19, /* 'G' */ 19,
 
274
               0, /* 'I' */ 29,            0,            0,
 
275
    /* 'L' */ 12,            0,            0,            0,
 
276
               0,            0,            0, /* 'S' */ 21,
 
277
               0,            0,            0,            0,
 
278
    /* 'X' */ 18,            0, /* 'Z' */ 13,            0,
 
279
               0,            0,            0,            0,
 
280
               0, /* 'a' */ 26,            0, /* 'c' */ 20,
 
281
    /* 'd' */ 15, /* 'e' */ 19, /* 'f' */ 19, /* 'g' */ 19,
 
282
    /* 'h' */ 10, /* 'i' */ 15, /* 'j' */ 28,            0,
 
283
    /* 'l' */ 11, /* 'm' */ 24, /* 'n' */ 23, /* 'o' */ 17,
 
284
    /* 'p' */ 22, /* 'q' */ 12,            0, /* 's' */ 21,
 
285
    /* 't' */ 27, /* 'u' */ 16,            0,            0,
 
286
    /* 'x' */ 18,            0, /* 'z' */ 13
 
287
  };
 
288
 
 
289
#define NOT_IN_JUMP_RANGE(Ch) ((Ch) < L_(' ') || (Ch) > L_('z'))
 
290
#define CHAR_CLASS(Ch) (jump_table[(INT_T) (Ch) - L_(' ')])
 
291
#ifdef SHARED
 
292
  /* 'int' is enough and it saves some space on 64 bit systems.  */
 
293
# define JUMP_TABLE_TYPE const int
 
294
# define JUMP(ChExpr, table)                                                  \
 
295
      do                                                                      \
 
296
        {                                                                     \
 
297
          int offset;                                                         \
 
298
          void *__unbounded ptr;                                              \
 
299
          spec = (ChExpr);                                                    \
 
300
          offset = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown)              \
 
301
            : table[CHAR_CLASS (spec)];                                       \
 
302
          ptr = &&do_form_unknown + offset;                                   \
 
303
          goto *ptr;                                                          \
 
304
        }                                                                     \
 
305
      while (0)
 
306
#else
 
307
# define JUMP_TABLE_TYPE const void *const
 
308
# define JUMP(ChExpr, table)                                                  \
 
309
      do                                                                      \
 
310
        {                                                                     \
 
311
          const void *__unbounded ptr;                                        \
 
312
          spec = (ChExpr);                                                    \
 
313
          ptr = NOT_IN_JUMP_RANGE (spec) ? REF (form_unknown)                 \
 
314
            : table[CHAR_CLASS (spec)];                                       \
 
315
          goto *ptr;                                                          \
 
316
        }                                                                     \
 
317
      while (0)
 
318
#endif
 
319
 
 
320
#define STEP0_3_TABLE                                                         \
 
321
    /* Step 0: at the beginning.  */                                          \
 
322
    static JUMP_TABLE_TYPE step0_jumps[30] =                                  \
 
323
    {                                                                         \
 
324
      REF (form_unknown),                                                     \
 
325
      REF (flag_space),         /* for ' ' */                                 \
 
326
      REF (flag_plus),          /* for '+' */                                 \
 
327
      REF (flag_minus),         /* for '-' */                                 \
 
328
      REF (flag_hash),          /* for '<hash>' */                            \
 
329
      REF (flag_zero),          /* for '0' */                                 \
 
330
      REF (flag_quote),         /* for '\'' */                                \
 
331
      REF (width_asterics),     /* for '*' */                                 \
 
332
      REF (width),              /* for '1'...'9' */                           \
 
333
      REF (precision),          /* for '.' */                                 \
 
334
      REF (mod_half),           /* for 'h' */                                 \
 
335
      REF (mod_long),           /* for 'l' */                                 \
 
336
      REF (mod_longlong),       /* for 'L', 'q' */                            \
 
337
      REF (mod_size_t),         /* for 'z', 'Z' */                            \
 
338
      REF (form_percent),       /* for '%' */                                 \
 
339
      REF (form_integer),       /* for 'd', 'i' */                            \
 
340
      REF (form_unsigned),      /* for 'u' */                                 \
 
341
      REF (form_octal),         /* for 'o' */                                 \
 
342
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
343
      REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
344
      REF (form_character),     /* for 'c' */                                 \
 
345
      REF (form_string),        /* for 's', 'S' */                            \
 
346
      REF (form_pointer),       /* for 'p' */                                 \
 
347
      REF (form_number),        /* for 'n' */                                 \
 
348
      REF (form_strerror),      /* for 'm' */                                 \
 
349
      REF (form_wcharacter),    /* for 'C' */                                 \
 
350
      REF (form_floathex),      /* for 'A', 'a' */                            \
 
351
      REF (mod_ptrdiff_t),      /* for 't' */                                 \
 
352
      REF (mod_intmax_t),       /* for 'j' */                                 \
 
353
      REF (flag_i18n),          /* for 'I' */                                 \
 
354
    };                                                                        \
 
355
    /* Step 1: after processing width.  */                                    \
 
356
    static JUMP_TABLE_TYPE step1_jumps[30] =                                  \
 
357
    {                                                                         \
 
358
      REF (form_unknown),                                                     \
 
359
      REF (form_unknown),       /* for ' ' */                                 \
 
360
      REF (form_unknown),       /* for '+' */                                 \
 
361
      REF (form_unknown),       /* for '-' */                                 \
 
362
      REF (form_unknown),       /* for '<hash>' */                            \
 
363
      REF (form_unknown),       /* for '0' */                                 \
 
364
      REF (form_unknown),       /* for '\'' */                                \
 
365
      REF (form_unknown),       /* for '*' */                                 \
 
366
      REF (form_unknown),       /* for '1'...'9' */                           \
 
367
      REF (precision),          /* for '.' */                                 \
 
368
      REF (mod_half),           /* for 'h' */                                 \
 
369
      REF (mod_long),           /* for 'l' */                                 \
 
370
      REF (mod_longlong),       /* for 'L', 'q' */                            \
 
371
      REF (mod_size_t),         /* for 'z', 'Z' */                            \
 
372
      REF (form_percent),       /* for '%' */                                 \
 
373
      REF (form_integer),       /* for 'd', 'i' */                            \
 
374
      REF (form_unsigned),      /* for 'u' */                                 \
 
375
      REF (form_octal),         /* for 'o' */                                 \
 
376
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
377
      REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
378
      REF (form_character),     /* for 'c' */                                 \
 
379
      REF (form_string),        /* for 's', 'S' */                            \
 
380
      REF (form_pointer),       /* for 'p' */                                 \
 
381
      REF (form_number),        /* for 'n' */                                 \
 
382
      REF (form_strerror),      /* for 'm' */                                 \
 
383
      REF (form_wcharacter),    /* for 'C' */                                 \
 
384
      REF (form_floathex),      /* for 'A', 'a' */                            \
 
385
      REF (mod_ptrdiff_t),      /* for 't' */                                 \
 
386
      REF (mod_intmax_t),       /* for 'j' */                                 \
 
387
      REF (form_unknown)        /* for 'I' */                                 \
 
388
    };                                                                        \
 
389
    /* Step 2: after processing precision.  */                                \
 
390
    static JUMP_TABLE_TYPE step2_jumps[30] =                                  \
 
391
    {                                                                         \
 
392
      REF (form_unknown),                                                     \
 
393
      REF (form_unknown),       /* for ' ' */                                 \
 
394
      REF (form_unknown),       /* for '+' */                                 \
 
395
      REF (form_unknown),       /* for '-' */                                 \
 
396
      REF (form_unknown),       /* for '<hash>' */                            \
 
397
      REF (form_unknown),       /* for '0' */                                 \
 
398
      REF (form_unknown),       /* for '\'' */                                \
 
399
      REF (form_unknown),       /* for '*' */                                 \
 
400
      REF (form_unknown),       /* for '1'...'9' */                           \
 
401
      REF (form_unknown),       /* for '.' */                                 \
 
402
      REF (mod_half),           /* for 'h' */                                 \
 
403
      REF (mod_long),           /* for 'l' */                                 \
 
404
      REF (mod_longlong),       /* for 'L', 'q' */                            \
 
405
      REF (mod_size_t),         /* for 'z', 'Z' */                            \
 
406
      REF (form_percent),       /* for '%' */                                 \
 
407
      REF (form_integer),       /* for 'd', 'i' */                            \
 
408
      REF (form_unsigned),      /* for 'u' */                                 \
 
409
      REF (form_octal),         /* for 'o' */                                 \
 
410
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
411
      REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
412
      REF (form_character),     /* for 'c' */                                 \
 
413
      REF (form_string),        /* for 's', 'S' */                            \
 
414
      REF (form_pointer),       /* for 'p' */                                 \
 
415
      REF (form_number),        /* for 'n' */                                 \
 
416
      REF (form_strerror),      /* for 'm' */                                 \
 
417
      REF (form_wcharacter),    /* for 'C' */                                 \
 
418
      REF (form_floathex),      /* for 'A', 'a' */                            \
 
419
      REF (mod_ptrdiff_t),      /* for 't' */                                 \
 
420
      REF (mod_intmax_t),       /* for 'j' */                                 \
 
421
      REF (form_unknown)        /* for 'I' */                                 \
 
422
    };                                                                        \
 
423
    /* Step 3a: after processing first 'h' modifier.  */                      \
 
424
    static JUMP_TABLE_TYPE step3a_jumps[30] =                                 \
 
425
    {                                                                         \
 
426
      REF (form_unknown),                                                     \
 
427
      REF (form_unknown),       /* for ' ' */                                 \
 
428
      REF (form_unknown),       /* for '+' */                                 \
 
429
      REF (form_unknown),       /* for '-' */                                 \
 
430
      REF (form_unknown),       /* for '<hash>' */                            \
 
431
      REF (form_unknown),       /* for '0' */                                 \
 
432
      REF (form_unknown),       /* for '\'' */                                \
 
433
      REF (form_unknown),       /* for '*' */                                 \
 
434
      REF (form_unknown),       /* for '1'...'9' */                           \
 
435
      REF (form_unknown),       /* for '.' */                                 \
 
436
      REF (mod_halfhalf),       /* for 'h' */                                 \
 
437
      REF (form_unknown),       /* for 'l' */                                 \
 
438
      REF (form_unknown),       /* for 'L', 'q' */                            \
 
439
      REF (form_unknown),       /* for 'z', 'Z' */                            \
 
440
      REF (form_percent),       /* for '%' */                                 \
 
441
      REF (form_integer),       /* for 'd', 'i' */                            \
 
442
      REF (form_unsigned),      /* for 'u' */                                 \
 
443
      REF (form_octal),         /* for 'o' */                                 \
 
444
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
445
      REF (form_unknown),       /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
446
      REF (form_unknown),       /* for 'c' */                                 \
 
447
      REF (form_unknown),       /* for 's', 'S' */                            \
 
448
      REF (form_unknown),       /* for 'p' */                                 \
 
449
      REF (form_number),        /* for 'n' */                                 \
 
450
      REF (form_unknown),       /* for 'm' */                                 \
 
451
      REF (form_unknown),       /* for 'C' */                                 \
 
452
      REF (form_unknown),       /* for 'A', 'a' */                            \
 
453
      REF (form_unknown),       /* for 't' */                                 \
 
454
      REF (form_unknown),       /* for 'j' */                                 \
 
455
      REF (form_unknown)        /* for 'I' */                                 \
 
456
    };                                                                        \
 
457
    /* Step 3b: after processing first 'l' modifier.  */                      \
 
458
    static JUMP_TABLE_TYPE step3b_jumps[30] =                                 \
 
459
    {                                                                         \
 
460
      REF (form_unknown),                                                     \
 
461
      REF (form_unknown),       /* for ' ' */                                 \
 
462
      REF (form_unknown),       /* for '+' */                                 \
 
463
      REF (form_unknown),       /* for '-' */                                 \
 
464
      REF (form_unknown),       /* for '<hash>' */                            \
 
465
      REF (form_unknown),       /* for '0' */                                 \
 
466
      REF (form_unknown),       /* for '\'' */                                \
 
467
      REF (form_unknown),       /* for '*' */                                 \
 
468
      REF (form_unknown),       /* for '1'...'9' */                           \
 
469
      REF (form_unknown),       /* for '.' */                                 \
 
470
      REF (form_unknown),       /* for 'h' */                                 \
 
471
      REF (mod_longlong),       /* for 'l' */                                 \
 
472
      REF (form_unknown),       /* for 'L', 'q' */                            \
 
473
      REF (form_unknown),       /* for 'z', 'Z' */                            \
 
474
      REF (form_percent),       /* for '%' */                                 \
 
475
      REF (form_integer),       /* for 'd', 'i' */                            \
 
476
      REF (form_unsigned),      /* for 'u' */                                 \
 
477
      REF (form_octal),         /* for 'o' */                                 \
 
478
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
479
      REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
480
      REF (form_character),     /* for 'c' */                                 \
 
481
      REF (form_string),        /* for 's', 'S' */                            \
 
482
      REF (form_pointer),       /* for 'p' */                                 \
 
483
      REF (form_number),        /* for 'n' */                                 \
 
484
      REF (form_strerror),      /* for 'm' */                                 \
 
485
      REF (form_wcharacter),    /* for 'C' */                                 \
 
486
      REF (form_floathex),      /* for 'A', 'a' */                            \
 
487
      REF (form_unknown),       /* for 't' */                                 \
 
488
      REF (form_unknown),       /* for 'j' */                                 \
 
489
      REF (form_unknown)        /* for 'I' */                                 \
 
490
    }
 
491
 
 
492
#define STEP4_TABLE                                                           \
 
493
    /* Step 4: processing format specifier.  */                               \
 
494
    static JUMP_TABLE_TYPE step4_jumps[30] =                                  \
 
495
    {                                                                         \
 
496
      REF (form_unknown),                                                     \
 
497
      REF (form_unknown),       /* for ' ' */                                 \
 
498
      REF (form_unknown),       /* for '+' */                                 \
 
499
      REF (form_unknown),       /* for '-' */                                 \
 
500
      REF (form_unknown),       /* for '<hash>' */                            \
 
501
      REF (form_unknown),       /* for '0' */                                 \
 
502
      REF (form_unknown),       /* for '\'' */                                \
 
503
      REF (form_unknown),       /* for '*' */                                 \
 
504
      REF (form_unknown),       /* for '1'...'9' */                           \
 
505
      REF (form_unknown),       /* for '.' */                                 \
 
506
      REF (form_unknown),       /* for 'h' */                                 \
 
507
      REF (form_unknown),       /* for 'l' */                                 \
 
508
      REF (form_unknown),       /* for 'L', 'q' */                            \
 
509
      REF (form_unknown),       /* for 'z', 'Z' */                            \
 
510
      REF (form_percent),       /* for '%' */                                 \
 
511
      REF (form_integer),       /* for 'd', 'i' */                            \
 
512
      REF (form_unsigned),      /* for 'u' */                                 \
 
513
      REF (form_octal),         /* for 'o' */                                 \
 
514
      REF (form_hexa),          /* for 'X', 'x' */                            \
 
515
      REF (form_float),         /* for 'E', 'e', 'F', 'f', 'G', 'g' */        \
 
516
      REF (form_character),     /* for 'c' */                                 \
 
517
      REF (form_string),        /* for 's', 'S' */                            \
 
518
      REF (form_pointer),       /* for 'p' */                                 \
 
519
      REF (form_number),        /* for 'n' */                                 \
 
520
      REF (form_strerror),      /* for 'm' */                                 \
 
521
      REF (form_wcharacter),    /* for 'C' */                                 \
 
522
      REF (form_floathex),      /* for 'A', 'a' */                            \
 
523
      REF (form_unknown),       /* for 't' */                                 \
 
524
      REF (form_unknown),       /* for 'j' */                                 \
 
525
      REF (form_unknown)        /* for 'I' */                                 \
 
526
    }
 
527
 
 
528
 
 
529
#define process_arg(fspec)                                                    \
 
530
      /* Start real work.  We know about all flags and modifiers and          \
 
531
         now process the wanted format specifier.  */                         \
 
532
    LABEL (form_percent):                                                     \
 
533
      /* Write a literal "%".  */                                             \
 
534
      outchar (L_('%'));                                                      \
 
535
      break;                                                                  \
 
536
                                                                              \
 
537
    LABEL (form_integer):                                                     \
 
538
      /* Signed decimal integer.  */                                          \
 
539
      base = 10;                                                              \
 
540
                                                                              \
 
541
      if (is_longlong)                                                        \
 
542
        {                                                                     \
 
543
          long long int signed_number;                                        \
 
544
                                                                              \
 
545
          if (fspec == NULL)                                                  \
 
546
            signed_number = va_arg (ap, long long int);                       \
 
547
          else                                                                \
 
548
            signed_number = args_value[fspec->data_arg].pa_long_long_int;     \
 
549
                                                                              \
 
550
          is_negative = signed_number < 0;                                    \
 
551
          number.longlong = is_negative ? (- signed_number) : signed_number;  \
 
552
                                                                              \
 
553
          goto LABEL (longlong_number);                                       \
 
554
        }                                                                     \
 
555
      else                                                                    \
 
556
        {                                                                     \
 
557
          long int signed_number;                                             \
 
558
                                                                              \
 
559
          if (fspec == NULL)                                                  \
 
560
            {                                                                 \
 
561
              if (is_long_num)                                                \
 
562
                signed_number = va_arg (ap, long int);                        \
 
563
              else if (is_char)                                               \
 
564
                signed_number = (signed char) va_arg (ap, unsigned int);      \
 
565
              else if (!is_short)                                             \
 
566
                signed_number = va_arg (ap, int);                             \
 
567
              else                                                            \
 
568
                signed_number = (short int) va_arg (ap, unsigned int);        \
 
569
            }                                                                 \
 
570
          else                                                                \
 
571
            if (is_long_num)                                                  \
 
572
              signed_number = args_value[fspec->data_arg].pa_long_int;        \
 
573
            else if (is_char)                                                 \
 
574
              signed_number = (signed char)                                   \
 
575
                args_value[fspec->data_arg].pa_u_int;                         \
 
576
            else if (!is_short)                                               \
 
577
              signed_number = args_value[fspec->data_arg].pa_int;             \
 
578
            else                                                              \
 
579
              signed_number = (short int)                                     \
 
580
                args_value[fspec->data_arg].pa_u_int;                         \
 
581
                                                                              \
 
582
          is_negative = signed_number < 0;                                    \
 
583
          number.word = is_negative ? (- signed_number) : signed_number;      \
 
584
                                                                              \
 
585
          goto LABEL (number);                                                \
 
586
        }                                                                     \
 
587
      /* NOTREACHED */                                                        \
 
588
                                                                              \
 
589
    LABEL (form_unsigned):                                                    \
 
590
      /* Unsigned decimal integer.  */                                        \
 
591
      base = 10;                                                              \
 
592
      goto LABEL (unsigned_number);                                           \
 
593
      /* NOTREACHED */                                                        \
 
594
                                                                              \
 
595
    LABEL (form_octal):                                                       \
 
596
      /* Unsigned octal integer.  */                                          \
 
597
      base = 8;                                                               \
 
598
      goto LABEL (unsigned_number);                                           \
 
599
      /* NOTREACHED */                                                        \
 
600
                                                                              \
 
601
    LABEL (form_hexa):                                                        \
 
602
      /* Unsigned hexadecimal integer.  */                                    \
 
603
      base = 16;                                                              \
 
604
                                                                              \
 
605
    LABEL (unsigned_number):      /* Unsigned number of base BASE.  */        \
 
606
                                                                              \
 
607
      /* ISO specifies the `+' and ` ' flags only for signed                  \
 
608
         conversions.  */                                                     \
 
609
      is_negative = 0;                                                        \
 
610
      showsign = 0;                                                           \
 
611
      space = 0;                                                              \
 
612
                                                                              \
 
613
      if (is_longlong)                                                        \
 
614
        {                                                                     \
 
615
          if (fspec == NULL)                                                  \
 
616
            number.longlong = va_arg (ap, unsigned long long int);            \
 
617
          else                                                                \
 
618
            number.longlong = args_value[fspec->data_arg].pa_u_long_long_int; \
 
619
                                                                              \
 
620
        LABEL (longlong_number):                                              \
 
621
          if (prec < 0)                                                       \
 
622
            /* Supply a default precision if none was given.  */              \
 
623
            prec = 1;                                                         \
 
624
          else                                                                \
 
625
            /* We have to take care for the '0' flag.  If a precision         \
 
626
               is given it must be ignored.  */                               \
 
627
            pad = L_(' ');                                                    \
 
628
                                                                              \
 
629
          /* If the precision is 0 and the number is 0 nothing has to         \
 
630
             be written for the number, except for the 'o' format in          \
 
631
             alternate form.  */                                              \
 
632
          if (prec == 0 && number.longlong == 0)                              \
 
633
            {                                                                 \
 
634
              string = workend;                                               \
 
635
              if (base == 8 && alt)                                           \
 
636
                *--string = L_('0');                                          \
 
637
            }                                                                 \
 
638
          else                                                                \
 
639
            {                                                                 \
 
640
              /* Put the number in WORK.  */                                  \
 
641
              string = _itoa (number.longlong, workend, base,                 \
 
642
                              spec == L_('X'));                               \
 
643
              if (group && grouping)                                          \
 
644
                string = group_number (string, workend, grouping,             \
 
645
                                       thousands_sep);                        \
 
646
                                                                              \
 
647
              if (use_outdigits && base == 10)                                \
 
648
                string = _i18n_number_rewrite (string, workend, workend);     \
 
649
            }                                                                 \
 
650
          /* Simplify further test for num != 0.  */                          \
 
651
          number.word = number.longlong != 0;                                 \
 
652
        }                                                                     \
 
653
      else                                                                    \
 
654
        {                                                                     \
 
655
          if (fspec == NULL)                                                  \
 
656
            {                                                                 \
 
657
              if (is_long_num)                                                \
 
658
                number.word = va_arg (ap, unsigned long int);                 \
 
659
              else if (is_char)                                               \
 
660
                number.word = (unsigned char) va_arg (ap, unsigned int);      \
 
661
              else if (!is_short)                                             \
 
662
                number.word = va_arg (ap, unsigned int);                      \
 
663
              else                                                            \
 
664
                number.word = (unsigned short int) va_arg (ap, unsigned int); \
 
665
            }                                                                 \
 
666
          else                                                                \
 
667
            if (is_long_num)                                                  \
 
668
              number.word = args_value[fspec->data_arg].pa_u_long_int;        \
 
669
            else if (is_char)                                                 \
 
670
              number.word = (unsigned char)                                   \
 
671
                args_value[fspec->data_arg].pa_u_int;                         \
 
672
            else if (!is_short)                                               \
 
673
              number.word = args_value[fspec->data_arg].pa_u_int;             \
 
674
            else                                                              \
 
675
              number.word = (unsigned short int)                              \
 
676
                args_value[fspec->data_arg].pa_u_int;                         \
 
677
                                                                              \
 
678
        LABEL (number):                                                       \
 
679
          if (prec < 0)                                                       \
 
680
            /* Supply a default precision if none was given.  */              \
 
681
            prec = 1;                                                         \
 
682
          else                                                                \
 
683
            /* We have to take care for the '0' flag.  If a precision         \
 
684
               is given it must be ignored.  */                               \
 
685
            pad = L_(' ');                                                    \
 
686
                                                                              \
 
687
          /* If the precision is 0 and the number is 0 nothing has to         \
 
688
             be written for the number, except for the 'o' format in          \
 
689
             alternate form.  */                                              \
 
690
          if (prec == 0 && number.word == 0)                                  \
 
691
            {                                                                 \
 
692
              string = workend;                                               \
 
693
              if (base == 8 && alt)                                           \
 
694
                *--string = L_('0');                                          \
 
695
            }                                                                 \
 
696
          else                                                                \
 
697
            {                                                                 \
 
698
              /* Put the number in WORK.  */                                  \
 
699
              string = _itoa_word (number.word, workend, base,                \
 
700
                                   spec == L_('X'));                          \
 
701
              if (group && grouping)                                          \
 
702
                string = group_number (string, workend, grouping,             \
 
703
                                       thousands_sep);                        \
 
704
                                                                              \
 
705
              if (use_outdigits && base == 10)                                \
 
706
                string = _i18n_number_rewrite (string, workend, workend);     \
 
707
            }                                                                 \
 
708
        }                                                                     \
 
709
                                                                              \
 
710
      if (prec <= workend - string && number.word != 0 && alt && base == 8)   \
 
711
        /* Add octal marker.  */                                              \
 
712
        *--string = L_('0');                                                  \
 
713
                                                                              \
 
714
      prec = MAX (0, prec - (workend - string));                              \
 
715
                                                                              \
 
716
      if (!left)                                                              \
 
717
        {                                                                     \
 
718
          width -= workend - string + prec;                                   \
 
719
                                                                              \
 
720
          if (number.word != 0 && alt && base == 16)                          \
 
721
            /* Account for 0X hex marker.  */                                 \
 
722
            width -= 2;                                                       \
 
723
                                                                              \
 
724
          if (is_negative || showsign || space)                               \
 
725
            --width;                                                          \
 
726
                                                                              \
 
727
          if (pad == L_(' '))                                                 \
 
728
            {                                                                 \
 
729
              PAD (L_(' '));                                                  \
 
730
              width = 0;                                                      \
 
731
            }                                                                 \
 
732
                                                                              \
 
733
          if (is_negative)                                                    \
 
734
            outchar (L_('-'));                                                \
 
735
          else if (showsign)                                                  \
 
736
            outchar (L_('+'));                                                \
 
737
          else if (space)                                                     \
 
738
            outchar (L_(' '));                                                \
 
739
                                                                              \
 
740
          if (number.word != 0 && alt && base == 16)                          \
 
741
            {                                                                 \
 
742
              outchar (L_('0'));                                              \
 
743
              outchar (spec);                                                 \
 
744
            }                                                                 \
 
745
                                                                              \
 
746
          width += prec;                                                      \
 
747
          PAD (L_('0'));                                                      \
 
748
                                                                              \
 
749
          outstring (string, workend - string);                               \
 
750
                                                                              \
 
751
          break;                                                              \
 
752
        }                                                                     \
 
753
      else                                                                    \
 
754
        {                                                                     \
 
755
          if (is_negative)                                                    \
 
756
            {                                                                 \
 
757
              outchar (L_('-'));                                              \
 
758
              --width;                                                        \
 
759
            }                                                                 \
 
760
          else if (showsign)                                                  \
 
761
            {                                                                 \
 
762
              outchar (L_('+'));                                              \
 
763
              --width;                                                        \
 
764
            }                                                                 \
 
765
          else if (space)                                                     \
 
766
            {                                                                 \
 
767
              outchar (L_(' '));                                              \
 
768
              --width;                                                        \
 
769
            }                                                                 \
 
770
                                                                              \
 
771
          if (number.word != 0 && alt && base == 16)                          \
 
772
            {                                                                 \
 
773
              outchar (L_('0'));                                              \
 
774
              outchar (spec);                                                 \
 
775
              width -= 2;                                                     \
 
776
            }                                                                 \
 
777
                                                                              \
 
778
          width -= workend - string + prec;                                   \
 
779
                                                                              \
 
780
          if (prec > 0)                                                       \
 
781
            {                                                                 \
 
782
              int temp = width;                                               \
 
783
              width = prec;                                                   \
 
784
              PAD (L_('0'));                                                  \
 
785
              width = temp;                                                   \
 
786
            }                                                                 \
 
787
                                                                              \
 
788
          outstring (string, workend - string);                               \
 
789
                                                                              \
 
790
          PAD (L_(' '));                                                      \
 
791
          break;                                                              \
 
792
        }                                                                     \
 
793
                                                                              \
 
794
    LABEL (form_float):                                                       \
 
795
      {                                                                       \
 
796
        /* Floating-point number.  This is handled by printf_fp.c.  */        \
 
797
        const void *ptr;                                                      \
 
798
        int function_done;                                                    \
 
799
                                                                              \
 
800
        if (fspec == NULL)                                                    \
 
801
          {                                                                   \
 
802
            if (__ldbl_is_dbl)                                                \
 
803
              is_long_double = 0;                                             \
 
804
                                                                              \
 
805
            struct printf_info info = { .prec = prec,                         \
 
806
                                        .width = width,                       \
 
807
                                        .spec = spec,                         \
 
808
                                        .is_long_double = is_long_double,     \
 
809
                                        .is_short = is_short,                 \
 
810
                                        .is_long = is_long,                   \
 
811
                                        .alt = alt,                           \
 
812
                                        .space = space,                       \
 
813
                                        .left = left,                         \
 
814
                                        .showsign = showsign,                 \
 
815
                                        .group = group,                       \
 
816
                                        .pad = pad,                           \
 
817
                                        .extra = 0,                           \
 
818
                                        .i18n = use_outdigits,                \
 
819
                                        .wide = sizeof (CHAR_T) != 1 };       \
 
820
                                                                              \
 
821
            if (is_long_double)                                               \
 
822
              the_arg.pa_long_double = va_arg (ap, long double);              \
 
823
            else                                                              \
 
824
              the_arg.pa_double = va_arg (ap, double);                        \
 
825
            ptr = (const void *) &the_arg;                                    \
 
826
                                                                              \
 
827
            function_done = __printf_fp (s, &info, &ptr);                     \
 
828
          }                                                                   \
 
829
        else                                                                  \
 
830
          {                                                                   \
 
831
            ptr = (const void *) &args_value[fspec->data_arg];                \
 
832
            if (__ldbl_is_dbl)                                                \
 
833
              {                                                               \
 
834
                fspec->data_arg_type = PA_DOUBLE;                             \
 
835
                fspec->info.is_long_double = 0;                               \
 
836
              }                                                               \
 
837
                                                                              \
 
838
            function_done = __printf_fp (s, &fspec->info, &ptr);              \
 
839
          }                                                                   \
 
840
                                                                              \
 
841
        if (function_done < 0)                                                \
 
842
          {                                                                   \
 
843
            /* Error in print handler; up to handler to set errno.  */        \
 
844
            done = -1;                                                        \
 
845
            goto all_done;                                                    \
 
846
          }                                                                   \
 
847
                                                                              \
 
848
        done_add (function_done);                                             \
 
849
      }                                                                       \
 
850
      break;                                                                  \
 
851
                                                                              \
 
852
    LABEL (form_floathex):                                                    \
 
853
      {                                                                       \
 
854
        /* Floating point number printed as hexadecimal number.  */           \
 
855
        const void *ptr;                                                      \
 
856
        int function_done;                                                    \
 
857
                                                                              \
 
858
        if (fspec == NULL)                                                    \
 
859
          {                                                                   \
 
860
            if (__ldbl_is_dbl)                                                \
 
861
              is_long_double = 0;                                             \
 
862
                                                                              \
 
863
            struct printf_info info = { .prec = prec,                         \
 
864
                                        .width = width,                       \
 
865
                                        .spec = spec,                         \
 
866
                                        .is_long_double = is_long_double,     \
 
867
                                        .is_short = is_short,                 \
 
868
                                        .is_long = is_long,                   \
 
869
                                        .alt = alt,                           \
 
870
                                        .space = space,                       \
 
871
                                        .left = left,                         \
 
872
                                        .showsign = showsign,                 \
 
873
                                        .group = group,                       \
 
874
                                        .pad = pad,                           \
 
875
                                        .extra = 0,                           \
 
876
                                        .wide = sizeof (CHAR_T) != 1 };       \
 
877
                                                                              \
 
878
            if (is_long_double)                                               \
 
879
              the_arg.pa_long_double = va_arg (ap, long double);              \
 
880
            else                                                              \
 
881
              the_arg.pa_double = va_arg (ap, double);                        \
 
882
            ptr = (const void *) &the_arg;                                    \
 
883
                                                                              \
 
884
            function_done = __printf_fphex (s, &info, &ptr);                  \
 
885
          }                                                                   \
 
886
        else                                                                  \
 
887
          {                                                                   \
 
888
            ptr = (const void *) &args_value[fspec->data_arg];                \
 
889
            if (__ldbl_is_dbl)                                                \
 
890
              fspec->info.is_long_double = 0;                                 \
 
891
                                                                              \
 
892
            function_done = __printf_fphex (s, &fspec->info, &ptr);           \
 
893
          }                                                                   \
 
894
                                                                              \
 
895
        if (function_done < 0)                                                \
 
896
          {                                                                   \
 
897
            /* Error in print handler; up to handler to set errno.  */        \
 
898
            done = -1;                                                        \
 
899
            goto all_done;                                                    \
 
900
          }                                                                   \
 
901
                                                                              \
 
902
        done_add (function_done);                                             \
 
903
      }                                                                       \
 
904
      break;                                                                  \
 
905
                                                                              \
 
906
    LABEL (form_pointer):                                                     \
 
907
      /* Generic pointer.  */                                                 \
 
908
      {                                                                       \
 
909
        const void *ptr;                                                      \
 
910
        if (fspec == NULL)                                                    \
 
911
          ptr = va_arg (ap, void *);                                          \
 
912
        else                                                                  \
 
913
          ptr = args_value[fspec->data_arg].pa_pointer;                       \
 
914
        if (ptr != NULL)                                                      \
 
915
          {                                                                   \
 
916
            /* If the pointer is not NULL, write it as a %#x spec.  */        \
 
917
            base = 16;                                                        \
 
918
            number.word = (unsigned long int) ptr;                            \
 
919
            is_negative = 0;                                                  \
 
920
            alt = 1;                                                          \
 
921
            group = 0;                                                        \
 
922
            spec = L_('x');                                                   \
 
923
            goto LABEL (number);                                              \
 
924
          }                                                                   \
 
925
        else                                                                  \
 
926
          {                                                                   \
 
927
            /* Write "(nil)" for a nil pointer.  */                           \
 
928
            string = (CHAR_T *) L_("(nil)");                                  \
 
929
            /* Make sure the full string "(nil)" is printed.  */              \
 
930
            if (prec < 5)                                                     \
 
931
              prec = 5;                                                       \
 
932
            is_long = 0;        /* This is no wide-char string.  */           \
 
933
            goto LABEL (print_string);                                        \
 
934
          }                                                                   \
 
935
      }                                                                       \
 
936
      /* NOTREACHED */                                                        \
 
937
                                                                              \
 
938
    LABEL (form_number):                                                      \
 
939
      if (s->_flags2 & _IO_FLAGS2_FORTIFY)                                    \
 
940
        {                                                                     \
 
941
          if (! readonly_format)                                              \
 
942
            {                                                                 \
 
943
              extern int __readonly_area (const void *, size_t)               \
 
944
                attribute_hidden;                                             \
 
945
              readonly_format                                                 \
 
946
                = __readonly_area (format, ((STR_LEN (format) + 1)            \
 
947
                                            * sizeof (CHAR_T)));              \
 
948
            }                                                                 \
 
949
          if (readonly_format < 0)                                            \
 
950
            __libc_fatal ("*** %n in writable segment detected ***\n");       \
 
951
        }                                                                     \
 
952
      /* Answer the count of characters written.  */                          \
 
953
      if (fspec == NULL)                                                      \
 
954
        {                                                                     \
 
955
          if (is_longlong)                                                    \
 
956
            *(long long int *) va_arg (ap, void *) = done;                    \
 
957
          else if (is_long_num)                                               \
 
958
            *(long int *) va_arg (ap, void *) = done;                         \
 
959
          else if (is_char)                                                   \
 
960
            *(char *) va_arg (ap, void *) = done;                             \
 
961
          else if (!is_short)                                                 \
 
962
            *(int *) va_arg (ap, void *) = done;                              \
 
963
          else                                                                \
 
964
            *(short int *) va_arg (ap, void *) = done;                        \
 
965
        }                                                                     \
 
966
      else                                                                    \
 
967
        if (is_longlong)                                                      \
 
968
          *(long long int *) args_value[fspec->data_arg].pa_pointer = done;   \
 
969
        else if (is_long_num)                                                 \
 
970
          *(long int *) args_value[fspec->data_arg].pa_pointer = done;        \
 
971
        else if (is_char)                                                     \
 
972
          *(char *) args_value[fspec->data_arg].pa_pointer = done;            \
 
973
        else if (!is_short)                                                   \
 
974
          *(int *) args_value[fspec->data_arg].pa_pointer = done;             \
 
975
        else                                                                  \
 
976
          *(short int *) args_value[fspec->data_arg].pa_pointer = done;       \
 
977
      break;                                                                  \
 
978
                                                                              \
 
979
    LABEL (form_strerror):                                                    \
 
980
      /* Print description of error ERRNO.  */                                \
 
981
      string =                                                                \
 
982
        (CHAR_T *) __strerror_r (save_errno, (char *) work_buffer,            \
 
983
                                 sizeof work_buffer);                         \
 
984
      is_long = 0;              /* This is no wide-char string.  */           \
 
985
      goto LABEL (print_string)
 
986
 
 
987
#ifdef COMPILE_WPRINTF
 
988
# define process_string_arg(fspec) \
 
989
    LABEL (form_character):                                                   \
 
990
      /* Character.  */                                                       \
 
991
      if (is_long)                                                            \
 
992
        goto LABEL (form_wcharacter);                                         \
 
993
      --width;  /* Account for the character itself.  */                      \
 
994
      if (!left)                                                              \
 
995
        PAD (L' ');                                                           \
 
996
      if (fspec == NULL)                                                      \
 
997
        outchar (__btowc ((unsigned char) va_arg (ap, int))); /* Promoted. */ \
 
998
      else                                                                    \
 
999
        outchar (__btowc ((unsigned char)                                     \
 
1000
                          args_value[fspec->data_arg].pa_int));               \
 
1001
      if (left)                                                               \
 
1002
        PAD (L' ');                                                           \
 
1003
      break;                                                                  \
 
1004
                                                                              \
 
1005
    LABEL (form_wcharacter):                                                  \
 
1006
      {                                                                       \
 
1007
        /* Wide character.  */                                                \
 
1008
        --width;                                                              \
 
1009
        if (!left)                                                            \
 
1010
          PAD (L' ');                                                         \
 
1011
        if (fspec == NULL)                                                    \
 
1012
          outchar (va_arg (ap, wchar_t));                                     \
 
1013
        else                                                                  \
 
1014
          outchar (args_value[fspec->data_arg].pa_wchar);                     \
 
1015
        if (left)                                                             \
 
1016
          PAD (L' ');                                                         \
 
1017
      }                                                                       \
 
1018
      break;                                                                  \
 
1019
                                                                              \
 
1020
    LABEL (form_string):                                                      \
 
1021
      {                                                                       \
 
1022
        size_t len;                                                           \
 
1023
        int string_malloced;                                                  \
 
1024
                                                                              \
 
1025
        /* The string argument could in fact be `char *' or `wchar_t *'.      \
 
1026
           But this should not make a difference here.  */                    \
 
1027
        if (fspec == NULL)                                                    \
 
1028
          string = (CHAR_T *) va_arg (ap, const wchar_t *);                   \
 
1029
        else                                                                  \
 
1030
          string = (CHAR_T *) args_value[fspec->data_arg].pa_wstring;         \
 
1031
                                                                              \
 
1032
        /* Entry point for printing other strings.  */                        \
 
1033
      LABEL (print_string):                                                   \
 
1034
                                                                              \
 
1035
        string_malloced = 0;                                                  \
 
1036
        if (string == NULL)                                                   \
 
1037
          {                                                                   \
 
1038
            /* Write "(null)" if there's space.  */                           \
 
1039
            if (prec == -1                                                    \
 
1040
                || prec >= (int) (sizeof (null) / sizeof (null[0])) - 1)      \
 
1041
              {                                                               \
 
1042
                string = (CHAR_T *) null;                                     \
 
1043
                len = (sizeof (null) / sizeof (null[0])) - 1;                 \
 
1044
              }                                                               \
 
1045
            else                                                              \
 
1046
              {                                                               \
 
1047
                string = (CHAR_T *) L"";                                      \
 
1048
                len = 0;                                                      \
 
1049
              }                                                               \
 
1050
          }                                                                   \
 
1051
        else if (!is_long && spec != L_('S'))                                 \
 
1052
          {                                                                   \
 
1053
            /* This is complicated.  We have to transform the multibyte       \
 
1054
               string into a wide character string.  */                       \
 
1055
            const char *mbs = (const char *) string;                          \
 
1056
            mbstate_t mbstate;                                                \
 
1057
                                                                              \
 
1058
            len = prec != -1 ? __strnlen (mbs, (size_t) prec) : strlen (mbs); \
 
1059
                                                                              \
 
1060
            /* Allocate dynamically an array which definitely is long         \
 
1061
               enough for the wide character version.  Each byte in the       \
 
1062
               multi-byte string can produce at most one wide character.  */  \
 
1063
            if (__libc_use_alloca (len * sizeof (wchar_t)))                   \
 
1064
              string = (CHAR_T *) alloca (len * sizeof (wchar_t));            \
 
1065
            else if ((string = (CHAR_T *) malloc (len * sizeof (wchar_t)))    \
 
1066
                     == NULL)                                                 \
 
1067
              {                                                               \
 
1068
                done = -1;                                                    \
 
1069
                goto all_done;                                                \
 
1070
              }                                                               \
 
1071
            else                                                              \
 
1072
              string_malloced = 1;                                            \
 
1073
                                                                              \
 
1074
            memset (&mbstate, '\0', sizeof (mbstate_t));                      \
 
1075
            len = __mbsrtowcs (string, &mbs, len, &mbstate);                  \
 
1076
            if (len == (size_t) -1)                                           \
 
1077
              {                                                               \
 
1078
                /* Illegal multibyte character.  */                           \
 
1079
                done = -1;                                                    \
 
1080
                goto all_done;                                                \
 
1081
              }                                                               \
 
1082
          }                                                                   \
 
1083
        else                                                                  \
 
1084
          {                                                                   \
 
1085
            if (prec != -1)                                                   \
 
1086
              /* Search for the end of the string, but don't search past      \
 
1087
                 the length specified by the precision.  */                   \
 
1088
              len = __wcsnlen (string, prec);                                 \
 
1089
            else                                                              \
 
1090
              len = __wcslen (string);                                        \
 
1091
          }                                                                   \
 
1092
                                                                              \
 
1093
        if ((width -= len) < 0)                                               \
 
1094
          {                                                                   \
 
1095
            outstring (string, len);                                          \
 
1096
            break;                                                            \
 
1097
          }                                                                   \
 
1098
                                                                              \
 
1099
        if (!left)                                                            \
 
1100
          PAD (L' ');                                                         \
 
1101
        outstring (string, len);                                              \
 
1102
        if (left)                                                             \
 
1103
          PAD (L' ');                                                         \
 
1104
        if (__builtin_expect (string_malloced, 0))                            \
 
1105
          free (string);                                                      \
 
1106
      }                                                                       \
 
1107
      break;
 
1108
#else
 
1109
# define process_string_arg(fspec) \
 
1110
    LABEL (form_character):                                                   \
 
1111
      /* Character.  */                                                       \
 
1112
      if (is_long)                                                            \
 
1113
        {                                                                     \
 
1114
          assert (MULTIBYTE_SUPPORT);                                         \
 
1115
          goto LABEL (form_wcharacter);                                       \
 
1116
        }                                                                     \
 
1117
      --width;  /* Account for the character itself.  */                      \
 
1118
      if (!left)                                                              \
 
1119
        PAD (' ');                                                            \
 
1120
      if (fspec == NULL)                                                      \
 
1121
        outchar ((unsigned char) va_arg (ap, int)); /* Promoted.  */          \
 
1122
      else                                                                    \
 
1123
        outchar ((unsigned char) args_value[fspec->data_arg].pa_int);         \
 
1124
      if (left)                                                               \
 
1125
        PAD (' ');                                                            \
 
1126
      break;                                                                  \
 
1127
                                                                              \
 
1128
    LABEL (form_wcharacter):                                                  \
 
1129
      assert (MULTIBYTE_SUPPORT);                                             \
 
1130
      {                                                                       \
 
1131
        /* Wide character.  */                                                \
 
1132
        char buf[MB_CUR_MAX];                                                 \
 
1133
        mbstate_t mbstate;                                                    \
 
1134
        size_t len;                                                           \
 
1135
                                                                              \
 
1136
        memset (&mbstate, '\0', sizeof (mbstate_t));                          \
 
1137
        len = __wcrtomb (buf, (fspec == NULL ? va_arg (ap, wchar_t)           \
 
1138
                               : args_value[fspec->data_arg].pa_wchar),       \
 
1139
                         &mbstate);                                           \
 
1140
        if (len == (size_t) -1)                                               \
 
1141
          {                                                                   \
 
1142
            /* Something went wrong during the conversion.  Bail out.  */     \
 
1143
            done = -1;                                                        \
 
1144
            goto all_done;                                                    \
 
1145
          }                                                                   \
 
1146
        width -= len;                                                         \
 
1147
        if (!left)                                                            \
 
1148
          PAD (' ');                                                          \
 
1149
        outstring (buf, len);                                                 \
 
1150
        if (left)                                                             \
 
1151
          PAD (' ');                                                          \
 
1152
      }                                                                       \
 
1153
      break;                                                                  \
 
1154
                                                                              \
 
1155
    LABEL (form_string):                                                      \
 
1156
      {                                                                       \
 
1157
        size_t len;                                                           \
 
1158
        int string_malloced;                                                  \
 
1159
                                                                              \
 
1160
        /* The string argument could in fact be `char *' or `wchar_t *'.      \
 
1161
           But this should not make a difference here.  */                    \
 
1162
        if (fspec == NULL)                                                    \
 
1163
          string = (char *) va_arg (ap, const char *);                        \
 
1164
        else                                                                  \
 
1165
          string = (char *) args_value[fspec->data_arg].pa_string;            \
 
1166
                                                                              \
 
1167
        /* Entry point for printing other strings.  */                        \
 
1168
      LABEL (print_string):                                                   \
 
1169
                                                                              \
 
1170
        string_malloced = 0;                                                  \
 
1171
        if (string == NULL)                                                   \
 
1172
          {                                                                   \
 
1173
            /* Write "(null)" if there's space.  */                           \
 
1174
            if (prec == -1 || prec >= (int) sizeof (null) - 1)                \
 
1175
              {                                                               \
 
1176
                string = (char *) null;                                       \
 
1177
                len = sizeof (null) - 1;                                      \
 
1178
              }                                                               \
 
1179
            else                                                              \
 
1180
              {                                                               \
 
1181
                string = (char *) "";                                         \
 
1182
                len = 0;                                                      \
 
1183
              }                                                               \
 
1184
          }                                                                   \
 
1185
        else if (!is_long && spec != L_('S'))                                 \
 
1186
          {                                                                   \
 
1187
            if (prec != -1)                                                   \
 
1188
              /* Search for the end of the string, but don't search past      \
 
1189
                 the length (in bytes) specified by the precision.  */        \
 
1190
              len = __strnlen (string, prec);                                 \
 
1191
            else                                                              \
 
1192
              len = strlen (string);                                          \
 
1193
          }                                                                   \
 
1194
        else                                                                  \
 
1195
          {                                                                   \
 
1196
            assert (MULTIBYTE_SUPPORT);                                       \
 
1197
            const wchar_t *s2 = (const wchar_t *) string;                     \
 
1198
            mbstate_t mbstate;                                                \
 
1199
                                                                              \
 
1200
            memset (&mbstate, '\0', sizeof (mbstate_t));                      \
 
1201
                                                                              \
 
1202
            if (prec >= 0)                                                    \
 
1203
              {                                                               \
 
1204
                /* The string `s2' might not be NUL terminated.  */           \
 
1205
                if (__libc_use_alloca (prec))                                 \
 
1206
                  string = (char *) alloca (prec);                            \
 
1207
                else if ((string = (char *) malloc (prec)) == NULL)           \
 
1208
                  {                                                           \
 
1209
                    done = -1;                                                \
 
1210
                    goto all_done;                                            \
 
1211
                  }                                                           \
 
1212
                else                                                          \
 
1213
                  string_malloced = 1;                                        \
 
1214
                len = __wcsrtombs (string, &s2, prec, &mbstate);              \
 
1215
              }                                                               \
 
1216
            else                                                              \
 
1217
              {                                                               \
 
1218
                len = __wcsrtombs (NULL, &s2, 0, &mbstate);                   \
 
1219
                if (len != (size_t) -1)                                       \
 
1220
                  {                                                           \
 
1221
                    assert (__mbsinit (&mbstate));                            \
 
1222
                    s2 = (const wchar_t *) string;                            \
 
1223
                    if (__libc_use_alloca (len + 1))                          \
 
1224
                      string = (char *) alloca (len + 1);                     \
 
1225
                    else if ((string = (char *) malloc (len + 1)) == NULL)    \
 
1226
                      {                                                       \
 
1227
                        done = -1;                                            \
 
1228
                        goto all_done;                                        \
 
1229
                      }                                                       \
 
1230
                    else                                                      \
 
1231
                      string_malloced = 1;                                    \
 
1232
                    (void) __wcsrtombs (string, &s2, len + 1, &mbstate);      \
 
1233
                  }                                                           \
 
1234
              }                                                               \
 
1235
                                                                              \
 
1236
            if (len == (size_t) -1)                                           \
 
1237
              {                                                               \
 
1238
                /* Illegal wide-character string.  */                         \
 
1239
                done = -1;                                                    \
 
1240
                goto all_done;                                                \
 
1241
              }                                                               \
 
1242
          }                                                                   \
 
1243
                                                                              \
 
1244
        if ((width -= len) < 0)                                               \
 
1245
          {                                                                   \
 
1246
            outstring (string, len);                                          \
 
1247
            break;                                                            \
 
1248
          }                                                                   \
 
1249
                                                                              \
 
1250
        if (!left)                                                            \
 
1251
          PAD (' ');                                                          \
 
1252
        outstring (string, len);                                              \
 
1253
        if (left)                                                             \
 
1254
          PAD (' ');                                                          \
 
1255
        if (__builtin_expect (string_malloced, 0))                            \
 
1256
          free (string);                                                      \
 
1257
      }                                                                       \
 
1258
      break;
 
1259
#endif
 
1260
 
 
1261
  /* Orient the stream.  */
 
1262
#ifdef ORIENT
 
1263
  ORIENT;
 
1264
#endif
 
1265
 
 
1266
  /* Sanity check of arguments.  */
 
1267
  ARGCHECK (s, format);
 
1268
 
 
1269
#ifdef ORIENT
 
1270
  /* Check for correct orientation.  */
 
1271
  if (_IO_vtable_offset (s) == 0 &&
 
1272
      _IO_fwide (s, sizeof (CHAR_T) == 1 ? -1 : 1)
 
1273
      != (sizeof (CHAR_T) == 1 ? -1 : 1))
 
1274
    /* The stream is already oriented otherwise.  */
 
1275
    return EOF;
 
1276
#endif
 
1277
 
 
1278
  if (UNBUFFERED_P (s))
 
1279
    /* Use a helper function which will allocate a local temporary buffer
 
1280
       for the stream and then call us again.  */
 
1281
    return buffered_vfprintf (s, format, ap);
 
1282
 
 
1283
  /* Initialize local variables.  */
 
1284
  done = 0;
 
1285
  grouping = (const char *) -1;
 
1286
#ifdef __va_copy
 
1287
  /* This macro will be available soon in gcc's <stdarg.h>.  We need it
 
1288
     since on some systems `va_list' is not an integral type.  */
 
1289
  __va_copy (ap_save, ap);
 
1290
#else
 
1291
  ap_save = ap;
 
1292
#endif
 
1293
  nspecs_done = 0;
 
1294
 
 
1295
#ifdef COMPILE_WPRINTF
 
1296
  /* Find the first format specifier.  */
 
1297
  f = lead_str_end = __find_specwc ((const UCHAR_T *) format);
 
1298
#else
 
1299
  /* Find the first format specifier.  */
 
1300
  f = lead_str_end = __find_specmb ((const UCHAR_T *) format);
 
1301
#endif
 
1302
 
 
1303
  /* Lock stream.  */
 
1304
  _IO_cleanup_region_start ((void (*) (void *)) &_IO_funlockfile, s);
 
1305
  _IO_flockfile (s);
 
1306
 
 
1307
  /* Write the literal text before the first format.  */
 
1308
  outstring ((const UCHAR_T *) format,
 
1309
             lead_str_end - (const UCHAR_T *) format);
 
1310
 
 
1311
  /* If we only have to print a simple string, return now.  */
 
1312
  if (*f == L_('\0'))
 
1313
    goto all_done;
 
1314
 
 
1315
  /* Use the slow path in case any printf handler is registered.  */
 
1316
  if (__builtin_expect (__printf_function_table != NULL
 
1317
                        || __printf_modifier_table != NULL
 
1318
                        || __printf_va_arg_table != NULL, 0))
 
1319
    goto do_positional;
 
1320
 
 
1321
  /* Process whole format string.  */
 
1322
  do
 
1323
    {
 
1324
#ifdef SHARED
 
1325
# define REF(Name) &&do_##Name - &&do_form_unknown
 
1326
#else
 
1327
# define REF(Name) &&do_##Name
 
1328
#endif
 
1329
#define LABEL(Name) do_##Name
 
1330
      STEP0_3_TABLE;
 
1331
      STEP4_TABLE;
 
1332
 
 
1333
      union printf_arg *args_value;     /* This is not used here but ... */
 
1334
      int is_negative;  /* Flag for negative number.  */
 
1335
      union
 
1336
      {
 
1337
        unsigned long long int longlong;
 
1338
        unsigned long int word;
 
1339
      } number;
 
1340
      int base;
 
1341
      union printf_arg the_arg;
 
1342
      CHAR_T *string;   /* Pointer to argument string.  */
 
1343
      int alt = 0;      /* Alternate format.  */
 
1344
      int space = 0;    /* Use space prefix if no sign is needed.  */
 
1345
      int left = 0;     /* Left-justify output.  */
 
1346
      int showsign = 0; /* Always begin with plus or minus sign.  */
 
1347
      int group = 0;    /* Print numbers according grouping rules.  */
 
1348
      int is_long_double = 0; /* Argument is long double/ long long int.  */
 
1349
      int is_short = 0; /* Argument is short int.  */
 
1350
      int is_long = 0;  /* Argument is long int.  */
 
1351
      int is_char = 0;  /* Argument is promoted (unsigned) char.  */
 
1352
      int width = 0;    /* Width of output; 0 means none specified.  */
 
1353
      int prec = -1;    /* Precision of output; -1 means none specified.  */
 
1354
      /* This flag is set by the 'I' modifier and selects the use of the
 
1355
         `outdigits' as determined by the current locale.  */
 
1356
      int use_outdigits = 0;
 
1357
      UCHAR_T pad = L_(' ');/* Padding character.  */
 
1358
      CHAR_T spec;
 
1359
 
 
1360
      workstart = NULL;
 
1361
      workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
 
1362
 
 
1363
      /* Get current character in format string.  */
 
1364
      JUMP (*++f, step0_jumps);
 
1365
 
 
1366
      /* ' ' flag.  */
 
1367
    LABEL (flag_space):
 
1368
      space = 1;
 
1369
      JUMP (*++f, step0_jumps);
 
1370
 
 
1371
      /* '+' flag.  */
 
1372
    LABEL (flag_plus):
 
1373
      showsign = 1;
 
1374
      JUMP (*++f, step0_jumps);
 
1375
 
 
1376
      /* The '-' flag.  */
 
1377
    LABEL (flag_minus):
 
1378
      left = 1;
 
1379
      pad = L_(' ');
 
1380
      JUMP (*++f, step0_jumps);
 
1381
 
 
1382
      /* The '#' flag.  */
 
1383
    LABEL (flag_hash):
 
1384
      alt = 1;
 
1385
      JUMP (*++f, step0_jumps);
 
1386
 
 
1387
      /* The '0' flag.  */
 
1388
    LABEL (flag_zero):
 
1389
      if (!left)
 
1390
        pad = L_('0');
 
1391
      JUMP (*++f, step0_jumps);
 
1392
 
 
1393
      /* The '\'' flag.  */
 
1394
    LABEL (flag_quote):
 
1395
      group = 1;
 
1396
 
 
1397
      if (! LOCALE_SUPPORT)
 
1398
        grouping = NULL;
 
1399
      else if (grouping == (const char *) -1)
 
1400
        {
 
1401
#ifdef COMPILE_WPRINTF
 
1402
          thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
 
1403
                                            _NL_NUMERIC_THOUSANDS_SEP_WC);
 
1404
#else
 
1405
          thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
 
1406
#endif
 
1407
 
 
1408
          grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
 
1409
          if (*grouping == '\0' || *grouping == CHAR_MAX
 
1410
#ifdef COMPILE_WPRINTF
 
1411
              || thousands_sep == L'\0'
 
1412
#else
 
1413
              || *thousands_sep == '\0'
 
1414
#endif
 
1415
              )
 
1416
            grouping = NULL;
 
1417
        }
 
1418
      JUMP (*++f, step0_jumps);
 
1419
 
 
1420
    LABEL (flag_i18n):
 
1421
      use_outdigits = 1;
 
1422
      JUMP (*++f, step0_jumps);
 
1423
 
 
1424
      /* Get width from argument.  */
 
1425
    LABEL (width_asterics):
 
1426
      {
 
1427
        const UCHAR_T *tmp;     /* Temporary value.  */
 
1428
 
 
1429
        tmp = ++f;
 
1430
        if (ISDIGIT (*tmp))
 
1431
          {
 
1432
            int pos = read_int (&tmp);
 
1433
 
 
1434
            if (pos == -1)
 
1435
              {
 
1436
                __set_errno (EOVERFLOW);
 
1437
                done = -1;
 
1438
                goto all_done;
 
1439
              }
 
1440
 
 
1441
            if (pos && *tmp == L_('$'))
 
1442
              /* The width comes from a positional parameter.  */
 
1443
              goto do_positional;
 
1444
          }
 
1445
        width = va_arg (ap, int);
 
1446
 
 
1447
        /* Negative width means left justified.  */
 
1448
        if (width < 0)
 
1449
          {
 
1450
            width = -width;
 
1451
            pad = L_(' ');
 
1452
            left = 1;
 
1453
          }
 
1454
 
 
1455
        if (__builtin_expect (width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
 
1456
          {
 
1457
            __set_errno (EOVERFLOW);
 
1458
            done = -1;
 
1459
            goto all_done;
 
1460
          }
 
1461
 
 
1462
        if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
 
1463
          {
 
1464
            /* We have to use a special buffer.  The "32" is just a safe
 
1465
               bet for all the output which is not counted in the width.  */
 
1466
            size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
 
1467
            if (__libc_use_alloca (needed))
 
1468
              workend = (CHAR_T *) alloca (needed) + width + 32;
 
1469
            else
 
1470
              {
 
1471
                workstart = (CHAR_T *) malloc (needed);
 
1472
                if (workstart == NULL)
 
1473
                  {
 
1474
                    done = -1;
 
1475
                    goto all_done;
 
1476
                  }
 
1477
                workend = workstart + width + 32;
 
1478
              }
 
1479
          }
 
1480
      }
 
1481
      JUMP (*f, step1_jumps);
 
1482
 
 
1483
      /* Given width in format string.  */
 
1484
    LABEL (width):
 
1485
      width = read_int (&f);
 
1486
 
 
1487
      if (__builtin_expect (width == -1
 
1488
                            || width >= INT_MAX / sizeof (CHAR_T) - 32, 0))
 
1489
        {
 
1490
          __set_errno (EOVERFLOW);
 
1491
          done = -1;
 
1492
          goto all_done;
 
1493
        }
 
1494
 
 
1495
      if (width >= sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
 
1496
        {
 
1497
          /* We have to use a special buffer.  The "32" is just a safe
 
1498
             bet for all the output which is not counted in the width.  */
 
1499
          size_t needed = ((size_t) width + 32) * sizeof (CHAR_T);
 
1500
          if (__libc_use_alloca (needed))
 
1501
            workend = (CHAR_T *) alloca (needed) + width + 32;
 
1502
          else
 
1503
            {
 
1504
              workstart = (CHAR_T *) malloc (needed);
 
1505
              if (workstart == NULL)
 
1506
                {
 
1507
                  done = -1;
 
1508
                  goto all_done;
 
1509
                }
 
1510
              workend = workstart + width + 32;
 
1511
            }
 
1512
        }
 
1513
      if (*f == L_('$'))
 
1514
        /* Oh, oh.  The argument comes from a positional parameter.  */
 
1515
        goto do_positional;
 
1516
      JUMP (*f, step1_jumps);
 
1517
 
 
1518
    LABEL (precision):
 
1519
      ++f;
 
1520
      if (*f == L_('*'))
 
1521
        {
 
1522
          const UCHAR_T *tmp;   /* Temporary value.  */
 
1523
 
 
1524
          tmp = ++f;
 
1525
          if (ISDIGIT (*tmp))
 
1526
            {
 
1527
              int pos = read_int (&tmp);
 
1528
 
 
1529
              if (pos == -1)
 
1530
                {
 
1531
                  __set_errno (EOVERFLOW);
 
1532
                  done = -1;
 
1533
                  goto all_done;
 
1534
                }
 
1535
 
 
1536
              if (pos && *tmp == L_('$'))
 
1537
                /* The precision comes from a positional parameter.  */
 
1538
                goto do_positional;
 
1539
            }
 
1540
          prec = va_arg (ap, int);
 
1541
 
 
1542
          /* If the precision is negative the precision is omitted.  */
 
1543
          if (prec < 0)
 
1544
            prec = -1;
 
1545
        }
 
1546
      else if (ISDIGIT (*f))
 
1547
        {
 
1548
          prec = read_int (&f);
 
1549
 
 
1550
          /* The precision was specified in this case as an extremely
 
1551
             large positive value.  */
 
1552
          if (prec == -1)
 
1553
            {
 
1554
              __set_errno (EOVERFLOW);
 
1555
              done = -1;
 
1556
              goto all_done;
 
1557
            }
 
1558
        }
 
1559
      else
 
1560
        prec = 0;
 
1561
      if (prec > width
 
1562
          && prec > sizeof (work_buffer) / sizeof (work_buffer[0]) - 32)
 
1563
        {
 
1564
          if (__builtin_expect (prec >= INT_MAX / sizeof (CHAR_T) - 32, 0))
 
1565
            {
 
1566
              __set_errno (EOVERFLOW);
 
1567
              done = -1;
 
1568
              goto all_done;
 
1569
            }
 
1570
          size_t needed = ((size_t) prec + 32) * sizeof (CHAR_T);
 
1571
 
 
1572
          if (__libc_use_alloca (needed))
 
1573
            workend = (CHAR_T *) alloca (needed) + prec + 32;
 
1574
          else
 
1575
            {
 
1576
              workstart = (CHAR_T *) malloc (needed);
 
1577
              if (workstart == NULL)
 
1578
                {
 
1579
                  done = -1;
 
1580
                  goto all_done;
 
1581
                }
 
1582
              workend = workstart + prec + 32;
 
1583
            }
 
1584
        }
 
1585
      JUMP (*f, step2_jumps);
 
1586
 
 
1587
      /* Process 'h' modifier.  There might another 'h' following.  */
 
1588
    LABEL (mod_half):
 
1589
      is_short = 1;
 
1590
      JUMP (*++f, step3a_jumps);
 
1591
 
 
1592
      /* Process 'hh' modifier.  */
 
1593
    LABEL (mod_halfhalf):
 
1594
      is_short = 0;
 
1595
      is_char = 1;
 
1596
      JUMP (*++f, step4_jumps);
 
1597
 
 
1598
      /* Process 'l' modifier.  There might another 'l' following.  */
 
1599
    LABEL (mod_long):
 
1600
      is_long = 1;
 
1601
      JUMP (*++f, step3b_jumps);
 
1602
 
 
1603
      /* Process 'L', 'q', or 'll' modifier.  No other modifier is
 
1604
         allowed to follow.  */
 
1605
    LABEL (mod_longlong):
 
1606
      is_long_double = 1;
 
1607
      is_long = 1;
 
1608
      JUMP (*++f, step4_jumps);
 
1609
 
 
1610
    LABEL (mod_size_t):
 
1611
      is_long_double = sizeof (size_t) > sizeof (unsigned long int);
 
1612
      is_long = sizeof (size_t) > sizeof (unsigned int);
 
1613
      JUMP (*++f, step4_jumps);
 
1614
 
 
1615
    LABEL (mod_ptrdiff_t):
 
1616
      is_long_double = sizeof (ptrdiff_t) > sizeof (unsigned long int);
 
1617
      is_long = sizeof (ptrdiff_t) > sizeof (unsigned int);
 
1618
      JUMP (*++f, step4_jumps);
 
1619
 
 
1620
    LABEL (mod_intmax_t):
 
1621
      is_long_double = sizeof (intmax_t) > sizeof (unsigned long int);
 
1622
      is_long = sizeof (intmax_t) > sizeof (unsigned int);
 
1623
      JUMP (*++f, step4_jumps);
 
1624
 
 
1625
      /* Process current format.  */
 
1626
      while (1)
 
1627
        {
 
1628
          process_arg (((struct printf_spec *) NULL));
 
1629
          process_string_arg (((struct printf_spec *) NULL));
 
1630
 
 
1631
        LABEL (form_unknown):
 
1632
          if (spec == L_('\0'))
 
1633
            {
 
1634
              /* The format string ended before the specifier is complete.  */
 
1635
              __set_errno (EINVAL);
 
1636
              done = -1;
 
1637
              goto all_done;
 
1638
            }
 
1639
 
 
1640
          /* If we are in the fast loop force entering the complicated
 
1641
             one.  */
 
1642
          goto do_positional;
 
1643
        }
 
1644
 
 
1645
      /* The format is correctly handled.  */
 
1646
      ++nspecs_done;
 
1647
 
 
1648
      if (__builtin_expect (workstart != NULL, 0))
 
1649
        free (workstart);
 
1650
      workstart = NULL;
 
1651
 
 
1652
      /* Look for next format specifier.  */
 
1653
#ifdef COMPILE_WPRINTF
 
1654
      f = __find_specwc ((end_of_spec = ++f));
 
1655
#else
 
1656
      f = __find_specmb ((end_of_spec = ++f));
 
1657
#endif
 
1658
 
 
1659
      /* Write the following constant string.  */
 
1660
      outstring (end_of_spec, f - end_of_spec);
 
1661
    }
 
1662
  while (*f != L_('\0'));
 
1663
 
 
1664
  /* Unlock stream and return.  */
 
1665
  goto all_done;
 
1666
 
 
1667
  /* Here starts the more complex loop to handle positional parameters.  */
 
1668
do_positional:
 
1669
  {
 
1670
    /* Array with information about the needed arguments.  This has to
 
1671
       be dynamically extensible.  */
 
1672
    size_t nspecs = 0;
 
1673
    /* A more or less arbitrary start value.  */
 
1674
    size_t nspecs_size = 32 * sizeof (struct printf_spec);
 
1675
    struct printf_spec *specs = alloca (nspecs_size);
 
1676
 
 
1677
    /* The number of arguments the format string requests.  This will
 
1678
       determine the size of the array needed to store the argument
 
1679
       attributes.  */
 
1680
    size_t nargs = 0;
 
1681
    size_t bytes_per_arg;
 
1682
    union printf_arg *args_value;
 
1683
    int *args_size;
 
1684
    int *args_type;
 
1685
 
 
1686
    /* Positional parameters refer to arguments directly.  This could
 
1687
       also determine the maximum number of arguments.  Track the
 
1688
       maximum number.  */
 
1689
    size_t max_ref_arg = 0;
 
1690
 
 
1691
    /* Just a counter.  */
 
1692
    size_t cnt;
 
1693
 
 
1694
    free (workstart);
 
1695
    workstart = NULL;
 
1696
 
 
1697
    if (! LOCALE_SUPPORT)
 
1698
      grouping = NULL;
 
1699
    else if (grouping == (const char *) -1)
 
1700
      {
 
1701
#ifdef COMPILE_WPRINTF
 
1702
        thousands_sep = _NL_CURRENT_WORD (LC_NUMERIC,
 
1703
                                          _NL_NUMERIC_THOUSANDS_SEP_WC);
 
1704
#else
 
1705
        thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
 
1706
#endif
 
1707
 
 
1708
        grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
 
1709
        if (*grouping == '\0' || *grouping == CHAR_MAX)
 
1710
          grouping = NULL;
 
1711
      }
 
1712
 
 
1713
    for (f = lead_str_end; *f != L_('\0'); f = specs[nspecs++].next_fmt)
 
1714
      {
 
1715
        if (nspecs * sizeof (*specs) >= nspecs_size)
 
1716
          {
 
1717
            /* Extend the array of format specifiers.  */
 
1718
            struct printf_spec *old = specs;
 
1719
            specs = extend_alloca (specs, nspecs_size, 2 * nspecs_size);
 
1720
 
 
1721
            /* Copy the old array's elements to the new space.  */
 
1722
            memmove (specs, old, nspecs * sizeof (*specs));
 
1723
          }
 
1724
 
 
1725
        /* Parse the format specifier.  */
 
1726
#ifdef COMPILE_WPRINTF
 
1727
        nargs += __parse_one_specwc (f, nargs, &specs[nspecs], &max_ref_arg);
 
1728
#else
 
1729
        nargs += __parse_one_specmb (f, nargs, &specs[nspecs], &max_ref_arg);
 
1730
#endif
 
1731
      }
 
1732
 
 
1733
    /* Determine the number of arguments the format string consumes.  */
 
1734
    nargs = MAX (nargs, max_ref_arg);
 
1735
    /* Calculate total size needed to represent a single argument across
 
1736
       all three argument-related arrays.  */
 
1737
    bytes_per_arg = (sizeof (*args_value) + sizeof (*args_size)
 
1738
                     + sizeof (*args_type));
 
1739
 
 
1740
    /* Check for potential integer overflow.  */
 
1741
    if (__builtin_expect (nargs > INT_MAX / bytes_per_arg, 0))
 
1742
      {
 
1743
         __set_errno (EOVERFLOW);
 
1744
         done = -1;
 
1745
         goto all_done;
 
1746
      }
 
1747
 
 
1748
    /* Allocate memory for all three argument arrays.  */
 
1749
    if (__libc_use_alloca (nargs * bytes_per_arg))
 
1750
        args_value = alloca (nargs * bytes_per_arg);
 
1751
    else
 
1752
      {
 
1753
        args_value = args_malloced = malloc (nargs * bytes_per_arg);
 
1754
        if (args_value == NULL)
 
1755
          {
 
1756
            done = -1;
 
1757
            goto all_done;
 
1758
          }
 
1759
      }
 
1760
 
 
1761
    /* Set up the remaining two arrays to each point past the end of the
 
1762
       prior array, since space for all three has been allocated now.  */
 
1763
    args_size = &args_value[nargs].pa_int;
 
1764
    args_type = &args_size[nargs];
 
1765
    memset (args_type, s->_flags2 & _IO_FLAGS2_FORTIFY ? '\xff' : '\0',
 
1766
            nargs * sizeof (*args_type));
 
1767
 
 
1768
    /* XXX Could do sanity check here: If any element in ARGS_TYPE is
 
1769
       still zero after this loop, format is invalid.  For now we
 
1770
       simply use 0 as the value.  */
 
1771
 
 
1772
    /* Fill in the types of all the arguments.  */
 
1773
    for (cnt = 0; cnt < nspecs; ++cnt)
 
1774
      {
 
1775
        /* If the width is determined by an argument this is an int.  */
 
1776
        if (specs[cnt].width_arg != -1)
 
1777
          args_type[specs[cnt].width_arg] = PA_INT;
 
1778
 
 
1779
        /* If the precision is determined by an argument this is an int.  */
 
1780
        if (specs[cnt].prec_arg != -1)
 
1781
          args_type[specs[cnt].prec_arg] = PA_INT;
 
1782
 
 
1783
        switch (specs[cnt].ndata_args)
 
1784
          {
 
1785
          case 0:               /* No arguments.  */
 
1786
            break;
 
1787
          case 1:               /* One argument; we already have the
 
1788
                                   type and size.  */
 
1789
            args_type[specs[cnt].data_arg] = specs[cnt].data_arg_type;
 
1790
            args_size[specs[cnt].data_arg] = specs[cnt].size;
 
1791
            break;
 
1792
          default:
 
1793
            /* We have more than one argument for this format spec.
 
1794
               We must call the arginfo function again to determine
 
1795
               all the types.  */
 
1796
            (void) (*__printf_arginfo_table[specs[cnt].info.spec])
 
1797
              (&specs[cnt].info,
 
1798
               specs[cnt].ndata_args, &args_type[specs[cnt].data_arg],
 
1799
               &args_size[specs[cnt].data_arg]);
 
1800
            break;
 
1801
          }
 
1802
      }
 
1803
 
 
1804
    /* Now we know all the types and the order.  Fill in the argument
 
1805
       values.  */
 
1806
    for (cnt = 0; cnt < nargs; ++cnt)
 
1807
      switch (args_type[cnt])
 
1808
        {
 
1809
#define T(tag, mem, type)                                                     \
 
1810
        case tag:                                                             \
 
1811
          args_value[cnt].mem = va_arg (ap_save, type);                       \
 
1812
          break
 
1813
 
 
1814
        T (PA_WCHAR, pa_wchar, wint_t);
 
1815
        case PA_CHAR:                           /* Promoted.  */
 
1816
        case PA_INT|PA_FLAG_SHORT:              /* Promoted.  */
 
1817
#if LONG_MAX == INT_MAX
 
1818
        case PA_INT|PA_FLAG_LONG:
 
1819
#endif
 
1820
        T (PA_INT, pa_int, int);
 
1821
#if LONG_MAX == LONG_LONG_MAX
 
1822
        case PA_INT|PA_FLAG_LONG:
 
1823
#endif
 
1824
        T (PA_INT|PA_FLAG_LONG_LONG, pa_long_long_int, long long int);
 
1825
#if LONG_MAX != INT_MAX && LONG_MAX != LONG_LONG_MAX
 
1826
# error "he?"
 
1827
#endif
 
1828
        case PA_FLOAT:                          /* Promoted.  */
 
1829
        T (PA_DOUBLE, pa_double, double);
 
1830
        case PA_DOUBLE|PA_FLAG_LONG_DOUBLE:
 
1831
          if (__ldbl_is_dbl)
 
1832
            {
 
1833
              args_value[cnt].pa_double = va_arg (ap_save, double);
 
1834
              args_type[cnt] &= ~PA_FLAG_LONG_DOUBLE;
 
1835
            }
 
1836
          else
 
1837
            args_value[cnt].pa_long_double = va_arg (ap_save, long double);
 
1838
          break;
 
1839
        case PA_STRING:                         /* All pointers are the same */
 
1840
        case PA_WSTRING:                        /* All pointers are the same */
 
1841
        T (PA_POINTER, pa_pointer, void *);
 
1842
#undef T
 
1843
        default:
 
1844
          if ((args_type[cnt] & PA_FLAG_PTR) != 0)
 
1845
            args_value[cnt].pa_pointer = va_arg (ap_save, void *);
 
1846
          else if (__builtin_expect (__printf_va_arg_table != NULL, 0)
 
1847
                   && __printf_va_arg_table[args_type[cnt] - PA_LAST] != NULL)
 
1848
            {
 
1849
              args_value[cnt].pa_user = alloca (args_size[cnt]);
 
1850
              (*__printf_va_arg_table[args_type[cnt] - PA_LAST])
 
1851
                (args_value[cnt].pa_user, &ap_save);
 
1852
            }
 
1853
          else
 
1854
            args_value[cnt].pa_long_double = 0.0;
 
1855
          break;
 
1856
        case -1:
 
1857
          /* Error case.  Not all parameters appear in N$ format
 
1858
             strings.  We have no way to determine their type.  */
 
1859
          assert (s->_flags2 & _IO_FLAGS2_FORTIFY);
 
1860
          __libc_fatal ("*** invalid %N$ use detected ***\n");
 
1861
        }
 
1862
 
 
1863
    /* Now walk through all format specifiers and process them.  */
 
1864
    for (; (size_t) nspecs_done < nspecs; ++nspecs_done)
 
1865
      {
 
1866
#undef REF
 
1867
#ifdef SHARED
 
1868
# define REF(Name) &&do2_##Name - &&do_form_unknown
 
1869
#else
 
1870
# define REF(Name) &&do2_##Name
 
1871
#endif
 
1872
#undef LABEL
 
1873
#define LABEL(Name) do2_##Name
 
1874
        STEP4_TABLE;
 
1875
 
 
1876
        int is_negative;
 
1877
        union
 
1878
        {
 
1879
          unsigned long long int longlong;
 
1880
          unsigned long int word;
 
1881
        } number;
 
1882
        int base;
 
1883
        union printf_arg the_arg;
 
1884
        CHAR_T *string;         /* Pointer to argument string.  */
 
1885
 
 
1886
        /* Fill variables from values in struct.  */
 
1887
        int alt = specs[nspecs_done].info.alt;
 
1888
        int space = specs[nspecs_done].info.space;
 
1889
        int left = specs[nspecs_done].info.left;
 
1890
        int showsign = specs[nspecs_done].info.showsign;
 
1891
        int group = specs[nspecs_done].info.group;
 
1892
        int is_long_double = specs[nspecs_done].info.is_long_double;
 
1893
        int is_short = specs[nspecs_done].info.is_short;
 
1894
        int is_char = specs[nspecs_done].info.is_char;
 
1895
        int is_long = specs[nspecs_done].info.is_long;
 
1896
        int width = specs[nspecs_done].info.width;
 
1897
        int prec = specs[nspecs_done].info.prec;
 
1898
        int use_outdigits = specs[nspecs_done].info.i18n;
 
1899
        char pad = specs[nspecs_done].info.pad;
 
1900
        CHAR_T spec = specs[nspecs_done].info.spec;
 
1901
 
 
1902
        workstart = NULL;
 
1903
        workend = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
 
1904
 
 
1905
        /* Fill in last information.  */
 
1906
        if (specs[nspecs_done].width_arg != -1)
 
1907
          {
 
1908
            /* Extract the field width from an argument.  */
 
1909
            specs[nspecs_done].info.width =
 
1910
              args_value[specs[nspecs_done].width_arg].pa_int;
 
1911
 
 
1912
            if (specs[nspecs_done].info.width < 0)
 
1913
              /* If the width value is negative left justification is
 
1914
                 selected and the value is taken as being positive.  */
 
1915
              {
 
1916
                specs[nspecs_done].info.width *= -1;
 
1917
                left = specs[nspecs_done].info.left = 1;
 
1918
              }
 
1919
            width = specs[nspecs_done].info.width;
 
1920
          }
 
1921
 
 
1922
        if (specs[nspecs_done].prec_arg != -1)
 
1923
          {
 
1924
            /* Extract the precision from an argument.  */
 
1925
            specs[nspecs_done].info.prec =
 
1926
              args_value[specs[nspecs_done].prec_arg].pa_int;
 
1927
 
 
1928
            if (specs[nspecs_done].info.prec < 0)
 
1929
              /* If the precision is negative the precision is
 
1930
                 omitted.  */
 
1931
              specs[nspecs_done].info.prec = -1;
 
1932
 
 
1933
            prec = specs[nspecs_done].info.prec;
 
1934
          }
 
1935
 
 
1936
        /* Maybe the buffer is too small.  */
 
1937
        if (MAX (prec, width) + 32 > (int) (sizeof (work_buffer)
 
1938
                                            / sizeof (CHAR_T)))
 
1939
          {
 
1940
            if (__libc_use_alloca ((MAX (prec, width) + 32)
 
1941
                                   * sizeof (CHAR_T)))
 
1942
              workend = ((CHAR_T *) alloca ((MAX (prec, width) + 32)
 
1943
                                            * sizeof (CHAR_T))
 
1944
                         + (MAX (prec, width) + 32));
 
1945
            else
 
1946
              {
 
1947
                workstart = (CHAR_T *) malloc ((MAX (prec, width) + 32)
 
1948
                                               * sizeof (CHAR_T));
 
1949
                workend = workstart + (MAX (prec, width) + 32);
 
1950
              }
 
1951
          }
 
1952
 
 
1953
        /* Process format specifiers.  */
 
1954
        while (1)
 
1955
          {
 
1956
            extern printf_function **__printf_function_table;
 
1957
            int function_done;
 
1958
 
 
1959
            if (spec <= UCHAR_MAX
 
1960
                && __printf_function_table != NULL
 
1961
                && __printf_function_table[(size_t) spec] != NULL)
 
1962
              {
 
1963
                const void **ptr = alloca (specs[nspecs_done].ndata_args
 
1964
                                           * sizeof (const void *));
 
1965
 
 
1966
                /* Fill in an array of pointers to the argument values.  */
 
1967
                for (unsigned int i = 0; i < specs[nspecs_done].ndata_args;
 
1968
                     ++i)
 
1969
                  ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
 
1970
 
 
1971
                /* Call the function.  */
 
1972
                function_done = __printf_function_table[(size_t) spec]
 
1973
                  (s, &specs[nspecs_done].info, ptr);
 
1974
 
 
1975
                if (function_done != -2)
 
1976
                  {
 
1977
                    /* If an error occurred we don't have information
 
1978
                       about # of chars.  */
 
1979
                    if (function_done < 0)
 
1980
                      {
 
1981
                        /* Function has set errno.  */
 
1982
                        done = -1;
 
1983
                        goto all_done;
 
1984
                      }
 
1985
 
 
1986
                    done_add (function_done);
 
1987
                    break;
 
1988
                  }
 
1989
              }
 
1990
 
 
1991
            JUMP (spec, step4_jumps);
 
1992
 
 
1993
            process_arg ((&specs[nspecs_done]));
 
1994
            process_string_arg ((&specs[nspecs_done]));
 
1995
 
 
1996
          LABEL (form_unknown):
 
1997
            {
 
1998
              unsigned int i;
 
1999
              const void **ptr;
 
2000
 
 
2001
              ptr = alloca (specs[nspecs_done].ndata_args
 
2002
                            * sizeof (const void *));
 
2003
 
 
2004
              /* Fill in an array of pointers to the argument values.  */
 
2005
              for (i = 0; i < specs[nspecs_done].ndata_args; ++i)
 
2006
                ptr[i] = &args_value[specs[nspecs_done].data_arg + i];
 
2007
 
 
2008
              /* Call the function.  */
 
2009
              function_done = printf_unknown (s, &specs[nspecs_done].info,
 
2010
                                              ptr);
 
2011
 
 
2012
              /* If an error occurred we don't have information about #
 
2013
                 of chars.  */
 
2014
              if (function_done < 0)
 
2015
                {
 
2016
                  /* Function has set errno.  */
 
2017
                  done = -1;
 
2018
                  goto all_done;
 
2019
                }
 
2020
 
 
2021
              done_add (function_done);
 
2022
            }
 
2023
            break;
 
2024
          }
 
2025
 
 
2026
        free (workstart);
 
2027
        workstart = NULL;
 
2028
 
 
2029
        /* Write the following constant string.  */
 
2030
        outstring (specs[nspecs_done].end_of_fmt,
 
2031
                   specs[nspecs_done].next_fmt
 
2032
                   - specs[nspecs_done].end_of_fmt);
 
2033
      }
 
2034
  }
 
2035
 
 
2036
all_done:
 
2037
  free (args_malloced);
 
2038
  free (workstart);
 
2039
  /* Unlock the stream.  */
 
2040
  _IO_funlockfile (s);
 
2041
  _IO_cleanup_region_end (0);
 
2042
 
 
2043
  return done;
 
2044
}
 
2045
 
 
2046
/* Handle an unknown format specifier.  This prints out a canonicalized
 
2047
   representation of the format spec itself.  */
 
2048
static int
 
2049
printf_unknown (FILE *s, const struct printf_info *info,
 
2050
                const void *const *args)
 
2051
 
 
2052
{
 
2053
  int done = 0;
 
2054
  CHAR_T work_buffer[MAX (sizeof (info->width), sizeof (info->prec)) * 3];
 
2055
  CHAR_T *const workend
 
2056
    = &work_buffer[sizeof (work_buffer) / sizeof (CHAR_T)];
 
2057
  register CHAR_T *w;
 
2058
 
 
2059
  outchar (L_('%'));
 
2060
 
 
2061
  if (info->alt)
 
2062
    outchar (L_('#'));
 
2063
  if (info->group)
 
2064
    outchar (L_('\''));
 
2065
  if (info->showsign)
 
2066
    outchar (L_('+'));
 
2067
  else if (info->space)
 
2068
    outchar (L_(' '));
 
2069
  if (info->left)
 
2070
    outchar (L_('-'));
 
2071
  if (info->pad == L_('0'))
 
2072
    outchar (L_('0'));
 
2073
  if (info->i18n)
 
2074
    outchar (L_('I'));
 
2075
 
 
2076
  if (info->width != 0)
 
2077
    {
 
2078
      w = _itoa_word (info->width, workend, 10, 0);
 
2079
      while (w < workend)
 
2080
        outchar (*w++);
 
2081
    }
 
2082
 
 
2083
  if (info->prec != -1)
 
2084
    {
 
2085
      outchar (L_('.'));
 
2086
      w = _itoa_word (info->prec, workend, 10, 0);
 
2087
      while (w < workend)
 
2088
        outchar (*w++);
 
2089
    }
 
2090
 
 
2091
  if (info->spec != L_('\0'))
 
2092
    outchar (info->spec);
 
2093
 
 
2094
 all_done:
 
2095
  return done;
 
2096
}
 
2097
 
 
2098
/* Group the digits according to the grouping rules of the current locale.
 
2099
   The interpretation of GROUPING is as in `struct lconv' from <locale.h>.  */
 
2100
static CHAR_T *
 
2101
internal_function
 
2102
group_number (CHAR_T *w, CHAR_T *rear_ptr, const char *grouping,
 
2103
#ifdef COMPILE_WPRINTF
 
2104
              wchar_t thousands_sep
 
2105
#else
 
2106
              const char *thousands_sep
 
2107
#endif
 
2108
              )
 
2109
{
 
2110
  int len;
 
2111
  CHAR_T *src, *s;
 
2112
#ifndef COMPILE_WPRINTF
 
2113
  int tlen = strlen (thousands_sep);
 
2114
#endif
 
2115
 
 
2116
  /* We treat all negative values like CHAR_MAX.  */
 
2117
 
 
2118
  if (*grouping == CHAR_MAX || *grouping <= 0)
 
2119
    /* No grouping should be done.  */
 
2120
    return w;
 
2121
 
 
2122
  len = *grouping++;
 
2123
 
 
2124
  /* Copy existing string so that nothing gets overwritten.  */
 
2125
  src = (CHAR_T *) alloca ((rear_ptr - w) * sizeof (CHAR_T));
 
2126
  s = (CHAR_T *) __mempcpy (src, w,
 
2127
                            (rear_ptr - w) * sizeof (CHAR_T));
 
2128
  w = rear_ptr;
 
2129
 
 
2130
  /* Process all characters in the string.  */
 
2131
  while (s > src)
 
2132
    {
 
2133
      *--w = *--s;
 
2134
 
 
2135
      if (--len == 0 && s > src)
 
2136
        {
 
2137
          /* A new group begins.  */
 
2138
#ifdef COMPILE_WPRINTF
 
2139
          *--w = thousands_sep;
 
2140
#else
 
2141
          int cnt = tlen;
 
2142
          do
 
2143
            *--w = thousands_sep[--cnt];
 
2144
          while (cnt > 0);
 
2145
#endif
 
2146
 
 
2147
          if (*grouping == CHAR_MAX
 
2148
#if CHAR_MIN < 0
 
2149
                   || *grouping < 0
 
2150
#endif
 
2151
                   )
 
2152
            {
 
2153
              /* No further grouping to be done.
 
2154
                 Copy the rest of the number.  */
 
2155
              do
 
2156
                *--w = *--s;
 
2157
              while (s > src);
 
2158
              break;
 
2159
            }
 
2160
          else if (*grouping != '\0')
 
2161
            /* The previous grouping repeats ad infinitum.  */
 
2162
            len = *grouping++;
 
2163
          else
 
2164
            len = grouping[-1];
 
2165
        }
 
2166
    }
 
2167
  return w;
 
2168
}
 
2169
 
 
2170
/* Helper "class" for `fprintf to unbuffered': creates a temporary buffer.  */
 
2171
struct helper_file
 
2172
  {
 
2173
    struct _IO_FILE_plus _f;
 
2174
#ifdef COMPILE_WPRINTF
 
2175
    struct _IO_wide_data _wide_data;
 
2176
#endif
 
2177
    _IO_FILE *_put_stream;
 
2178
#ifdef _IO_MTSAFE_IO
 
2179
    _IO_lock_t lock;
 
2180
#endif
 
2181
  };
 
2182
 
 
2183
static int
 
2184
_IO_helper_overflow (_IO_FILE *s, int c)
 
2185
{
 
2186
  _IO_FILE *target = ((struct helper_file*) s)->_put_stream;
 
2187
#ifdef COMPILE_WPRINTF
 
2188
  int used = s->_wide_data->_IO_write_ptr - s->_wide_data->_IO_write_base;
 
2189
  if (used)
 
2190
    {
 
2191
      _IO_size_t written = _IO_sputn (target, s->_wide_data->_IO_write_base,
 
2192
                                      used);
 
2193
      if (written == 0 || written == WEOF)
 
2194
        return WEOF;
 
2195
      __wmemmove (s->_wide_data->_IO_write_base,
 
2196
                  s->_wide_data->_IO_write_base + written,
 
2197
                  used - written);
 
2198
      s->_wide_data->_IO_write_ptr -= written;
 
2199
    }
 
2200
#else
 
2201
  int used = s->_IO_write_ptr - s->_IO_write_base;
 
2202
  if (used)
 
2203
    {
 
2204
      _IO_size_t written = _IO_sputn (target, s->_IO_write_base, used);
 
2205
      if (written == 0 || written == EOF)
 
2206
        return EOF;
 
2207
      memmove (s->_IO_write_base, s->_IO_write_base + written,
 
2208
               used - written);
 
2209
      s->_IO_write_ptr -= written;
 
2210
    }
 
2211
#endif
 
2212
  return PUTC (c, s);
 
2213
}
 
2214
 
 
2215
#ifdef COMPILE_WPRINTF
 
2216
static const struct _IO_jump_t _IO_helper_jumps =
 
2217
{
 
2218
  JUMP_INIT_DUMMY,
 
2219
  JUMP_INIT (finish, _IO_wdefault_finish),
 
2220
  JUMP_INIT (overflow, _IO_helper_overflow),
 
2221
  JUMP_INIT (underflow, _IO_default_underflow),
 
2222
  JUMP_INIT (uflow, _IO_default_uflow),
 
2223
  JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wdefault_pbackfail),
 
2224
  JUMP_INIT (xsputn, _IO_wdefault_xsputn),
 
2225
  JUMP_INIT (xsgetn, _IO_wdefault_xsgetn),
 
2226
  JUMP_INIT (seekoff, _IO_default_seekoff),
 
2227
  JUMP_INIT (seekpos, _IO_default_seekpos),
 
2228
  JUMP_INIT (setbuf, _IO_default_setbuf),
 
2229
  JUMP_INIT (sync, _IO_default_sync),
 
2230
  JUMP_INIT (doallocate, _IO_wdefault_doallocate),
 
2231
  JUMP_INIT (read, _IO_default_read),
 
2232
  JUMP_INIT (write, _IO_default_write),
 
2233
  JUMP_INIT (seek, _IO_default_seek),
 
2234
  JUMP_INIT (close, _IO_default_close),
 
2235
  JUMP_INIT (stat, _IO_default_stat)
 
2236
};
 
2237
#else
 
2238
static const struct _IO_jump_t _IO_helper_jumps =
 
2239
{
 
2240
  JUMP_INIT_DUMMY,
 
2241
  JUMP_INIT (finish, _IO_default_finish),
 
2242
  JUMP_INIT (overflow, _IO_helper_overflow),
 
2243
  JUMP_INIT (underflow, _IO_default_underflow),
 
2244
  JUMP_INIT (uflow, _IO_default_uflow),
 
2245
  JUMP_INIT (pbackfail, _IO_default_pbackfail),
 
2246
  JUMP_INIT (xsputn, _IO_default_xsputn),
 
2247
  JUMP_INIT (xsgetn, _IO_default_xsgetn),
 
2248
  JUMP_INIT (seekoff, _IO_default_seekoff),
 
2249
  JUMP_INIT (seekpos, _IO_default_seekpos),
 
2250
  JUMP_INIT (setbuf, _IO_default_setbuf),
 
2251
  JUMP_INIT (sync, _IO_default_sync),
 
2252
  JUMP_INIT (doallocate, _IO_default_doallocate),
 
2253
  JUMP_INIT (read, _IO_default_read),
 
2254
  JUMP_INIT (write, _IO_default_write),
 
2255
  JUMP_INIT (seek, _IO_default_seek),
 
2256
  JUMP_INIT (close, _IO_default_close),
 
2257
  JUMP_INIT (stat, _IO_default_stat)
 
2258
};
 
2259
#endif
 
2260
 
 
2261
static int
 
2262
internal_function
 
2263
buffered_vfprintf (register _IO_FILE *s, const CHAR_T *format,
 
2264
                   _IO_va_list args)
 
2265
{
 
2266
  CHAR_T buf[_IO_BUFSIZ];
 
2267
  struct helper_file helper;
 
2268
  register _IO_FILE *hp = (_IO_FILE *) &helper._f;
 
2269
  int result, to_flush;
 
2270
 
 
2271
  /* Orient the stream.  */
 
2272
#ifdef ORIENT
 
2273
  ORIENT;
 
2274
#endif
 
2275
 
 
2276
  /* Initialize helper.  */
 
2277
  helper._put_stream = s;
 
2278
#ifdef COMPILE_WPRINTF
 
2279
  hp->_wide_data = &helper._wide_data;
 
2280
  _IO_wsetp (hp, buf, buf + sizeof buf / sizeof (CHAR_T));
 
2281
  hp->_mode = 1;
 
2282
#else
 
2283
  _IO_setp (hp, buf, buf + sizeof buf);
 
2284
  hp->_mode = -1;
 
2285
#endif
 
2286
  hp->_IO_file_flags = _IO_MAGIC|_IO_NO_READS|_IO_USER_LOCK;
 
2287
#if _IO_JUMPS_OFFSET
 
2288
  hp->_vtable_offset = 0;
 
2289
#endif
 
2290
#ifdef _IO_MTSAFE_IO
 
2291
  hp->_lock = NULL;
 
2292
#endif
 
2293
  hp->_flags2 = s->_flags2;
 
2294
  _IO_JUMPS (&helper._f) = (struct _IO_jump_t *) &_IO_helper_jumps;
 
2295
 
 
2296
  /* Now print to helper instead.  */
 
2297
#ifndef COMPILE_WPRINTF
 
2298
  result = _IO_vfprintf (hp, format, args);
 
2299
#else
 
2300
  result = vfprintf (hp, format, args);
 
2301
#endif
 
2302
 
 
2303
  /* Lock stream.  */
 
2304
  __libc_cleanup_region_start (1, (void (*) (void *)) &_IO_funlockfile, s);
 
2305
  _IO_flockfile (s);
 
2306
 
 
2307
  /* Now flush anything from the helper to the S. */
 
2308
#ifdef COMPILE_WPRINTF
 
2309
  if ((to_flush = (hp->_wide_data->_IO_write_ptr
 
2310
                   - hp->_wide_data->_IO_write_base)) > 0)
 
2311
    {
 
2312
      if ((int) _IO_sputn (s, hp->_wide_data->_IO_write_base, to_flush)
 
2313
          != to_flush)
 
2314
        result = -1;
 
2315
    }
 
2316
#else
 
2317
  if ((to_flush = hp->_IO_write_ptr - hp->_IO_write_base) > 0)
 
2318
    {
 
2319
      if ((int) _IO_sputn (s, hp->_IO_write_base, to_flush) != to_flush)
 
2320
        result = -1;
 
2321
    }
 
2322
#endif
 
2323
 
 
2324
  /* Unlock the stream.  */
 
2325
  _IO_funlockfile (s);
 
2326
  __libc_cleanup_region_end (0);
 
2327
 
 
2328
  return result;
 
2329
}
 
2330
 
 
2331
#undef vfprintf
 
2332
#ifdef COMPILE_WPRINTF
 
2333
strong_alias (_IO_vfwprintf, __vfwprintf);
 
2334
ldbl_weak_alias (_IO_vfwprintf, vfwprintf);
 
2335
#else
 
2336
ldbl_strong_alias (_IO_vfprintf_internal, vfprintf);
 
2337
ldbl_hidden_def (_IO_vfprintf_internal, vfprintf)
 
2338
ldbl_strong_alias (_IO_vfprintf_internal, _IO_vfprintf);
 
2339
ldbl_hidden_def (_IO_vfprintf_internal, _IO_vfprintf)
 
2340
#endif