xref: /titanic_51/usr/src/lib/libc/port/stdio/doscan.c (revision d12182d8ece1c86804ccf0f3bceb9bf14d671f71)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf /*	Copyright (c) 1988 AT&T	*/
287257d1b4Sraf /*	  All Rights Reserved  	*/
297257d1b4Sraf 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include "mtlib.h"
357c478bd9Sstevel@tonic-gate #include "file64.h"
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <stdarg.h>
397c478bd9Sstevel@tonic-gate #include <values.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <math.h>
447c478bd9Sstevel@tonic-gate #include <thread.h>
457c478bd9Sstevel@tonic-gate #include <synch.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <fnmatch.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <wchar.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include "libc.h"
527c478bd9Sstevel@tonic-gate #include "stdiom.h"
537c478bd9Sstevel@tonic-gate #include "xpg6.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	NCHARS	(1 << BITSPERBYTE)
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* if the _IOWRT flag is set, this must be a call from sscanf */
587c478bd9Sstevel@tonic-gate #define	locgetc(cnt)	(cnt += 1, (iop->_flag & _IOWRT) ? \
597c478bd9Sstevel@tonic-gate 				((*iop->_ptr == '\0') ? EOF : *iop->_ptr++) : \
607c478bd9Sstevel@tonic-gate 				GETC(iop))
617c478bd9Sstevel@tonic-gate #define	locungetc(cnt, x) (cnt -= 1, (x == EOF) ? EOF : \
627c478bd9Sstevel@tonic-gate 				((iop->_flag & _IOWRT) ? *(--iop->_ptr) : \
637c478bd9Sstevel@tonic-gate 				    (++iop->_cnt, *(--iop->_ptr))))
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	wlocgetc()	((iop->_flag & _IOWRT) ? \
667c478bd9Sstevel@tonic-gate 				((*iop->_ptr == '\0') ? EOF : *iop->_ptr++) : \
677c478bd9Sstevel@tonic-gate 				GETC(iop))
687c478bd9Sstevel@tonic-gate #define	wlocungetc(x) ((x == EOF) ? EOF : \
697c478bd9Sstevel@tonic-gate 				((iop->_flag & _IOWRT) ? *(--iop->_ptr) : \
707c478bd9Sstevel@tonic-gate 				    UNGETC(x, iop)))
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	MAXARGS	30	/* max. number of args for fast positional paramters */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * stva_list is used to subvert C's restriction that a variable with an
767c478bd9Sstevel@tonic-gate  * array type can not appear on the left hand side of an assignment operator.
777c478bd9Sstevel@tonic-gate  * By putting the array inside a structure, the functionality of assigning to
787c478bd9Sstevel@tonic-gate  * the whole array through a simple assignment is achieved..
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate typedef struct stva_list {
817c478bd9Sstevel@tonic-gate 	va_list	ap;
827c478bd9Sstevel@tonic-gate } stva_list;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int number(int *, int *, int, int, int, int, FILE *, va_list *);
857c478bd9Sstevel@tonic-gate static int readchar(FILE *, int *);
867c478bd9Sstevel@tonic-gate static int string(int *, int *, int, int, int, char *, FILE *, va_list *);
877c478bd9Sstevel@tonic-gate static int wstring(int *, int *, int, int, int, FILE *, va_list *);
887c478bd9Sstevel@tonic-gate static int	wbrstring(int *, int *, int, int, int, FILE *,
897c478bd9Sstevel@tonic-gate 	unsigned char *, va_list *);
907c478bd9Sstevel@tonic-gate #ifdef	_WIDE
917c478bd9Sstevel@tonic-gate static int	brstring(int *, int *, int, int, int, FILE *,
927c478bd9Sstevel@tonic-gate 	unsigned char *, va_list *);
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate static int _bi_getwc(FILE *);
957c478bd9Sstevel@tonic-gate static int _bi_ungetwc(wint_t, FILE *);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #ifdef	_WIDE
987c478bd9Sstevel@tonic-gate static int _mkarglst(const wchar_t *, stva_list, stva_list[]);
997c478bd9Sstevel@tonic-gate static wint_t	_wd_getwc(int *, FILE *);
1007c478bd9Sstevel@tonic-gate static wint_t	_wd_ungetwc(int *, wchar_t, FILE *);
1017c478bd9Sstevel@tonic-gate static int	_watoi(wchar_t *);
1027c478bd9Sstevel@tonic-gate #else  /* _WIDE */
1037c478bd9Sstevel@tonic-gate static int _mkarglst(const char *, stva_list, stva_list[]);
1047c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifndef	_WIDE
1077c478bd9Sstevel@tonic-gate int
1087c478bd9Sstevel@tonic-gate _doscan(FILE *iop, const char *fmt, va_list va_Alist)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	int ret;
1117c478bd9Sstevel@tonic-gate 	rmutex_t *lk;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (iop->_flag & _IOWRT)
1147c478bd9Sstevel@tonic-gate 		ret = __doscan_u(iop, fmt, va_Alist, 0);
1157c478bd9Sstevel@tonic-gate 	else {
1167c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop);
1177c478bd9Sstevel@tonic-gate 		ret = __doscan_u(iop, fmt, va_Alist, 0);
1187c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	return (ret);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate #endif  /* _WIDE */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
1257c478bd9Sstevel@tonic-gate #ifdef	_WIDE
1267c478bd9Sstevel@tonic-gate int
1277c478bd9Sstevel@tonic-gate __wdoscan_u(FILE *iop, const wchar_t *fmt, va_list va_Alist, int scflag)
1287c478bd9Sstevel@tonic-gate #else  /* _WIDE */
1297c478bd9Sstevel@tonic-gate int
1307c478bd9Sstevel@tonic-gate __doscan_u(FILE *iop, const char *sfmt, va_list va_Alist, int scflag)
1317c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate #ifdef	_WIDE
1347c478bd9Sstevel@tonic-gate 	wchar_t	ch;
1357c478bd9Sstevel@tonic-gate 	wchar_t	inchar, size;
1367c478bd9Sstevel@tonic-gate 	int	nmatch = 0, len, stow;
1377c478bd9Sstevel@tonic-gate #else  /* _WIDE */
1387c478bd9Sstevel@tonic-gate 	int	ch;
1397c478bd9Sstevel@tonic-gate 	int		nmatch = 0, len, inchar, stow, size;
1407c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	unsigned char	*bracket_str = NULL;
1437c478bd9Sstevel@tonic-gate 	int		chcount, flag_eof;
1447c478bd9Sstevel@tonic-gate 	char	tab[NCHARS];
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/* variables for postional parameters */
1477c478bd9Sstevel@tonic-gate #ifdef	_WIDE
1487c478bd9Sstevel@tonic-gate 	const wchar_t	*sformat = fmt;	/* save the beginning of the format */
1497c478bd9Sstevel@tonic-gate #else  /* _WIDE */
1507c478bd9Sstevel@tonic-gate 	const unsigned char	*fmt = (const unsigned char *)sfmt;
1517c478bd9Sstevel@tonic-gate 	const char	*sformat = sfmt; /* save the beginning of the format */
1527c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1537c478bd9Sstevel@tonic-gate 	int		fpos = 1;	/* 1 if first postional parameter */
154*d12182d8Srobbin 	stva_list	args;	/* used to step through the argument list */
155*d12182d8Srobbin 	stva_list	sargs;	/* used to save start of the argument list */
1567c478bd9Sstevel@tonic-gate 	stva_list	arglst[MAXARGS];
1577c478bd9Sstevel@tonic-gate 					/*
1587c478bd9Sstevel@tonic-gate 					 * array giving the appropriate values
1597c478bd9Sstevel@tonic-gate 					 * for va_arg() to retrieve the
1607c478bd9Sstevel@tonic-gate 					 * corresponding argument:
1617c478bd9Sstevel@tonic-gate 					 * arglst[0] is the first argument
1627c478bd9Sstevel@tonic-gate 					 * arglst[1] is the second argument,etc.
1637c478bd9Sstevel@tonic-gate 					 */
1647c478bd9Sstevel@tonic-gate 	/* Check if readable stream */
1657c478bd9Sstevel@tonic-gate 	if (!(iop->_flag & (_IOREAD | _IORW))) {
1667c478bd9Sstevel@tonic-gate 		errno = EBADF;
1677c478bd9Sstevel@tonic-gate 		return (EOF);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Initialize args and sargs to the start of the argument list.
1727c478bd9Sstevel@tonic-gate 	 * We don't know any portable way to copy an arbitrary C object
1737c478bd9Sstevel@tonic-gate 	 * so we use a system-specific routine(probably a macro) from
1747c478bd9Sstevel@tonic-gate 	 * stdarg.h.  (Remember that if va_list is an array, in_args will
1757c478bd9Sstevel@tonic-gate 	 * be a pointer and &in_args won't be what we would want for
1767c478bd9Sstevel@tonic-gate 	 * memcpy.)
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	va_copy(args.ap, va_Alist);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	sargs = args;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	chcount = 0; flag_eof = 0;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * ****************************************************
1867c478bd9Sstevel@tonic-gate 	 * Main loop: reads format to determine a pattern,
1877c478bd9Sstevel@tonic-gate 	 *		and then goes to read input stream
1887c478bd9Sstevel@tonic-gate 	 *		in attempt to match the pattern.
1897c478bd9Sstevel@tonic-gate 	 * ****************************************************
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	for (; ; ) {
1927c478bd9Sstevel@tonic-gate 		if ((ch = *fmt++) == '\0') {
1937c478bd9Sstevel@tonic-gate 			return (nmatch); /* end of format */
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate #ifdef	_WIDE
1967c478bd9Sstevel@tonic-gate 		if (iswspace(ch)) {
1977c478bd9Sstevel@tonic-gate 			if (!flag_eof) {
1987c478bd9Sstevel@tonic-gate 				while (iswspace(inchar =
1997c478bd9Sstevel@tonic-gate 				    _wd_getwc(&chcount, iop)))
2007c478bd9Sstevel@tonic-gate 					;
2017c478bd9Sstevel@tonic-gate 				if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
2027c478bd9Sstevel@tonic-gate 					flag_eof = 1;
2037c478bd9Sstevel@tonic-gate 			}
2047c478bd9Sstevel@tonic-gate 			continue;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		if (ch != '%' || (ch = *fmt++) == '%') {
207*d12182d8Srobbin 			if (ch == '%') {
208*d12182d8Srobbin 				if (!flag_eof) {
209*d12182d8Srobbin 					while (iswspace(inchar =
210*d12182d8Srobbin 					    _wd_getwc(&chcount, iop)))
211*d12182d8Srobbin 						;
212*d12182d8Srobbin 					if (_wd_ungetwc(&chcount, inchar, iop)
213*d12182d8Srobbin 					    == WEOF)
214*d12182d8Srobbin 						flag_eof = 1;
215*d12182d8Srobbin 				}
216*d12182d8Srobbin 			}
2177c478bd9Sstevel@tonic-gate 			if ((inchar = _wd_getwc(&chcount, iop)) == ch)
2187c478bd9Sstevel@tonic-gate 				continue;
2197c478bd9Sstevel@tonic-gate 			if (_wd_ungetwc(&chcount, inchar, iop) != WEOF) {
2207c478bd9Sstevel@tonic-gate 				return (nmatch); /* failed to match input */
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate #else  /* _WIDE */
2257c478bd9Sstevel@tonic-gate 		if (isspace(ch)) {
2267c478bd9Sstevel@tonic-gate 			if (!flag_eof) {
2277c478bd9Sstevel@tonic-gate 				while (isspace(inchar = locgetc(chcount)))
2287c478bd9Sstevel@tonic-gate 					;
2297c478bd9Sstevel@tonic-gate 				if (locungetc(chcount, inchar) == EOF)
2307c478bd9Sstevel@tonic-gate 					flag_eof = 1;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 			continue;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		if (ch != '%' || (ch = *fmt++) == '%') {
236*d12182d8Srobbin 			if (ch == '%') {
237*d12182d8Srobbin 				if (!flag_eof) {
238*d12182d8Srobbin 					while (isspace(inchar =
239*d12182d8Srobbin 					    locgetc(chcount)))
240*d12182d8Srobbin 						;
241*d12182d8Srobbin 					if (locungetc(chcount, inchar) == EOF)
242*d12182d8Srobbin 						flag_eof = 1;
243*d12182d8Srobbin 				}
244*d12182d8Srobbin 			}
2457c478bd9Sstevel@tonic-gate 			if ((inchar = locgetc(chcount)) == ch)
2467c478bd9Sstevel@tonic-gate 				continue;
2477c478bd9Sstevel@tonic-gate 			if (locungetc(chcount, inchar) != EOF) {
2487c478bd9Sstevel@tonic-gate 				return (nmatch); /* failed to match input */
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 		}
2527c478bd9Sstevel@tonic-gate #endif /* _WIDE */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate charswitch:	/* target of a goto 8-( */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (ch == '*') {
2577c478bd9Sstevel@tonic-gate 			stow = 0;
2587c478bd9Sstevel@tonic-gate 			ch = *fmt++;
2597c478bd9Sstevel@tonic-gate 		} else
2607c478bd9Sstevel@tonic-gate 			stow = 1;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate #ifdef	_WIDE
2637c478bd9Sstevel@tonic-gate 		for (len = 0; ((ch >= 0) && (ch < 256) && isdigit(ch));
2647c478bd9Sstevel@tonic-gate 		    ch = *fmt++)
2657c478bd9Sstevel@tonic-gate 			len = len * 10 + ch - '0';
2667c478bd9Sstevel@tonic-gate #else  /* _WIDE */
2677c478bd9Sstevel@tonic-gate 		for (len = 0; isdigit(ch); ch = *fmt++)
2687c478bd9Sstevel@tonic-gate 			len = len * 10 + ch - '0';
2697c478bd9Sstevel@tonic-gate #endif /* _WIDE */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		if (ch == '$') {
2727c478bd9Sstevel@tonic-gate 			/*
2737c478bd9Sstevel@tonic-gate 			 * positional parameter handling - the number
2747c478bd9Sstevel@tonic-gate 			 * specified in len gives the argument to which
2757c478bd9Sstevel@tonic-gate 			 * the next conversion should be applied.
2767c478bd9Sstevel@tonic-gate 			 * WARNING: This implementation of positional
2777c478bd9Sstevel@tonic-gate 			 * parameters assumes that the sizes of all pointer
2787c478bd9Sstevel@tonic-gate 			 * types are the same. (Code similar to that
2797c478bd9Sstevel@tonic-gate 			 * in the portable doprnt.c should be used if this
2807c478bd9Sstevel@tonic-gate 			 * assumption does not hold for a particular
2817c478bd9Sstevel@tonic-gate 			 * port.)
2827c478bd9Sstevel@tonic-gate 			 */
2837c478bd9Sstevel@tonic-gate 			if (fpos) {
2847c478bd9Sstevel@tonic-gate 				if (_mkarglst(sformat, sargs, arglst) != 0) {
2857c478bd9Sstevel@tonic-gate 					return (EOF);
2867c478bd9Sstevel@tonic-gate 				} else {
2877c478bd9Sstevel@tonic-gate 					fpos = 0;
2887c478bd9Sstevel@tonic-gate 				}
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 			if (len <= MAXARGS) {
2917c478bd9Sstevel@tonic-gate 				args = arglst[len - 1];
2927c478bd9Sstevel@tonic-gate 			} else {
2937c478bd9Sstevel@tonic-gate 				args = arglst[MAXARGS - 1];
2947c478bd9Sstevel@tonic-gate 				for (len -= MAXARGS; len > 0; len--)
2957c478bd9Sstevel@tonic-gate 					(void) va_arg(args.ap, void *);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			len = 0;
2987c478bd9Sstevel@tonic-gate 			ch = *fmt++;
2997c478bd9Sstevel@tonic-gate 			goto charswitch;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		if (len == 0)
3037c478bd9Sstevel@tonic-gate 			len = MAXINT;
3047c478bd9Sstevel@tonic-gate #ifdef	_WIDE
3057c478bd9Sstevel@tonic-gate 		if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
3067c478bd9Sstevel@tonic-gate 		    (size == 'j') || (size == 't') || (size == 'z'))
3077c478bd9Sstevel@tonic-gate 			ch = *fmt++;
3087c478bd9Sstevel@tonic-gate #else  /* _WIDE */
3097c478bd9Sstevel@tonic-gate 		if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
3107c478bd9Sstevel@tonic-gate 		    (size == 'w') || (size == 'j') || (size == 't') ||
3117c478bd9Sstevel@tonic-gate 		    (size == 'z'))
3127c478bd9Sstevel@tonic-gate 			ch = *fmt++;
3137c478bd9Sstevel@tonic-gate #endif /* _WIDE */
3147c478bd9Sstevel@tonic-gate 		if (size == 'l' && ch == 'l') {
3157c478bd9Sstevel@tonic-gate 			size = 'm';		/* size = 'm' if long long */
3167c478bd9Sstevel@tonic-gate 			ch = *fmt++;
3177c478bd9Sstevel@tonic-gate 		} else if (size == 'h' && ch == 'h') {
3187c478bd9Sstevel@tonic-gate 			size = 'b';		/* use size = 'b' if char */
3197c478bd9Sstevel@tonic-gate 			ch = *fmt++;
3207c478bd9Sstevel@tonic-gate 		} else if ((size == 't') || (size == 'z')) {
3217c478bd9Sstevel@tonic-gate 			size = 'l';
3227c478bd9Sstevel@tonic-gate 		} else if (size == 'j') {
3237c478bd9Sstevel@tonic-gate #ifndef _LP64
3247c478bd9Sstevel@tonic-gate 			/* check scflag for size of u/intmax_t (32-bit libc) */
325*d12182d8Srobbin 			if (!(scflag & _F_INTMAX32)) {
3267c478bd9Sstevel@tonic-gate #endif
3277c478bd9Sstevel@tonic-gate 				size = 'm';
3287c478bd9Sstevel@tonic-gate #ifndef _LP64
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate #endif
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		if (ch == '\0') {
3337c478bd9Sstevel@tonic-gate 			return (EOF);		/* unexpected end of format */
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate #ifdef	_WIDE
3367c478bd9Sstevel@tonic-gate 		if (ch == '[') {
3377c478bd9Sstevel@tonic-gate 			wchar_t	c;
3387c478bd9Sstevel@tonic-gate 			size_t	len;
3397c478bd9Sstevel@tonic-gate 			int	negflg = 0;
3407c478bd9Sstevel@tonic-gate 			wchar_t	*p;
3417c478bd9Sstevel@tonic-gate 			wchar_t	*wbracket_str;
3427c478bd9Sstevel@tonic-gate 			size_t	wlen, clen;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			/* p points to the address of '[' */
3457c478bd9Sstevel@tonic-gate 			p = (wchar_t *)fmt - 1;
3467c478bd9Sstevel@tonic-gate 			len = 0;
3477c478bd9Sstevel@tonic-gate 			if (*fmt == '^') {
3487c478bd9Sstevel@tonic-gate 				len++;
3497c478bd9Sstevel@tonic-gate 				fmt++;
3507c478bd9Sstevel@tonic-gate 				negflg = 1;
3517c478bd9Sstevel@tonic-gate 			}
3527c478bd9Sstevel@tonic-gate 			if (((c = *fmt) == ']') || (c == '-')) {
3537c478bd9Sstevel@tonic-gate 				len++;
3547c478bd9Sstevel@tonic-gate 				fmt++;
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 			while ((c = *fmt) != ']') {
3577c478bd9Sstevel@tonic-gate 				if (c == '\0') {
3587c478bd9Sstevel@tonic-gate 					return (EOF); /* unexpected EOF */
3597c478bd9Sstevel@tonic-gate 				} else {
3607c478bd9Sstevel@tonic-gate 					len++;
3617c478bd9Sstevel@tonic-gate 					fmt++;
3627c478bd9Sstevel@tonic-gate 				}
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 			fmt++;
3657c478bd9Sstevel@tonic-gate 			len += 2;
3667c478bd9Sstevel@tonic-gate 			wbracket_str = (wchar_t *)
3677c478bd9Sstevel@tonic-gate 			    malloc(sizeof (wchar_t) * (len + 1));
3687c478bd9Sstevel@tonic-gate 			if (wbracket_str == NULL) {
3697c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
3707c478bd9Sstevel@tonic-gate 				return (EOF);
3717c478bd9Sstevel@tonic-gate 			} else {
3727c478bd9Sstevel@tonic-gate 				(void) wmemcpy(wbracket_str,
3737c478bd9Sstevel@tonic-gate 				    (const wchar_t *)p, len);
3747c478bd9Sstevel@tonic-gate 				*(wbracket_str + len) = L'\0';
3757c478bd9Sstevel@tonic-gate 				if (negflg && *(wbracket_str + 1) == '^') {
3767c478bd9Sstevel@tonic-gate 					*(wbracket_str + 1) = L'!';
3777c478bd9Sstevel@tonic-gate 				}
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 			wlen = wcslen(wbracket_str);
3807c478bd9Sstevel@tonic-gate 			clen = wcstombs((char *)NULL, wbracket_str, 0);
3817c478bd9Sstevel@tonic-gate 			if (clen == (size_t)-1) {
3827c478bd9Sstevel@tonic-gate 				free(wbracket_str);
3837c478bd9Sstevel@tonic-gate 				return (EOF);
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 			bracket_str = (unsigned char *)
3867c478bd9Sstevel@tonic-gate 			    malloc(sizeof (unsigned char) * (clen + 1));
3877c478bd9Sstevel@tonic-gate 			if (bracket_str == NULL) {
3887c478bd9Sstevel@tonic-gate 				free(wbracket_str);
3897c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
3907c478bd9Sstevel@tonic-gate 				return (EOF);
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			clen = wcstombs((char *)bracket_str, wbracket_str,
3937c478bd9Sstevel@tonic-gate 			    wlen + 1);
3947c478bd9Sstevel@tonic-gate 			free(wbracket_str);
3957c478bd9Sstevel@tonic-gate 			if (clen == (size_t)-1) {
3967c478bd9Sstevel@tonic-gate 				free(bracket_str);
3977c478bd9Sstevel@tonic-gate 				return (EOF);
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate #else  /* _WIDE */
4017c478bd9Sstevel@tonic-gate 		if (ch == '[') {
4027c478bd9Sstevel@tonic-gate 			if (size == 'l') {
4037c478bd9Sstevel@tonic-gate 				int	c, len, i;
4047c478bd9Sstevel@tonic-gate 				int	negflg = 0;
4057c478bd9Sstevel@tonic-gate 				unsigned char 	*p;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 				p = (unsigned char *)(fmt - 1);
4087c478bd9Sstevel@tonic-gate 				len = 0;
4097c478bd9Sstevel@tonic-gate 				if (*fmt == '^') {
4107c478bd9Sstevel@tonic-gate 					len++;
4117c478bd9Sstevel@tonic-gate 					fmt++;
4127c478bd9Sstevel@tonic-gate 					negflg = 1;
4137c478bd9Sstevel@tonic-gate 				}
4147c478bd9Sstevel@tonic-gate 				if (((c = *fmt) == ']') || (c == '-')) {
4157c478bd9Sstevel@tonic-gate 					len++;
4167c478bd9Sstevel@tonic-gate 					fmt++;
4177c478bd9Sstevel@tonic-gate 				}
4187c478bd9Sstevel@tonic-gate 				while ((c = *fmt) != ']') {
4197c478bd9Sstevel@tonic-gate 					if (c == '\0') {
4207c478bd9Sstevel@tonic-gate 						return (EOF);
4217c478bd9Sstevel@tonic-gate 					} else if (isascii(c)) {
4227c478bd9Sstevel@tonic-gate 						len++;
4237c478bd9Sstevel@tonic-gate 						fmt++;
4247c478bd9Sstevel@tonic-gate 					} else {
4257c478bd9Sstevel@tonic-gate 						i = mblen((const char *)fmt,
4267c478bd9Sstevel@tonic-gate 						    MB_CUR_MAX);
4277c478bd9Sstevel@tonic-gate 						if (i <= 0) {
4287c478bd9Sstevel@tonic-gate 							return (EOF);
4297c478bd9Sstevel@tonic-gate 						} else {
4307c478bd9Sstevel@tonic-gate 							len += i;
4317c478bd9Sstevel@tonic-gate 							fmt += i;
4327c478bd9Sstevel@tonic-gate 						}
4337c478bd9Sstevel@tonic-gate 					}
4347c478bd9Sstevel@tonic-gate 				}
4357c478bd9Sstevel@tonic-gate 				fmt++;
4367c478bd9Sstevel@tonic-gate 				len += 2;
4377c478bd9Sstevel@tonic-gate 				bracket_str = (unsigned char *)
438*d12182d8Srobbin 				    malloc(sizeof (unsigned char) * (len + 1));
4397c478bd9Sstevel@tonic-gate 				if (bracket_str == NULL) {
4407c478bd9Sstevel@tonic-gate 					errno = ENOMEM;
4417c478bd9Sstevel@tonic-gate 					return (EOF);
4427c478bd9Sstevel@tonic-gate 				} else {
4437c478bd9Sstevel@tonic-gate 					(void) strncpy((char *)bracket_str,
4447c478bd9Sstevel@tonic-gate 					    (const char *)p, len);
4457c478bd9Sstevel@tonic-gate 					*(bracket_str + len) = '\0';
4467c478bd9Sstevel@tonic-gate 					if (negflg &&
4477c478bd9Sstevel@tonic-gate 					    *(bracket_str + 1) == '^') {
4487c478bd9Sstevel@tonic-gate 						*(bracket_str + 1) = '!';
4497c478bd9Sstevel@tonic-gate 					}
4507c478bd9Sstevel@tonic-gate 				}
4517c478bd9Sstevel@tonic-gate 			} else {
4527c478bd9Sstevel@tonic-gate 				int	t = 0;
4537c478bd9Sstevel@tonic-gate 				int	b, c, d;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 				if (*fmt == '^') {
4567c478bd9Sstevel@tonic-gate 					t++;
4577c478bd9Sstevel@tonic-gate 					fmt++;
4587c478bd9Sstevel@tonic-gate 				}
4597c478bd9Sstevel@tonic-gate 				(void) memset(tab, !t, NCHARS);
4607c478bd9Sstevel@tonic-gate 				if ((c = *fmt) == ']' || c == '-') {
4617c478bd9Sstevel@tonic-gate 					tab[c] = t;
4627c478bd9Sstevel@tonic-gate 					fmt++;
4637c478bd9Sstevel@tonic-gate 				}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 				while ((c = *fmt) != ']') {
4667c478bd9Sstevel@tonic-gate 					if (c == '\0') {
4677c478bd9Sstevel@tonic-gate 						return (EOF);
4687c478bd9Sstevel@tonic-gate 					}
4697c478bd9Sstevel@tonic-gate 					b = *(fmt - 1);
4707c478bd9Sstevel@tonic-gate 					d = *(fmt + 1);
4717c478bd9Sstevel@tonic-gate 					if ((c == '-') && (d != ']') &&
4727c478bd9Sstevel@tonic-gate 					    (b < d)) {
4737c478bd9Sstevel@tonic-gate 						(void) memset(&tab[b], t,
4747c478bd9Sstevel@tonic-gate 						    d - b + 1);
4757c478bd9Sstevel@tonic-gate 						fmt += 2;
4767c478bd9Sstevel@tonic-gate 					} else {
4777c478bd9Sstevel@tonic-gate 						tab[c] = t;
4787c478bd9Sstevel@tonic-gate 						fmt++;
4797c478bd9Sstevel@tonic-gate 					}
4807c478bd9Sstevel@tonic-gate 				}
4817c478bd9Sstevel@tonic-gate 				fmt++;
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate #endif /* _WIDE */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate #ifdef	_WIDE
4877c478bd9Sstevel@tonic-gate 		if ((ch >= 0) && (ch < 256) &&
4887c478bd9Sstevel@tonic-gate 		    isupper((int)ch)) { /* no longer documented */
4897c478bd9Sstevel@tonic-gate 			if (_lib_version == c_issue_4) {
4907c478bd9Sstevel@tonic-gate 				if (size != 'm' && size != 'L')
4917c478bd9Sstevel@tonic-gate 					size = 'l';
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			ch = _tolower((int)ch);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 		if (ch != 'n' && !flag_eof) {
4967c478bd9Sstevel@tonic-gate 			if (ch != 'c' && ch != 'C' && ch != '[') {
4977c478bd9Sstevel@tonic-gate 				while (iswspace(inchar =
4987c478bd9Sstevel@tonic-gate 				    _wd_getwc(&chcount, iop)))
4997c478bd9Sstevel@tonic-gate 					;
5007c478bd9Sstevel@tonic-gate 				if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
5017c478bd9Sstevel@tonic-gate 					break;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate #else  /* _WIDE */
5067c478bd9Sstevel@tonic-gate 		if (isupper(ch)) { /* no longer documented */
5077c478bd9Sstevel@tonic-gate 			if (_lib_version == c_issue_4) {
5087c478bd9Sstevel@tonic-gate 				if (size != 'm' && size != 'L')
5097c478bd9Sstevel@tonic-gate 					size = 'l';
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 			ch = _tolower(ch);
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 		if (ch != 'n' && !flag_eof) {
5147c478bd9Sstevel@tonic-gate 			if (ch != 'c' && ch != 'C' && ch != '[') {
5157c478bd9Sstevel@tonic-gate 				while (isspace(inchar = locgetc(chcount)))
5167c478bd9Sstevel@tonic-gate 					;
5177c478bd9Sstevel@tonic-gate 				if (locungetc(chcount, inchar) == EOF)
5187c478bd9Sstevel@tonic-gate 					break;
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		switch (ch) {
5247c478bd9Sstevel@tonic-gate 		case 'C':
5257c478bd9Sstevel@tonic-gate 		case 'S':
5267c478bd9Sstevel@tonic-gate 		case 'c':
5277c478bd9Sstevel@tonic-gate 		case 's':
5287c478bd9Sstevel@tonic-gate #ifdef	_WIDE
5297c478bd9Sstevel@tonic-gate 			if ((size == 'l') || (size == 'C') || (size == 'S'))
5307c478bd9Sstevel@tonic-gate #else  /* _WIDE */
5317c478bd9Sstevel@tonic-gate 			if ((size == 'w') || (size == 'l') || (size == 'C') ||
5327c478bd9Sstevel@tonic-gate 			    (size == 'S'))
5337c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5347c478bd9Sstevel@tonic-gate 			{
5357c478bd9Sstevel@tonic-gate 				size = wstring(&chcount, &flag_eof, stow,
5367c478bd9Sstevel@tonic-gate 				    (int)ch, len, iop, &args.ap);
5377c478bd9Sstevel@tonic-gate 			} else {
5387c478bd9Sstevel@tonic-gate 				size = string(&chcount, &flag_eof, stow,
5397c478bd9Sstevel@tonic-gate 				    (int)ch, len, tab, iop, &args.ap);
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 			break;
5427c478bd9Sstevel@tonic-gate 		case '[':
5437c478bd9Sstevel@tonic-gate 			if (size == 'l') {
5447c478bd9Sstevel@tonic-gate 				size = wbrstring(&chcount, &flag_eof, stow,
545*d12182d8Srobbin 				    (int)ch, len, iop, bracket_str, &args.ap);
5467c478bd9Sstevel@tonic-gate 				free(bracket_str);
5477c478bd9Sstevel@tonic-gate 				bracket_str = NULL;
5487c478bd9Sstevel@tonic-gate 			} else {
5497c478bd9Sstevel@tonic-gate #ifdef	_WIDE
5507c478bd9Sstevel@tonic-gate 				size = brstring(&chcount, &flag_eof, stow,
551*d12182d8Srobbin 				    (int)ch, len, iop, bracket_str, &args.ap);
5527c478bd9Sstevel@tonic-gate 				free(bracket_str);
5537c478bd9Sstevel@tonic-gate 				bracket_str = NULL;
5547c478bd9Sstevel@tonic-gate #else  /* _WIDE */
5557c478bd9Sstevel@tonic-gate 				size = string(&chcount, &flag_eof, stow,
5567c478bd9Sstevel@tonic-gate 				    ch, len, tab, iop, &args.ap);
5577c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 			break;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		case 'n':
5627c478bd9Sstevel@tonic-gate 			if (stow == 0)
5637c478bd9Sstevel@tonic-gate 				continue;
5647c478bd9Sstevel@tonic-gate 			if (size == 'b')	/* char */
5657c478bd9Sstevel@tonic-gate 				*va_arg(args.ap, char *) = (char)chcount;
5667c478bd9Sstevel@tonic-gate 			else if (size == 'h')
5677c478bd9Sstevel@tonic-gate 				*va_arg(args.ap, short *) = (short)chcount;
5687c478bd9Sstevel@tonic-gate 			else if (size == 'l')
5697c478bd9Sstevel@tonic-gate 				*va_arg(args.ap, long *) = (long)chcount;
5707c478bd9Sstevel@tonic-gate 			else if (size == 'm') /* long long */
5717c478bd9Sstevel@tonic-gate 				*va_arg(args.ap, long long *) =
5727c478bd9Sstevel@tonic-gate 				    (long long) chcount;
5737c478bd9Sstevel@tonic-gate 			else
5747c478bd9Sstevel@tonic-gate 				*va_arg(args.ap, int *) = (int)chcount;
5757c478bd9Sstevel@tonic-gate 			continue;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		case 'i':
5787c478bd9Sstevel@tonic-gate 		default:
5797c478bd9Sstevel@tonic-gate 			size = number(&chcount, &flag_eof, stow, (int)ch,
5807c478bd9Sstevel@tonic-gate 			    len, (int)size, iop, &args.ap);
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		if (size)
5847c478bd9Sstevel@tonic-gate 			nmatch += stow;
5857c478bd9Sstevel@tonic-gate 		else {
5867c478bd9Sstevel@tonic-gate 			return ((flag_eof && !nmatch) ? EOF : nmatch);
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		continue;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if (bracket_str)
5917c478bd9Sstevel@tonic-gate 		free(bracket_str);
5927c478bd9Sstevel@tonic-gate 	return (nmatch != 0 ? nmatch : EOF); /* end of input */
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /* ****************************************************************** */
5967c478bd9Sstevel@tonic-gate /* Functions to read the input stream in an attempt to match incoming */
5977c478bd9Sstevel@tonic-gate /* data to the current pattern from the main loop of _doscan(). */
5987c478bd9Sstevel@tonic-gate /* ****************************************************************** */
5997c478bd9Sstevel@tonic-gate static int
6007c478bd9Sstevel@tonic-gate number(int *chcount, int *flag_eof, int stow, int type, int len, int size,
6017c478bd9Sstevel@tonic-gate 	FILE *iop, va_list *listp)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	char	numbuf[64];
6047c478bd9Sstevel@tonic-gate 	char	*np = numbuf;
6057c478bd9Sstevel@tonic-gate 	int	c, base, inchar, lookahead;
6067c478bd9Sstevel@tonic-gate 	int	digitseen = 0, floater = 0, negflg = 0;
6077c478bd9Sstevel@tonic-gate 	int	lc;
6087c478bd9Sstevel@tonic-gate 	long long	lcval = 0LL;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	switch (type) {
6117c478bd9Sstevel@tonic-gate 	case 'e':
6127c478bd9Sstevel@tonic-gate 	case 'f':
6137c478bd9Sstevel@tonic-gate 	case 'g':
6147c478bd9Sstevel@tonic-gate 		/*
6157c478bd9Sstevel@tonic-gate 		 * lc = 0 corresponds to c90 mode: do not recognize
6167c478bd9Sstevel@tonic-gate 		 *	hexadecimal fp strings; attempt to push back
6177c478bd9Sstevel@tonic-gate 		 *	all unused characters read
6187c478bd9Sstevel@tonic-gate 		 *
6197c478bd9Sstevel@tonic-gate 		 * lc = -1 corresponds to c99 mode: recognize hexa-
6207c478bd9Sstevel@tonic-gate 		 *	decimal fp strings; push back at most one
6217c478bd9Sstevel@tonic-gate 		 *	unused character
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 		lc = (__xpg6 & _C99SUSv3_recognize_hexfp)? -1 : 0;
6247c478bd9Sstevel@tonic-gate 		floater = 1;
6257c478bd9Sstevel@tonic-gate 		break;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	case 'a':
6287c478bd9Sstevel@tonic-gate 		lc = -1;
6297c478bd9Sstevel@tonic-gate 		floater = 1;
6307c478bd9Sstevel@tonic-gate 		break;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	case 'd':
6337c478bd9Sstevel@tonic-gate 	case 'u':
6347c478bd9Sstevel@tonic-gate 	case 'i':
6357c478bd9Sstevel@tonic-gate 		base = 10;
6367c478bd9Sstevel@tonic-gate 		break;
6377c478bd9Sstevel@tonic-gate 	case 'o':
6387c478bd9Sstevel@tonic-gate 		base = 8;
6397c478bd9Sstevel@tonic-gate 		break;
6407c478bd9Sstevel@tonic-gate 	case 'p':
6417c478bd9Sstevel@tonic-gate #ifdef	_LP64
6427c478bd9Sstevel@tonic-gate 		size = 'l'; /* pointers are long in LP64 */
6437c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
6447c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
6457c478bd9Sstevel@tonic-gate 	case 'x':
6467c478bd9Sstevel@tonic-gate 		base = 16;
6477c478bd9Sstevel@tonic-gate 		break;
6487c478bd9Sstevel@tonic-gate 	default:
6497c478bd9Sstevel@tonic-gate 		return (0); /* unrecognized conversion character */
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (floater != 0) {
6537c478bd9Sstevel@tonic-gate 		/*
6547c478bd9Sstevel@tonic-gate 		 * Handle floating point with
6557c478bd9Sstevel@tonic-gate 		 * file_to_decimal.
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		decimal_mode		dm;
6587c478bd9Sstevel@tonic-gate 		decimal_record		dr;
6597c478bd9Sstevel@tonic-gate 		fp_exception_field_type	efs;
6607c478bd9Sstevel@tonic-gate 		enum decimal_string_form form;
6617c478bd9Sstevel@tonic-gate 		char			*echar;
6627c478bd9Sstevel@tonic-gate 		int			nread;
6637c478bd9Sstevel@tonic-gate 		char			buffer[1024+1];
6647c478bd9Sstevel@tonic-gate 		char			*nb = buffer;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		if (len > 1024)
6677c478bd9Sstevel@tonic-gate 			len = 1024;
6687c478bd9Sstevel@tonic-gate 		file_to_decimal(&nb, len, lc, &dr, &form, &echar, iop, &nread);
6697c478bd9Sstevel@tonic-gate 		if (lc == -1) {
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * In C99 mode, the entire string read has to be
6727c478bd9Sstevel@tonic-gate 			 * accepted in order to qualify as a match
6737c478bd9Sstevel@tonic-gate 			 */
6747c478bd9Sstevel@tonic-gate 			if (nb != buffer + nread)
6757c478bd9Sstevel@tonic-gate 				form = invalid_form;
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 		if (stow && (form != invalid_form)) {
6787c478bd9Sstevel@tonic-gate #if defined(__sparc)
6797c478bd9Sstevel@tonic-gate 			dm.rd = _QgetRD();
6807c478bd9Sstevel@tonic-gate 			if (size == 'L') {		/* long double */
6817c478bd9Sstevel@tonic-gate 				if ((int)form < 0)
6827c478bd9Sstevel@tonic-gate 					__hex_to_quadruple(&dr, dm.rd,
683*d12182d8Srobbin 					    va_arg(*listp, quadruple *), &efs);
6847c478bd9Sstevel@tonic-gate 				else
6857c478bd9Sstevel@tonic-gate 					decimal_to_quadruple(
6867c478bd9Sstevel@tonic-gate 					    va_arg(*listp, quadruple *),
6877c478bd9Sstevel@tonic-gate 					    &dm, &dr, &efs);
6887c478bd9Sstevel@tonic-gate 			}
6897c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
6907c478bd9Sstevel@tonic-gate 			dm.rd = __xgetRD();
6917c478bd9Sstevel@tonic-gate 			if (size == 'L') {		/* long double */
6927c478bd9Sstevel@tonic-gate 				if ((int)form < 0)
6937c478bd9Sstevel@tonic-gate 					__hex_to_extended(&dr, dm.rd,
694*d12182d8Srobbin 					    va_arg(*listp, extended *), &efs);
6957c478bd9Sstevel@tonic-gate 				else
6967c478bd9Sstevel@tonic-gate 					decimal_to_extended(
6977c478bd9Sstevel@tonic-gate 					    va_arg(*listp, extended *),
6987c478bd9Sstevel@tonic-gate 					    &dm, &dr, &efs);
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate #else
7017c478bd9Sstevel@tonic-gate #error Unknown architecture
7027c478bd9Sstevel@tonic-gate #endif
7037c478bd9Sstevel@tonic-gate 			else if (size == 'l') {		/* double */
7047c478bd9Sstevel@tonic-gate 				if ((int)form < 0)
7057c478bd9Sstevel@tonic-gate 					__hex_to_double(&dr, dm.rd,
7067c478bd9Sstevel@tonic-gate 					    va_arg(*listp, double *), &efs);
7077c478bd9Sstevel@tonic-gate 				else
7087c478bd9Sstevel@tonic-gate 					decimal_to_double(
7097c478bd9Sstevel@tonic-gate 					    va_arg(*listp, double *),
7107c478bd9Sstevel@tonic-gate 					    &dm, &dr, &efs);
7117c478bd9Sstevel@tonic-gate 			} else {			/* float */
7127c478bd9Sstevel@tonic-gate 				if ((int)form < 0)
7137c478bd9Sstevel@tonic-gate 					__hex_to_single(&dr, dm.rd,
7147c478bd9Sstevel@tonic-gate 					    va_arg(*listp, single *), &efs);
7157c478bd9Sstevel@tonic-gate 				else
7167c478bd9Sstevel@tonic-gate 					decimal_to_single((single *)
7177c478bd9Sstevel@tonic-gate 					    va_arg(*listp, single *),
7187c478bd9Sstevel@tonic-gate 					    &dm, &dr, &efs);
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 			if ((efs & (1 << fp_overflow)) != 0) {
7217c478bd9Sstevel@tonic-gate 				errno = ERANGE;
7227c478bd9Sstevel@tonic-gate 			}
7237c478bd9Sstevel@tonic-gate 			if ((efs & (1 << fp_underflow)) != 0) {
7247c478bd9Sstevel@tonic-gate 				errno = ERANGE;
7257c478bd9Sstevel@tonic-gate 			}
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 		(*chcount) += nread;	/* Count characters read. */
7287c478bd9Sstevel@tonic-gate 		c = locgetc((*chcount));
7297c478bd9Sstevel@tonic-gate 		if (locungetc((*chcount), c) == EOF)
7307c478bd9Sstevel@tonic-gate 			*flag_eof = 1;
7317c478bd9Sstevel@tonic-gate 		return ((form == invalid_form) ? 0 : 1);
7327c478bd9Sstevel@tonic-gate 				/* successful match if non-zero */
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	switch (c = locgetc((*chcount))) {
7367c478bd9Sstevel@tonic-gate 	case '-':
7377c478bd9Sstevel@tonic-gate 		negflg++;
7387c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
7397c478bd9Sstevel@tonic-gate 	case '+':
7407c478bd9Sstevel@tonic-gate 		if (--len <= 0)
7417c478bd9Sstevel@tonic-gate 			break;
7427c478bd9Sstevel@tonic-gate 		if ((c = locgetc((*chcount))) != '0')
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
7457c478bd9Sstevel@tonic-gate 	case '0':
7467c478bd9Sstevel@tonic-gate 		/*
7477c478bd9Sstevel@tonic-gate 		 * If %i or %x, the characters 0x or 0X may optionally precede
7487c478bd9Sstevel@tonic-gate 		 * the sequence of letters and digits (base 16).
7497c478bd9Sstevel@tonic-gate 		 */
7507c478bd9Sstevel@tonic-gate 		if ((type != 'i' && type != 'x') || (len <= 1))
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 		if (((inchar = locgetc((*chcount))) == 'x') ||
7537c478bd9Sstevel@tonic-gate 		    (inchar == 'X')) {
7547c478bd9Sstevel@tonic-gate 			lookahead = readchar(iop, chcount);
7557c478bd9Sstevel@tonic-gate 			if (isxdigit(lookahead)) {
7567c478bd9Sstevel@tonic-gate 				base = 16;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 				if (len <= 2) {
7597c478bd9Sstevel@tonic-gate 					(void) locungetc((*chcount), lookahead);
7607c478bd9Sstevel@tonic-gate 					/* Take into account the 'x' */
7617c478bd9Sstevel@tonic-gate 					len -= 1;
7627c478bd9Sstevel@tonic-gate 				} else {
7637c478bd9Sstevel@tonic-gate 					c = lookahead;
7647c478bd9Sstevel@tonic-gate 					/* Take into account '0x' */
7657c478bd9Sstevel@tonic-gate 					len -= 2;
7667c478bd9Sstevel@tonic-gate 				}
7677c478bd9Sstevel@tonic-gate 			} else {
7687c478bd9Sstevel@tonic-gate 				(void) locungetc((*chcount), lookahead);
7697c478bd9Sstevel@tonic-gate 				(void) locungetc((*chcount), inchar);
7707c478bd9Sstevel@tonic-gate 			}
7717c478bd9Sstevel@tonic-gate 		} else {
7727c478bd9Sstevel@tonic-gate 			/* inchar wans't 'x'. */
7737c478bd9Sstevel@tonic-gate 			(void) locungetc((*chcount), inchar); /* Put it back. */
7747c478bd9Sstevel@tonic-gate 			if (type == 'i') /* Only %i accepts an octal. */
7757c478bd9Sstevel@tonic-gate 				base = 8;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 	for (; --len  >= 0; *np++ = (char)c, c = locgetc((*chcount))) {
7797c478bd9Sstevel@tonic-gate 		if (np > numbuf + 62) {
7807c478bd9Sstevel@tonic-gate 			errno = ERANGE;
7817c478bd9Sstevel@tonic-gate 			return (0);
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		if (isdigit(c) || base == 16 && isxdigit(c)) {
7847c478bd9Sstevel@tonic-gate 			int digit = c - (isdigit(c) ? '0' :
7857c478bd9Sstevel@tonic-gate 			    isupper(c) ? 'A' - 10 : 'a' - 10);
7867c478bd9Sstevel@tonic-gate 			if (digit >= base)
7877c478bd9Sstevel@tonic-gate 				break;
7887c478bd9Sstevel@tonic-gate 			if (stow)
7897c478bd9Sstevel@tonic-gate 				lcval = base * lcval + digit;
7907c478bd9Sstevel@tonic-gate 			digitseen++;
7917c478bd9Sstevel@tonic-gate 			continue;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 		break;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (stow && digitseen) {
7977c478bd9Sstevel@tonic-gate 		/* suppress possible overflow on 2's-comp negation */
7987c478bd9Sstevel@tonic-gate 		if (negflg && lcval != (1ULL << 63))
7997c478bd9Sstevel@tonic-gate 			lcval = -lcval;
8007c478bd9Sstevel@tonic-gate 		switch (size) {
8017c478bd9Sstevel@tonic-gate 			case 'm':
8027c478bd9Sstevel@tonic-gate 				*va_arg(*listp, long long *) = lcval;
8037c478bd9Sstevel@tonic-gate 				break;
8047c478bd9Sstevel@tonic-gate 			case 'l':
8057c478bd9Sstevel@tonic-gate 				*va_arg(*listp, long *) = (long)lcval;
8067c478bd9Sstevel@tonic-gate 				break;
8077c478bd9Sstevel@tonic-gate 			case 'h':
8087c478bd9Sstevel@tonic-gate 				*va_arg(*listp, short *) = (short)lcval;
8097c478bd9Sstevel@tonic-gate 				break;
8107c478bd9Sstevel@tonic-gate 			case 'b':
8117c478bd9Sstevel@tonic-gate 				*va_arg(*listp, char *) = (char)lcval;
8127c478bd9Sstevel@tonic-gate 				break;
8137c478bd9Sstevel@tonic-gate 			default:
8147c478bd9Sstevel@tonic-gate 				*va_arg(*listp, int *) = (int)lcval;
8157c478bd9Sstevel@tonic-gate 				break;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	if (locungetc((*chcount), c) == EOF)
8197c478bd9Sstevel@tonic-gate 		*flag_eof = 1;
8207c478bd9Sstevel@tonic-gate 	return (digitseen); /* successful match if non-zero */
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /* Get a character. If not using sscanf and at the buffer's end */
8247c478bd9Sstevel@tonic-gate /* then do a direct read(). Characters read via readchar() */
8257c478bd9Sstevel@tonic-gate /* can be  pushed back on the input stream by locungetc((*chcount),) */
8267c478bd9Sstevel@tonic-gate /* since there is padding allocated at the end of the stream buffer. */
8277c478bd9Sstevel@tonic-gate static int
8287c478bd9Sstevel@tonic-gate readchar(FILE *iop, int *chcount)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	int	inchar;
8317c478bd9Sstevel@tonic-gate 	char	buf[1];
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if ((iop->_flag & _IOWRT) || (iop->_cnt != 0))
8347c478bd9Sstevel@tonic-gate 		inchar = locgetc((*chcount));
8357c478bd9Sstevel@tonic-gate 	else {
8367c478bd9Sstevel@tonic-gate 		if (read(FILENO(iop), buf, 1) != 1)
8377c478bd9Sstevel@tonic-gate 			return (EOF);
8387c478bd9Sstevel@tonic-gate 		inchar = (int)buf[0];
8397c478bd9Sstevel@tonic-gate 		(*chcount) += 1;
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	return (inchar);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate static int
8457c478bd9Sstevel@tonic-gate string(int *chcount, int *flag_eof, int stow, int type, int len, char *tab,
8467c478bd9Sstevel@tonic-gate 	FILE *iop, va_list *listp)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	int	ch;
8497c478bd9Sstevel@tonic-gate 	char	*ptr;
8507c478bd9Sstevel@tonic-gate 	char	*start;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	start = ptr = stow ? va_arg(*listp, char *) : NULL;
8537c478bd9Sstevel@tonic-gate 	if (((type == 'c') || (type == 'C')) && len == MAXINT)
8547c478bd9Sstevel@tonic-gate 		len = 1;
8557c478bd9Sstevel@tonic-gate #ifdef	_WIDE
8567c478bd9Sstevel@tonic-gate 	while ((ch = locgetc((*chcount))) != EOF &&
8577c478bd9Sstevel@tonic-gate 	    !(((type == 's') || (type == 'S')) && isspace(ch))) {
8587c478bd9Sstevel@tonic-gate #else  /* _WIDE */
8597c478bd9Sstevel@tonic-gate 	while ((ch = locgetc((*chcount))) != EOF &&
8607c478bd9Sstevel@tonic-gate 	    !(((type == 's') || (type == 'S')) &&
8617c478bd9Sstevel@tonic-gate 	    isspace(ch) || type == '[' && tab[ch])) {
8627c478bd9Sstevel@tonic-gate #endif /* _WIDE */
8637c478bd9Sstevel@tonic-gate 		if (stow)
8647c478bd9Sstevel@tonic-gate 			*ptr = (char)ch;
8657c478bd9Sstevel@tonic-gate 		ptr++;
8667c478bd9Sstevel@tonic-gate 		if (--len <= 0)
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	if (ch == EOF) {
8707c478bd9Sstevel@tonic-gate 		(*flag_eof) = 1;
8717c478bd9Sstevel@tonic-gate 		(*chcount) -= 1;
8727c478bd9Sstevel@tonic-gate 	} else if (len > 0 && locungetc((*chcount), ch) == EOF)
8737c478bd9Sstevel@tonic-gate 		(*flag_eof) = 1;
8747c478bd9Sstevel@tonic-gate 	if (ptr == start)
8757c478bd9Sstevel@tonic-gate 		return (0);	/* no match */
8767c478bd9Sstevel@tonic-gate 	if (stow && ((type != 'c') && (type != 'C')))
8777c478bd9Sstevel@tonic-gate 		*ptr = '\0';
8787c478bd9Sstevel@tonic-gate 	return (1);	/* successful match */
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /* This function initializes arglst, to contain the appropriate */
8827c478bd9Sstevel@tonic-gate /* va_list values for the first MAXARGS arguments. */
8837c478bd9Sstevel@tonic-gate /* WARNING: this code assumes that the sizes of all pointer types */
8847c478bd9Sstevel@tonic-gate /* are the same. (Code similar to that in the portable doprnt.c */
8857c478bd9Sstevel@tonic-gate /* should be used if this assumption is not true for a */
8867c478bd9Sstevel@tonic-gate /* particular port.) */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate #ifdef	_WIDE
8897c478bd9Sstevel@tonic-gate static int
8907c478bd9Sstevel@tonic-gate _mkarglst(const wchar_t *fmt, stva_list args, stva_list arglst[])
8917c478bd9Sstevel@tonic-gate #else  /* _WIDE */
8927c478bd9Sstevel@tonic-gate static int
8937c478bd9Sstevel@tonic-gate _mkarglst(const char *fmt, stva_list args, stva_list arglst[])
8947c478bd9Sstevel@tonic-gate #endif /* _WIDE */
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate #ifdef	_WIDE
8977c478bd9Sstevel@tonic-gate #define	STRCHR	wcschr
8987c478bd9Sstevel@tonic-gate #define	STRSPN	wcsspn
8997c478bd9Sstevel@tonic-gate #define	ATOI(x)	_watoi((wchar_t *)x)
9007c478bd9Sstevel@tonic-gate #define	SPNSTR1	L"01234567890"
9017c478bd9Sstevel@tonic-gate #define	SPNSTR2	L"# +-.0123456789hL$"
9027c478bd9Sstevel@tonic-gate #else  /* _WIDE */
9037c478bd9Sstevel@tonic-gate #define	STRCHR	strchr
9047c478bd9Sstevel@tonic-gate #define	STRSPN	strspn
9057c478bd9Sstevel@tonic-gate #define	ATOI(x)	atoi(x)
9067c478bd9Sstevel@tonic-gate #define	SPNSTR1	"01234567890"
9077c478bd9Sstevel@tonic-gate #define	SPNSTR2	"# +-.0123456789hL$"
9087c478bd9Sstevel@tonic-gate #endif /* _WIDE */
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	int maxnum, curargno;
9117c478bd9Sstevel@tonic-gate 	size_t n;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	maxnum = -1;
9147c478bd9Sstevel@tonic-gate 	curargno = 0;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	while ((fmt = STRCHR(fmt, '%')) != NULL) {
9177c478bd9Sstevel@tonic-gate 		fmt++;	/* skip % */
9187c478bd9Sstevel@tonic-gate 		if (*fmt == '*' || *fmt == '%')
9197c478bd9Sstevel@tonic-gate 			continue;
9207c478bd9Sstevel@tonic-gate 		if (fmt[n = STRSPN(fmt, SPNSTR1)] == L'$') {
9217c478bd9Sstevel@tonic-gate 			/* convert to zero base */
9227c478bd9Sstevel@tonic-gate 			curargno = ATOI(fmt) - 1;
9237c478bd9Sstevel@tonic-gate 			fmt += n + 1;
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		if (maxnum < curargno)
9277c478bd9Sstevel@tonic-gate 			maxnum = curargno;
9287c478bd9Sstevel@tonic-gate 		curargno++;	/* default to next in list */
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		fmt += STRSPN(fmt, SPNSTR2);
9317c478bd9Sstevel@tonic-gate 		if (*fmt == '[') {
9327c478bd9Sstevel@tonic-gate 			int	i;
9337c478bd9Sstevel@tonic-gate 			fmt++; /* has to be at least on item in scan list */
9347c478bd9Sstevel@tonic-gate 			if (*fmt == ']') {
9357c478bd9Sstevel@tonic-gate 				fmt++;
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 			while (*fmt != ']') {
9387c478bd9Sstevel@tonic-gate 				if (*fmt == L'\0') {
9397c478bd9Sstevel@tonic-gate 					return (-1); /* bad format */
9407c478bd9Sstevel@tonic-gate #ifdef	_WIDE
9417c478bd9Sstevel@tonic-gate 				} else {
9427c478bd9Sstevel@tonic-gate 					fmt++;
9437c478bd9Sstevel@tonic-gate 				}
9447c478bd9Sstevel@tonic-gate #else  /* _WIDE */
9457c478bd9Sstevel@tonic-gate 				} else if (isascii(*fmt)) {
9467c478bd9Sstevel@tonic-gate 					fmt++;
9477c478bd9Sstevel@tonic-gate 				} else {
9487c478bd9Sstevel@tonic-gate 					i = mblen((const char *)
9497c478bd9Sstevel@tonic-gate 					    fmt, MB_CUR_MAX);
9507c478bd9Sstevel@tonic-gate 					if (i <= 0) {
9517c478bd9Sstevel@tonic-gate 						return (-1);
9527c478bd9Sstevel@tonic-gate 					} else {
9537c478bd9Sstevel@tonic-gate 						fmt += i;
9547c478bd9Sstevel@tonic-gate 					}
9557c478bd9Sstevel@tonic-gate 				}
9567c478bd9Sstevel@tonic-gate #endif /* _WIDE */
9577c478bd9Sstevel@tonic-gate 			}
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	if (maxnum > MAXARGS)
9617c478bd9Sstevel@tonic-gate 		maxnum = MAXARGS;
9627c478bd9Sstevel@tonic-gate 	for (n = 0; n <= maxnum; n++) {
9637c478bd9Sstevel@tonic-gate 		arglst[n] = args;
9647c478bd9Sstevel@tonic-gate 		(void) va_arg(args.ap, void *);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	return (0);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * For wide character handling
9727c478bd9Sstevel@tonic-gate  */
973*d12182d8Srobbin 
9747c478bd9Sstevel@tonic-gate #ifdef	_WIDE
9757c478bd9Sstevel@tonic-gate static int
9767c478bd9Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type,
9777c478bd9Sstevel@tonic-gate 	int len, FILE *iop, va_list *listp)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	wint_t	wch;
9807c478bd9Sstevel@tonic-gate 	wchar_t	*ptr;
9817c478bd9Sstevel@tonic-gate 	wchar_t	*wstart;
9827c478bd9Sstevel@tonic-gate 	int	dummy;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if ((type == 'c') && len == MAXINT)
9877c478bd9Sstevel@tonic-gate 		len = 1;
9887c478bd9Sstevel@tonic-gate 	while (((wch = _wd_getwc(chcount, iop)) != WEOF) &&
9897c478bd9Sstevel@tonic-gate 	    !(type == 's' && iswspace(wch))) {
9907c478bd9Sstevel@tonic-gate 		if (stow)
9917c478bd9Sstevel@tonic-gate 			*ptr = wch;
9927c478bd9Sstevel@tonic-gate 		ptr++;
9937c478bd9Sstevel@tonic-gate 		if (--len <= 0)
9947c478bd9Sstevel@tonic-gate 			break;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	if (wch == WEOF) {
9977c478bd9Sstevel@tonic-gate 		*flag_eof = 1;
9987c478bd9Sstevel@tonic-gate 		(*chcount) -= 1;
9997c478bd9Sstevel@tonic-gate 	} else {
10007c478bd9Sstevel@tonic-gate 		if (len > 0 && _wd_ungetwc(chcount, wch, iop) == WEOF)
10017c478bd9Sstevel@tonic-gate 			*flag_eof = 1;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	if (ptr == wstart)
10047c478bd9Sstevel@tonic-gate 		return (0); /* no match */
10057c478bd9Sstevel@tonic-gate 	if (stow && (type != 'c'))
10067c478bd9Sstevel@tonic-gate 		*ptr = '\0';
10077c478bd9Sstevel@tonic-gate 	return (1); /* successful match */
10087c478bd9Sstevel@tonic-gate }
1009*d12182d8Srobbin 
10107c478bd9Sstevel@tonic-gate #else  /* _WIDE */
10117c478bd9Sstevel@tonic-gate static int
10127c478bd9Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type, int len, FILE *iop,
10137c478bd9Sstevel@tonic-gate 	va_list *listp)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	int	wch;
10167c478bd9Sstevel@tonic-gate 	wchar_t	*ptr;
10177c478bd9Sstevel@tonic-gate 	wchar_t	*wstart;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if ((type == 'c') && len == MAXINT)
10227c478bd9Sstevel@tonic-gate 		len = 1;
10237c478bd9Sstevel@tonic-gate 	while (((wch = _bi_getwc(iop)) != EOF) &&
10247c478bd9Sstevel@tonic-gate 	    !(type == 's' && (isascii(wch) ? isspace(wch) : 0))) {
10257c478bd9Sstevel@tonic-gate 		(*chcount) += _scrwidth((wchar_t)wch);
10267c478bd9Sstevel@tonic-gate 		if (stow)
10277c478bd9Sstevel@tonic-gate 			*ptr = wch;
10287c478bd9Sstevel@tonic-gate 		ptr++;
10297c478bd9Sstevel@tonic-gate 		if (--len <= 0)
10307c478bd9Sstevel@tonic-gate 			break;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 	if (wch == EOF) {
10337c478bd9Sstevel@tonic-gate 		(*flag_eof) = 1;
10347c478bd9Sstevel@tonic-gate 		(*chcount) -= 1;
10357c478bd9Sstevel@tonic-gate 	} else {
10367c478bd9Sstevel@tonic-gate 		if (len > 0 && _bi_ungetwc(wch, iop) == EOF)
10377c478bd9Sstevel@tonic-gate 			(*flag_eof) = 1;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	if (ptr == wstart)
10407c478bd9Sstevel@tonic-gate 		return (0); /* no match */
10417c478bd9Sstevel@tonic-gate 	if (stow && (type != 'c'))
10427c478bd9Sstevel@tonic-gate 		*ptr = '\0';
10437c478bd9Sstevel@tonic-gate 	return (1); /* successful match */
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate #endif /* _WIDE */
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate #ifdef	_WIDE
10487c478bd9Sstevel@tonic-gate static wint_t
10497c478bd9Sstevel@tonic-gate _wd_getwc(int *chcount, FILE *iop)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	wint_t	wc;
10527c478bd9Sstevel@tonic-gate 	int	len;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
10557c478bd9Sstevel@tonic-gate 		/* call from fwscanf, wscanf */
10567c478bd9Sstevel@tonic-gate 		wc = __fgetwc_xpg5(iop);
10577c478bd9Sstevel@tonic-gate 		(*chcount)++;
10587c478bd9Sstevel@tonic-gate 		return (wc);
10597c478bd9Sstevel@tonic-gate 	} else {
10607c478bd9Sstevel@tonic-gate 		/* call from swscanf */
10617c478bd9Sstevel@tonic-gate 		if (*iop->_ptr == '\0')
10627c478bd9Sstevel@tonic-gate 			return (WEOF);
10637c478bd9Sstevel@tonic-gate 		len = mbtowc((wchar_t *)&wc, (const char *)iop->_ptr,
10647c478bd9Sstevel@tonic-gate 		    MB_CUR_MAX);
10657c478bd9Sstevel@tonic-gate 		if (len == -1)
10667c478bd9Sstevel@tonic-gate 			return (WEOF);
10677c478bd9Sstevel@tonic-gate 		iop->_ptr += len;
10687c478bd9Sstevel@tonic-gate 		(*chcount)++;
10697c478bd9Sstevel@tonic-gate 		return (wc);
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate static wint_t
10747c478bd9Sstevel@tonic-gate _wd_ungetwc(int *chcount, wchar_t wc, FILE *iop)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	wint_t	ret;
10777c478bd9Sstevel@tonic-gate 	int	len;
10787c478bd9Sstevel@tonic-gate 	char	mbs[MB_LEN_MAX];
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (wc == WEOF)
10817c478bd9Sstevel@tonic-gate 		return (WEOF);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (!(iop->_flag & _IOWRT)) {
10847c478bd9Sstevel@tonic-gate 		/* call from fwscanf, wscanf */
10857c478bd9Sstevel@tonic-gate 		ret = __ungetwc_xpg5((wint_t)wc, iop);
10867c478bd9Sstevel@tonic-gate 		if (ret != (wint_t)wc)
10877c478bd9Sstevel@tonic-gate 			return (WEOF);
10887c478bd9Sstevel@tonic-gate 		(*chcount)--;
10897c478bd9Sstevel@tonic-gate 		return (ret);
10907c478bd9Sstevel@tonic-gate 	} else {
10917c478bd9Sstevel@tonic-gate 		/* call from swscanf */
10927c478bd9Sstevel@tonic-gate 		len = wctomb(mbs, wc);
10937c478bd9Sstevel@tonic-gate 		if (len == -1)
10947c478bd9Sstevel@tonic-gate 			return (WEOF);
10957c478bd9Sstevel@tonic-gate 		iop->_ptr -= len;
10967c478bd9Sstevel@tonic-gate 		(*chcount)--;
10977c478bd9Sstevel@tonic-gate 		return ((wint_t)wc);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate static int
11027c478bd9Sstevel@tonic-gate _watoi(wchar_t *fmt)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	int	n = 0;
11057c478bd9Sstevel@tonic-gate 	wchar_t	ch;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	ch = *fmt;
11087c478bd9Sstevel@tonic-gate 	if ((ch >= 0) && (ch < 256) && isdigit((int)ch)) {
11097c478bd9Sstevel@tonic-gate 		n = ch - '0';
11107c478bd9Sstevel@tonic-gate 		while (((ch = *++fmt) >= 0) && (ch < 256) &&
11117c478bd9Sstevel@tonic-gate 		    isdigit((int)ch)) {
11127c478bd9Sstevel@tonic-gate 			n *= 10;
11137c478bd9Sstevel@tonic-gate 			n += ch - '0';
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	return (n);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
11217c478bd9Sstevel@tonic-gate static int
11227c478bd9Sstevel@tonic-gate wbrstring(int *chcount, int *flag_eof, int stow, int type,
11237c478bd9Sstevel@tonic-gate 	int len, FILE *iop, unsigned char *brstr, va_list *listp)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate 	wint_t	wch;
11267c478bd9Sstevel@tonic-gate 	int	i;
11277c478bd9Sstevel@tonic-gate 	char	str[MB_LEN_MAX + 1]; /* include null termination */
11287c478bd9Sstevel@tonic-gate 	wchar_t	*ptr, *start;
11297c478bd9Sstevel@tonic-gate #ifdef	_WIDE
11307c478bd9Sstevel@tonic-gate 	int	dummy;
11317c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	start = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate #ifdef	_WIDE
11367c478bd9Sstevel@tonic-gate 	while ((wch = _wd_getwc(&dummy, iop)) != WEOF) {
11377c478bd9Sstevel@tonic-gate #else  /* _WIDE */
11387c478bd9Sstevel@tonic-gate 	while ((wch = _bi_getwc(iop)) != WEOF) {
11397c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11407c478bd9Sstevel@tonic-gate 		i = wctomb(str, (wchar_t)wch);
11417c478bd9Sstevel@tonic-gate 		if (i == -1) {
11427c478bd9Sstevel@tonic-gate 			return (0);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 		str[i] = '\0';
11457c478bd9Sstevel@tonic-gate 		if (fnmatch((const char *)brstr, (const char *)str,
11467c478bd9Sstevel@tonic-gate 		    FNM_NOESCAPE)) {
11477c478bd9Sstevel@tonic-gate 			break;
11487c478bd9Sstevel@tonic-gate 		} else {
11497c478bd9Sstevel@tonic-gate 			if (len > 0) {
11507c478bd9Sstevel@tonic-gate #ifdef	_WIDE
11517c478bd9Sstevel@tonic-gate 				(*chcount)++;
11527c478bd9Sstevel@tonic-gate #else  /* _WIDE */
11537c478bd9Sstevel@tonic-gate 				(*chcount) += _scrwidth(wch);
11547c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11557c478bd9Sstevel@tonic-gate 				len--;
11567c478bd9Sstevel@tonic-gate 				if (stow) {
11577c478bd9Sstevel@tonic-gate 					*ptr = wch;
11587c478bd9Sstevel@tonic-gate 				}
11597c478bd9Sstevel@tonic-gate 				ptr++;
11607c478bd9Sstevel@tonic-gate 				if (len <= 0)
11617c478bd9Sstevel@tonic-gate 					break;
11627c478bd9Sstevel@tonic-gate 			} else {
11637c478bd9Sstevel@tonic-gate 				break;
11647c478bd9Sstevel@tonic-gate 			}
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	if (wch == WEOF) {
11687c478bd9Sstevel@tonic-gate 		*flag_eof = 1;
11697c478bd9Sstevel@tonic-gate 	} else {
11707c478bd9Sstevel@tonic-gate #ifdef	_WIDE
11717c478bd9Sstevel@tonic-gate 		if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF)
11727c478bd9Sstevel@tonic-gate #else  /* _WIDE */
11737c478bd9Sstevel@tonic-gate 		if (len > 0 && _bi_ungetwc(wch, iop) == WEOF)
11747c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11757c478bd9Sstevel@tonic-gate 			*flag_eof = 1;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 	if (ptr == start)
11787c478bd9Sstevel@tonic-gate 		return (0);				/* no match */
11797c478bd9Sstevel@tonic-gate 	if (stow)
11807c478bd9Sstevel@tonic-gate 		*ptr = L'\0';
11817c478bd9Sstevel@tonic-gate 	return (1);					/* successful match */
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate #ifdef	_WIDE
11857c478bd9Sstevel@tonic-gate static int
11867c478bd9Sstevel@tonic-gate brstring(int *chcount, int *flag_eof, int stow, int type,
11877c478bd9Sstevel@tonic-gate 	int len, FILE *iop, unsigned char *brstr, va_list *listp)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	wint_t	wch;
11907c478bd9Sstevel@tonic-gate 	int	i;
11917c478bd9Sstevel@tonic-gate 	char	str[MB_LEN_MAX + 1]; /* include null termination */
11927c478bd9Sstevel@tonic-gate 	char	*ptr, *start, *p;
11937c478bd9Sstevel@tonic-gate 	int	dummy;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	start = ptr = stow ? va_arg(*listp, char *) : NULL;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	while ((wch = _wd_getwc(&dummy, iop)) != WEOF) {
11987c478bd9Sstevel@tonic-gate 		p = str;
11997c478bd9Sstevel@tonic-gate 		i = wctomb(str, (wchar_t)wch);
12007c478bd9Sstevel@tonic-gate 		if (i == -1) {
12017c478bd9Sstevel@tonic-gate 			return (0);
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 		str[i] = '\0';
12047c478bd9Sstevel@tonic-gate 		if (fnmatch((const char *)brstr, (const char *)str,
12057c478bd9Sstevel@tonic-gate 		    FNM_NOESCAPE)) {
12067c478bd9Sstevel@tonic-gate 			break;
12077c478bd9Sstevel@tonic-gate 		} else {
12087c478bd9Sstevel@tonic-gate 			if (len >= i) {
12097c478bd9Sstevel@tonic-gate 				(*chcount)++;
12107c478bd9Sstevel@tonic-gate 				len -= i;
12117c478bd9Sstevel@tonic-gate 				if (stow) {
12127c478bd9Sstevel@tonic-gate 					while (i-- > 0) {
12137c478bd9Sstevel@tonic-gate 						*ptr++ = *p++;
12147c478bd9Sstevel@tonic-gate 					}
12157c478bd9Sstevel@tonic-gate 				} else {
12167c478bd9Sstevel@tonic-gate 					while (i-- > 0) {
12177c478bd9Sstevel@tonic-gate 						ptr++;
12187c478bd9Sstevel@tonic-gate 					}
12197c478bd9Sstevel@tonic-gate 				}
12207c478bd9Sstevel@tonic-gate 				if (len <= 0)
12217c478bd9Sstevel@tonic-gate 					break;
12227c478bd9Sstevel@tonic-gate 			} else {
12237c478bd9Sstevel@tonic-gate 				break;
12247c478bd9Sstevel@tonic-gate 			}
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 	if (wch == WEOF) {
12287c478bd9Sstevel@tonic-gate 		*flag_eof = 1;
12297c478bd9Sstevel@tonic-gate 	} else {
12307c478bd9Sstevel@tonic-gate 		if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF)
12317c478bd9Sstevel@tonic-gate 			*flag_eof = 1;
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 	if (ptr == start)
12347c478bd9Sstevel@tonic-gate 		return (0);				/* no match */
12357c478bd9Sstevel@tonic-gate 	if (stow)
12367c478bd9Sstevel@tonic-gate 		*ptr = '\0';
12377c478bd9Sstevel@tonic-gate 	return (1);					/* successful match */
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate #endif /* _WIDE */
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate /*
12427c478bd9Sstevel@tonic-gate  * Locally define getwc and ungetwc
12437c478bd9Sstevel@tonic-gate  */
12447c478bd9Sstevel@tonic-gate static int
12457c478bd9Sstevel@tonic-gate _bi_getwc(FILE *iop)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	int c;
12487c478bd9Sstevel@tonic-gate 	wchar_t intcode;
12497c478bd9Sstevel@tonic-gate 	int i, nbytes, cur_max;
12507c478bd9Sstevel@tonic-gate 	char buff[MB_LEN_MAX];
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if ((c = wlocgetc()) == EOF)
12537c478bd9Sstevel@tonic-gate 		return (WEOF);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	if (isascii(c))	/* ASCII code */
12567c478bd9Sstevel@tonic-gate 		return ((wint_t)c);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	buff[0] = (char)c;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	cur_max = (int)MB_CUR_MAX;
12617c478bd9Sstevel@tonic-gate 	/* MB_CUR_MAX doen't exeed the value of MB_LEN_MAX */
12627c478bd9Sstevel@tonic-gate 	/* So we use MB_CUR_MAX instead of MB_LEN_MAX for */
12637c478bd9Sstevel@tonic-gate 	/* improving the performance. */
12647c478bd9Sstevel@tonic-gate 	for (i = 1; i < cur_max; i++) {
12657c478bd9Sstevel@tonic-gate 		c = wlocgetc();
12667c478bd9Sstevel@tonic-gate 		if (c == '\n') {
12677c478bd9Sstevel@tonic-gate 			(void) wlocungetc(c);
12687c478bd9Sstevel@tonic-gate 			break;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 		if (c == EOF) {
12717c478bd9Sstevel@tonic-gate 			/* this still may be a valid multibyte character */
12727c478bd9Sstevel@tonic-gate 			break;
12737c478bd9Sstevel@tonic-gate 		}
12747c478bd9Sstevel@tonic-gate 		buff[i] = (char)c;
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if ((nbytes = mbtowc(&intcode, buff, i)) == -1) {
12787c478bd9Sstevel@tonic-gate 		/*
12797c478bd9Sstevel@tonic-gate 		 * If mbtowc fails, the input was not a legal character.
12807c478bd9Sstevel@tonic-gate 		 *	ungetc all but one character.
12817c478bd9Sstevel@tonic-gate 		 *
12827c478bd9Sstevel@tonic-gate 		 * Note:  the number of pushback characters that
12837c478bd9Sstevel@tonic-gate 		 *	ungetc() can handle must be >= (MB_LEN_MAX - 1).
12847c478bd9Sstevel@tonic-gate 		 *	In Solaris 2.x, the number of pushback
12857c478bd9Sstevel@tonic-gate 		 *	characters is 4.
12867c478bd9Sstevel@tonic-gate 		 */
12877c478bd9Sstevel@tonic-gate 		while (i-- > 1) {
12887c478bd9Sstevel@tonic-gate 			(void) wlocungetc((signed char)buff[i]);
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 		errno = EILSEQ;
12917c478bd9Sstevel@tonic-gate 		return (WEOF); /* Illegal EUC sequence. */
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	while (i-- > nbytes) {
12957c478bd9Sstevel@tonic-gate 		/*
12967c478bd9Sstevel@tonic-gate 		 * Note:  the number of pushback characters that
12977c478bd9Sstevel@tonic-gate 		 *	ungetc() can handle must be >= (MB_LEN_MAX - 1).
12987c478bd9Sstevel@tonic-gate 		 *	In Solaris 2.x, the number of pushback
12997c478bd9Sstevel@tonic-gate 		 *	characters is 4.
13007c478bd9Sstevel@tonic-gate 		 */
13017c478bd9Sstevel@tonic-gate 		(void) wlocungetc((signed char)buff[i]);
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 	return ((int)intcode);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate static int
13077c478bd9Sstevel@tonic-gate _bi_ungetwc(wint_t wc, FILE *iop)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	char mbs[MB_LEN_MAX];
13107c478bd9Sstevel@tonic-gate 	unsigned char *p;
13117c478bd9Sstevel@tonic-gate 	int n;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0))
13147c478bd9Sstevel@tonic-gate 		return (WEOF);
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	n = wctomb(mbs, (wchar_t)wc);
13177c478bd9Sstevel@tonic-gate 	if (n <= 0)
13187c478bd9Sstevel@tonic-gate 		return (WEOF);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (iop->_ptr <= iop->_base) {
13217c478bd9Sstevel@tonic-gate 		if (iop->_base == NULL) {
13227c478bd9Sstevel@tonic-gate 			return (WEOF);
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 		if ((iop->_ptr == iop->_base) && (iop->_cnt == 0)) {
13257c478bd9Sstevel@tonic-gate 			++iop->_ptr;
13267c478bd9Sstevel@tonic-gate 		} else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) {
13277c478bd9Sstevel@tonic-gate 			return (WEOF);
13287c478bd9Sstevel@tonic-gate 		}
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	p = (unsigned char *)(mbs+n-1); /* p points the last byte */
13327c478bd9Sstevel@tonic-gate 	/* if _IOWRT is set to iop->_flag, it means this is */
13337c478bd9Sstevel@tonic-gate 	/* an invocation from sscanf(), and in that time we */
13347c478bd9Sstevel@tonic-gate 	/* don't touch iop->_cnt.  Otherwise, which means an */
13357c478bd9Sstevel@tonic-gate 	/* invocation from fscanf() or scanf(), we touch iop->_cnt */
13367c478bd9Sstevel@tonic-gate 	if ((iop->_flag & _IOWRT) == 0) {
13377c478bd9Sstevel@tonic-gate 		/* scanf() and fscanf() */
13387c478bd9Sstevel@tonic-gate 		iop->_cnt += n;
13397c478bd9Sstevel@tonic-gate 		while (n--) {
13407c478bd9Sstevel@tonic-gate 			*--iop->_ptr = *(p--);
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 	} else {
13437c478bd9Sstevel@tonic-gate 		/* sscanf() */
13447c478bd9Sstevel@tonic-gate 		iop->_ptr -= n;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	return (wc);
13477c478bd9Sstevel@tonic-gate }
1348