mktime: check signed shifts on long_int and time_t, too
[glibc.git] / stdio-common / printf-prs.c
blob68a9c61a137815cd2881562d33ce5d20fe7a36df
1 /* Copyright (C) 1991, 1992, 1995, 1996, 1999, 2000, 2002-2005, 2007, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <printf.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24 #include <sys/param.h>
26 #include "../locale/localeinfo.h"
28 #ifndef COMPILE_WPRINTF
29 # define CHAR_T char
30 # define UCHAR_T unsigned char
31 # define INT_T int
32 # define L_(Str) Str
33 # define ISDIGIT(Ch) isdigit (Ch)
34 # define ISASCII(Ch) isascii (Ch)
35 # define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St)
37 # define PUT(F, S, N) _IO_sputn (F, S, N)
38 # define PAD(Padchar) \
39 if (width > 0) \
40 done += INTUSE(_IO_padn) (s, Padchar, width)
41 #else
42 # define vfprintf vfwprintf
43 # define CHAR_T wchar_t
44 # define UCHAR_T uwchar_t
45 # define INT_T wint_t
46 # define L_(Str) L##Str
47 # define ISDIGIT(Ch) iswdigit (Ch)
49 # define PUT(F, S, N) _IO_sputn (F, S, N)
50 # define PAD(Padchar) \
51 if (width > 0) \
52 done += _IO_wpadn (s, Padchar, width)
53 #endif
55 #define DONT_NEED_READ_INT
56 #include "printf-parse.h"
59 size_t
60 parse_printf_format (fmt, n, argtypes)
61 const char *fmt;
62 size_t n;
63 int *argtypes;
65 size_t nargs; /* Number of arguments. */
66 size_t max_ref_arg; /* Highest index used in a positional arg. */
67 struct printf_spec spec;
68 const unsigned char *f = (const unsigned char *) fmt;
70 nargs = 0;
71 max_ref_arg = 0;
73 /* Search for format specifications. */
74 for (f = __find_specmb (f); *f != '\0'; f = spec.next_fmt)
76 /* Parse this spec. */
77 nargs += __parse_one_specmb (f, nargs, &spec, &max_ref_arg);
79 /* If the width is determined by an argument this is an int. */
80 if (spec.width_arg != -1 && (size_t) spec.width_arg < n)
81 argtypes[spec.width_arg] = PA_INT;
83 /* If the precision is determined by an argument this is an int. */
84 if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n)
85 argtypes[spec.prec_arg] = PA_INT;
87 if ((size_t) spec.data_arg < n)
88 switch (spec.ndata_args)
90 case 0: /* No arguments. */
91 break;
92 case 1: /* One argument; we already have the type. */
93 argtypes[spec.data_arg] = spec.data_arg_type;
94 break;
95 default:
96 /* We have more than one argument for this format spec. We must
97 call the arginfo function again to determine all the types. */
98 (void) (*__printf_arginfo_table[spec.info.spec])
99 (&spec.info, n - spec.data_arg, &argtypes[spec.data_arg],
100 &spec.size);
101 break;
105 return MAX (nargs, max_ref_arg);