xref: /minix3/lib/libc/gen/fmtcheck.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: fmtcheck.c,v 1.9 2014/06/14 08:18:24 apb Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2000 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code was contributed to The NetBSD Foundation by Allen Briggs.
82fe8fb19SBen Gras  *
92fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
102fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
112fe8fb19SBen Gras  * are met:
122fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
142fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
152fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
162fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
192fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
202fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
212fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
222fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
232fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
242fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
252fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
262fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
272fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
282fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #include <sys/cdefs.h>
322fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fmtcheck.c,v 1.9 2014/06/14 08:18:24 apb Exp $");
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras 
362fe8fb19SBen Gras #include "namespace.h"
372fe8fb19SBen Gras 
382fe8fb19SBen Gras #include <stdio.h>
392fe8fb19SBen Gras #include <string.h>
402fe8fb19SBen Gras #include <ctype.h>
412fe8fb19SBen Gras 
422fe8fb19SBen Gras #ifdef __weak_alias
432fe8fb19SBen Gras __weak_alias(fmtcheck,__fmtcheck)
442fe8fb19SBen Gras #endif
452fe8fb19SBen Gras 
462fe8fb19SBen Gras enum __e_fmtcheck_types {
472fe8fb19SBen Gras 	FMTCHECK_START,
482fe8fb19SBen Gras 	FMTCHECK_SHORT,
492fe8fb19SBen Gras 	FMTCHECK_INT,
502fe8fb19SBen Gras 	FMTCHECK_LONG,
512fe8fb19SBen Gras 	FMTCHECK_QUAD,
52*0a6a1f1dSLionel Sambuc 	FMTCHECK_POINTER,
532fe8fb19SBen Gras 	FMTCHECK_SHORTPOINTER,
542fe8fb19SBen Gras 	FMTCHECK_INTPOINTER,
552fe8fb19SBen Gras 	FMTCHECK_LONGPOINTER,
562fe8fb19SBen Gras 	FMTCHECK_QUADPOINTER,
572fe8fb19SBen Gras 	FMTCHECK_DOUBLE,
582fe8fb19SBen Gras 	FMTCHECK_LONGDOUBLE,
592fe8fb19SBen Gras 	FMTCHECK_STRING,
602fe8fb19SBen Gras 	FMTCHECK_WIDTH,
612fe8fb19SBen Gras 	FMTCHECK_PRECISION,
622fe8fb19SBen Gras 	FMTCHECK_DONE,
632fe8fb19SBen Gras 	FMTCHECK_UNKNOWN
642fe8fb19SBen Gras };
652fe8fb19SBen Gras typedef enum __e_fmtcheck_types EFT;
662fe8fb19SBen Gras 
672fe8fb19SBen Gras #define RETURN(pf,f,r) do { \
682fe8fb19SBen Gras 			*(pf) = (f); \
692fe8fb19SBen Gras 			return r; \
702fe8fb19SBen Gras 		       } /*NOTREACHED*/ /*CONSTCOND*/ while (0)
712fe8fb19SBen Gras 
722fe8fb19SBen Gras static EFT
get_next_format_from_precision(const char ** pf)732fe8fb19SBen Gras get_next_format_from_precision(const char **pf)
742fe8fb19SBen Gras {
752fe8fb19SBen Gras 	int		sh, lg, quad, longdouble;
762fe8fb19SBen Gras 	const char	*f;
772fe8fb19SBen Gras 
782fe8fb19SBen Gras 	sh = lg = quad = longdouble = 0;
792fe8fb19SBen Gras 
802fe8fb19SBen Gras 	f = *pf;
812fe8fb19SBen Gras 	switch (*f) {
822fe8fb19SBen Gras 	case 'h':
832fe8fb19SBen Gras 		f++;
842fe8fb19SBen Gras 		sh = 1;
852fe8fb19SBen Gras 		break;
862fe8fb19SBen Gras 	case 'l':
872fe8fb19SBen Gras 		f++;
882fe8fb19SBen Gras 		if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
892fe8fb19SBen Gras 		if (*f == 'l') {
902fe8fb19SBen Gras 			f++;
912fe8fb19SBen Gras 			quad = 1;
922fe8fb19SBen Gras 		} else {
932fe8fb19SBen Gras 			lg = 1;
942fe8fb19SBen Gras 		}
952fe8fb19SBen Gras 		break;
962fe8fb19SBen Gras 	case 'q':
972fe8fb19SBen Gras 		f++;
982fe8fb19SBen Gras 		quad = 1;
992fe8fb19SBen Gras 		break;
1002fe8fb19SBen Gras 	case 'L':
1012fe8fb19SBen Gras 		f++;
1022fe8fb19SBen Gras 		longdouble = 1;
1032fe8fb19SBen Gras 		break;
1042fe8fb19SBen Gras 	default:
1052fe8fb19SBen Gras 		break;
1062fe8fb19SBen Gras 	}
1072fe8fb19SBen Gras 	if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
1082fe8fb19SBen Gras 	if (strchr("diouxX", *f)) {
1092fe8fb19SBen Gras 		if (longdouble)
1102fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1112fe8fb19SBen Gras 		if (lg)
1122fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_LONG);
1132fe8fb19SBen Gras 		if (quad)
1142fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_QUAD);
1152fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_INT);
1162fe8fb19SBen Gras 	}
1172fe8fb19SBen Gras 	if (*f == 'n') {
1182fe8fb19SBen Gras 		if (longdouble)
1192fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1202fe8fb19SBen Gras 		if (sh)
1212fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_SHORTPOINTER);
1222fe8fb19SBen Gras 		if (lg)
1232fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_LONGPOINTER);
1242fe8fb19SBen Gras 		if (quad)
1252fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_QUADPOINTER);
1262fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_INTPOINTER);
1272fe8fb19SBen Gras 	}
1282fe8fb19SBen Gras 	if (strchr("DOU", *f)) {
1292fe8fb19SBen Gras 		if (sh + lg + quad + longdouble)
1302fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1312fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_LONG);
1322fe8fb19SBen Gras 	}
1332fe8fb19SBen Gras 	if (strchr("eEfg", *f)) {
1342fe8fb19SBen Gras 		if (longdouble)
1352fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_LONGDOUBLE);
1362fe8fb19SBen Gras 		if (sh + lg + quad)
1372fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1382fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_DOUBLE);
1392fe8fb19SBen Gras 	}
1402fe8fb19SBen Gras 	if (*f == 'c') {
1412fe8fb19SBen Gras 		if (sh + lg + quad + longdouble)
1422fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1432fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_INT);
1442fe8fb19SBen Gras 	}
1452fe8fb19SBen Gras 	if (*f == 's') {
1462fe8fb19SBen Gras 		if (sh + lg + quad + longdouble)
1472fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
1482fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_STRING);
1492fe8fb19SBen Gras 	}
1502fe8fb19SBen Gras 	if (*f == 'p') {
1512fe8fb19SBen Gras 		if (sh + lg + quad + longdouble)
1522fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
153*0a6a1f1dSLionel Sambuc 		RETURN(pf,f,FMTCHECK_POINTER);
1542fe8fb19SBen Gras 	}
1552fe8fb19SBen Gras 	RETURN(pf,f,FMTCHECK_UNKNOWN);
1562fe8fb19SBen Gras 	/*NOTREACHED*/
1572fe8fb19SBen Gras }
1582fe8fb19SBen Gras 
1592fe8fb19SBen Gras static EFT
get_next_format_from_width(const char ** pf)1602fe8fb19SBen Gras get_next_format_from_width(const char **pf)
1612fe8fb19SBen Gras {
1622fe8fb19SBen Gras 	const char	*f;
1632fe8fb19SBen Gras 
1642fe8fb19SBen Gras 	f = *pf;
1652fe8fb19SBen Gras 	if (*f == '.') {
1662fe8fb19SBen Gras 		f++;
1672fe8fb19SBen Gras 		if (*f == '*') {
1682fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_PRECISION);
1692fe8fb19SBen Gras 		}
1702fe8fb19SBen Gras 		/* eat any precision (empty is allowed) */
1712fe8fb19SBen Gras 		while (isdigit((unsigned char)*f)) f++;
1722fe8fb19SBen Gras 		if (!*f) RETURN(pf,f,FMTCHECK_UNKNOWN);
1732fe8fb19SBen Gras 	}
1742fe8fb19SBen Gras 	RETURN(pf,f,get_next_format_from_precision(pf));
1752fe8fb19SBen Gras 	/*NOTREACHED*/
1762fe8fb19SBen Gras }
1772fe8fb19SBen Gras 
1782fe8fb19SBen Gras static EFT
get_next_format(const char ** pf,EFT eft)1792fe8fb19SBen Gras get_next_format(const char **pf, EFT eft)
1802fe8fb19SBen Gras {
1812fe8fb19SBen Gras 	int		infmt;
1822fe8fb19SBen Gras 	const char	*f;
1832fe8fb19SBen Gras 
1842fe8fb19SBen Gras 	if (eft == FMTCHECK_WIDTH) {
1852fe8fb19SBen Gras 		(*pf)++;
1862fe8fb19SBen Gras 		return get_next_format_from_width(pf);
1872fe8fb19SBen Gras 	} else if (eft == FMTCHECK_PRECISION) {
1882fe8fb19SBen Gras 		(*pf)++;
1892fe8fb19SBen Gras 		return get_next_format_from_precision(pf);
1902fe8fb19SBen Gras 	}
1912fe8fb19SBen Gras 
1922fe8fb19SBen Gras 	f = *pf;
1932fe8fb19SBen Gras 	infmt = 0;
1942fe8fb19SBen Gras 	while (!infmt) {
1952fe8fb19SBen Gras 		f = strchr(f, '%');
1962fe8fb19SBen Gras 		if (f == NULL)
1972fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_DONE);
1982fe8fb19SBen Gras 		f++;
1992fe8fb19SBen Gras 		if (!*f)
2002fe8fb19SBen Gras 			RETURN(pf,f,FMTCHECK_UNKNOWN);
2012fe8fb19SBen Gras 		if (*f != '%')
2022fe8fb19SBen Gras 			infmt = 1;
2032fe8fb19SBen Gras 		else
2042fe8fb19SBen Gras 			f++;
2052fe8fb19SBen Gras 	}
2062fe8fb19SBen Gras 
2072fe8fb19SBen Gras 	/* Eat any of the flags */
2082fe8fb19SBen Gras 	while (*f && (strchr("#0- +", *f)))
2092fe8fb19SBen Gras 		f++;
2102fe8fb19SBen Gras 
2112fe8fb19SBen Gras 	if (*f == '*') {
2122fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_WIDTH);
2132fe8fb19SBen Gras 	}
2142fe8fb19SBen Gras 	/* eat any width */
2152fe8fb19SBen Gras 	while (isdigit((unsigned char)*f)) f++;
2162fe8fb19SBen Gras 	if (!*f) {
2172fe8fb19SBen Gras 		RETURN(pf,f,FMTCHECK_UNKNOWN);
2182fe8fb19SBen Gras 	}
2192fe8fb19SBen Gras 
2202fe8fb19SBen Gras 	RETURN(pf,f,get_next_format_from_width(pf));
2212fe8fb19SBen Gras 	/*NOTREACHED*/
2222fe8fb19SBen Gras }
2232fe8fb19SBen Gras 
2242fe8fb19SBen Gras const char *
fmtcheck(const char * f1,const char * f2)2252fe8fb19SBen Gras fmtcheck(const char *f1, const char *f2)
2262fe8fb19SBen Gras {
2272fe8fb19SBen Gras 	const char	*f1p, *f2p;
2282fe8fb19SBen Gras 	EFT		f1t, f2t;
2292fe8fb19SBen Gras 
2302fe8fb19SBen Gras 	if (!f1) return f2;
2312fe8fb19SBen Gras 
2322fe8fb19SBen Gras 	f1p = f1;
2332fe8fb19SBen Gras 	f1t = FMTCHECK_START;
2342fe8fb19SBen Gras 	f2p = f2;
2352fe8fb19SBen Gras 	f2t = FMTCHECK_START;
2362fe8fb19SBen Gras 	while ((f1t = get_next_format(&f1p, f1t)) != FMTCHECK_DONE) {
2372fe8fb19SBen Gras 		if (f1t == FMTCHECK_UNKNOWN)
2382fe8fb19SBen Gras 			return f2;
2392fe8fb19SBen Gras 		f2t = get_next_format(&f2p, f2t);
2402fe8fb19SBen Gras 		if (f1t != f2t)
2412fe8fb19SBen Gras 			return f2;
2422fe8fb19SBen Gras 	}
2432fe8fb19SBen Gras 	return f1;
2442fe8fb19SBen Gras }
245