]>
git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/fmtcheck.c
1 /* $NetBSD: fmtcheck.c,v 1.8 2008/04/28 20:22:59 martin Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Allen Briggs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/lib/libc/gen/fmtcheck.c,v 1.10 2009/06/23 23:52:12 delphij Exp $");
38 __weak_reference(__fmtcheck
, fmtcheck
);
40 enum __e_fmtcheck_types
{
51 FMTCHECK_SHORTPOINTER
,
55 FMTCHECK_INTMAXTPOINTER
,
56 FMTCHECK_PTRDIFFTPOINTER
,
57 FMTCHECK_SIZETPOINTER
,
58 #ifndef NO_FLOATING_POINT
69 typedef enum __e_fmtcheck_types EFT
;
83 #define RETURN(pf,f,r) do { \
86 } /*NOTREACHED*/ /*CONSTCOND*/ while (0)
89 get_next_format_from_precision(const char **pf
)
91 enum e_modifier modifier
;
98 if (!*f
) RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
103 modifier
= MOD_SHORT
;
108 modifier
= MOD_INTMAXT
;
112 if (!*f
) RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
126 modifier
= MOD_PTRDIFFT
;
130 modifier
= MOD_SIZET
;
134 modifier
= MOD_LONGDOUBLE
;
140 if (!*f
) RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
141 if (strchr("diouxX", *f
)) {
144 RETURN(pf
,f
,FMTCHECK_LONG
);
146 RETURN(pf
,f
,FMTCHECK_QUAD
);
148 RETURN(pf
,f
,FMTCHECK_INTMAXT
);
150 RETURN(pf
,f
,FMTCHECK_PTRDIFFT
);
152 RETURN(pf
,f
,FMTCHECK_SIZET
);
156 RETURN(pf
,f
,FMTCHECK_INT
);
158 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
164 RETURN(pf
,f
,FMTCHECK_CHARPOINTER
);
166 RETURN(pf
,f
,FMTCHECK_SHORTPOINTER
);
168 RETURN(pf
,f
,FMTCHECK_LONGPOINTER
);
170 RETURN(pf
,f
,FMTCHECK_QUADPOINTER
);
172 RETURN(pf
,f
,FMTCHECK_INTMAXTPOINTER
);
174 RETURN(pf
,f
,FMTCHECK_PTRDIFFTPOINTER
);
176 RETURN(pf
,f
,FMTCHECK_SIZETPOINTER
);
178 RETURN(pf
,f
,FMTCHECK_INTPOINTER
);
180 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
183 if (strchr("DOU", *f
)) {
184 if (modifier
!= MOD_NONE
)
185 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
186 RETURN(pf
,f
,FMTCHECK_LONG
);
188 #ifndef NO_FLOATING_POINT
189 if (strchr("aAeEfFgG", *f
)) {
192 RETURN(pf
,f
,FMTCHECK_LONGDOUBLE
);
195 RETURN(pf
,f
,FMTCHECK_DOUBLE
);
197 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
204 RETURN(pf
,f
,FMTCHECK_WINTT
);
206 RETURN(pf
,f
,FMTCHECK_INT
);
208 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
212 if (modifier
!= MOD_NONE
)
213 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
214 RETURN(pf
,f
,FMTCHECK_WINTT
);
219 RETURN(pf
,f
,FMTCHECK_WSTRING
);
221 RETURN(pf
,f
,FMTCHECK_STRING
);
223 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
227 if (modifier
!= MOD_NONE
)
228 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
229 RETURN(pf
,f
,FMTCHECK_WSTRING
);
232 if (modifier
!= MOD_NONE
)
233 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
234 RETURN(pf
,f
,FMTCHECK_LONG
);
236 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
241 get_next_format_from_width(const char **pf
)
249 RETURN(pf
,f
,FMTCHECK_PRECISION
);
251 /* eat any precision (empty is allowed) */
252 while (isdigit(*f
)) f
++;
253 if (!*f
) RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
255 RETURN(pf
,f
,get_next_format_from_precision(pf
));
260 get_next_format(const char **pf
, EFT eft
)
265 if (eft
== FMTCHECK_WIDTH
) {
267 return get_next_format_from_width(pf
);
268 } else if (eft
== FMTCHECK_PRECISION
) {
270 return get_next_format_from_precision(pf
);
278 RETURN(pf
,f
,FMTCHECK_DONE
);
281 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
288 /* Eat any of the flags */
289 while (*f
&& (strchr("#'0- +", *f
)))
293 RETURN(pf
,f
,FMTCHECK_WIDTH
);
296 while (isdigit(*f
)) f
++;
298 RETURN(pf
,f
,FMTCHECK_UNKNOWN
);
301 RETURN(pf
,f
,get_next_format_from_width(pf
));
306 __fmtcheck(const char *f1
, const char *f2
)
308 const char *f1p
, *f2p
;
314 f1t
= FMTCHECK_START
;
316 f2t
= FMTCHECK_START
;
317 while ((f1t
= get_next_format(&f1p
, f1t
)) != FMTCHECK_DONE
) {
318 if (f1t
== FMTCHECK_UNKNOWN
)
320 f2t
= get_next_format(&f2p
, f2t
);