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