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 __unused)125*4a38094cSToomas Soome __wdoscan_u(FILE *iop, const wchar_t *fmt, va_list va_Alist,
126*4a38094cSToomas Soome int scflag __unused)
1277c478bd9Sstevel@tonic-gate #else /* _WIDE */
1287c478bd9Sstevel@tonic-gate int
129*4a38094cSToomas Soome __doscan_u(FILE *iop, const char *sfmt, va_list va_Alist, int scflag __unused)
1307c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate #ifdef _WIDE
1337c478bd9Sstevel@tonic-gate wchar_t ch;
1347c478bd9Sstevel@tonic-gate wchar_t inchar, size;
1357c478bd9Sstevel@tonic-gate int nmatch = 0, len, stow;
1367c478bd9Sstevel@tonic-gate #else /* _WIDE */
1377c478bd9Sstevel@tonic-gate int ch;
1387c478bd9Sstevel@tonic-gate int nmatch = 0, len, inchar, stow, size;
1397c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate unsigned char *bracket_str = NULL;
1427c478bd9Sstevel@tonic-gate int chcount, flag_eof;
1437c478bd9Sstevel@tonic-gate char tab[NCHARS];
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* variables for postional parameters */
1467c478bd9Sstevel@tonic-gate #ifdef _WIDE
1477c478bd9Sstevel@tonic-gate const wchar_t *sformat = fmt; /* save the beginning of the format */
1487c478bd9Sstevel@tonic-gate #else /* _WIDE */
1497c478bd9Sstevel@tonic-gate const unsigned char *fmt = (const unsigned char *)sfmt;
1507c478bd9Sstevel@tonic-gate const char *sformat = sfmt; /* save the beginning of the format */
1517c478bd9Sstevel@tonic-gate #endif /* _WIDE */
1527c478bd9Sstevel@tonic-gate int fpos = 1; /* 1 if first postional parameter */
153d12182d8Srobbin stva_list args; /* used to step through the argument list */
154d12182d8Srobbin stva_list sargs; /* used to save start of the argument list */
1557c478bd9Sstevel@tonic-gate stva_list arglst[MAXARGS];
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * array giving the appropriate values
1587c478bd9Sstevel@tonic-gate * for va_arg() to retrieve the
1597c478bd9Sstevel@tonic-gate * corresponding argument:
1607c478bd9Sstevel@tonic-gate * arglst[0] is the first argument
1617c478bd9Sstevel@tonic-gate * arglst[1] is the second argument,etc.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate /* Check if readable stream */
1647c478bd9Sstevel@tonic-gate if (!(iop->_flag & (_IOREAD | _IORW))) {
1657c478bd9Sstevel@tonic-gate errno = EBADF;
1667c478bd9Sstevel@tonic-gate return (EOF);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Initialize args and sargs to the start of the argument list.
1717c478bd9Sstevel@tonic-gate * We don't know any portable way to copy an arbitrary C object
1727c478bd9Sstevel@tonic-gate * so we use a system-specific routine(probably a macro) from
1737c478bd9Sstevel@tonic-gate * stdarg.h. (Remember that if va_list is an array, in_args will
1747c478bd9Sstevel@tonic-gate * be a pointer and &in_args won't be what we would want for
1757c478bd9Sstevel@tonic-gate * memcpy.)
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate va_copy(args.ap, va_Alist);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate sargs = args;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate chcount = 0; flag_eof = 0;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate * ****************************************************
1857c478bd9Sstevel@tonic-gate * Main loop: reads format to determine a pattern,
1867c478bd9Sstevel@tonic-gate * and then goes to read input stream
1877c478bd9Sstevel@tonic-gate * in attempt to match the pattern.
1887c478bd9Sstevel@tonic-gate * ****************************************************
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate for (; ; ) {
1917c478bd9Sstevel@tonic-gate if ((ch = *fmt++) == '\0') {
1927c478bd9Sstevel@tonic-gate return (nmatch); /* end of format */
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate #ifdef _WIDE
1957c478bd9Sstevel@tonic-gate if (iswspace(ch)) {
1967c478bd9Sstevel@tonic-gate if (!flag_eof) {
1977c478bd9Sstevel@tonic-gate while (iswspace(inchar =
1987c478bd9Sstevel@tonic-gate _wd_getwc(&chcount, iop)))
1997c478bd9Sstevel@tonic-gate ;
2007c478bd9Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
2017c478bd9Sstevel@tonic-gate flag_eof = 1;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate continue;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate if (ch != '%' || (ch = *fmt++) == '%') {
206d12182d8Srobbin if (ch == '%') {
207d12182d8Srobbin if (!flag_eof) {
208d12182d8Srobbin while (iswspace(inchar =
209d12182d8Srobbin _wd_getwc(&chcount, iop)))
210d12182d8Srobbin ;
211d12182d8Srobbin if (_wd_ungetwc(&chcount, inchar, iop)
212d12182d8Srobbin == WEOF)
213d12182d8Srobbin flag_eof = 1;
214d12182d8Srobbin }
215d12182d8Srobbin }
2167c478bd9Sstevel@tonic-gate if ((inchar = _wd_getwc(&chcount, iop)) == ch)
2177c478bd9Sstevel@tonic-gate continue;
2187c478bd9Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) != WEOF) {
2197c478bd9Sstevel@tonic-gate return (nmatch); /* failed to match input */
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate break;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate #else /* _WIDE */
2247c478bd9Sstevel@tonic-gate if (isspace(ch)) {
2257c478bd9Sstevel@tonic-gate if (!flag_eof) {
2267c478bd9Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount)))
2277c478bd9Sstevel@tonic-gate ;
2287c478bd9Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF)
2297c478bd9Sstevel@tonic-gate flag_eof = 1;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate continue;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate if (ch != '%' || (ch = *fmt++) == '%') {
235d12182d8Srobbin if (ch == '%') {
236d12182d8Srobbin if (!flag_eof) {
237d12182d8Srobbin while (isspace(inchar =
238d12182d8Srobbin locgetc(chcount)))
239d12182d8Srobbin ;
240d12182d8Srobbin if (locungetc(chcount, inchar) == EOF)
241d12182d8Srobbin flag_eof = 1;
242d12182d8Srobbin }
243d12182d8Srobbin }
2447c478bd9Sstevel@tonic-gate if ((inchar = locgetc(chcount)) == ch)
2457c478bd9Sstevel@tonic-gate continue;
2467c478bd9Sstevel@tonic-gate if (locungetc(chcount, inchar) != EOF) {
2477c478bd9Sstevel@tonic-gate return (nmatch); /* failed to match input */
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate break;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate #endif /* _WIDE */
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate charswitch: /* target of a goto 8-( */
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if (ch == '*') {
2567c478bd9Sstevel@tonic-gate stow = 0;
2577c478bd9Sstevel@tonic-gate ch = *fmt++;
2587c478bd9Sstevel@tonic-gate } else
2597c478bd9Sstevel@tonic-gate stow = 1;
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate #ifdef _WIDE
2627c478bd9Sstevel@tonic-gate for (len = 0; ((ch >= 0) && (ch < 256) && isdigit(ch));
2637c478bd9Sstevel@tonic-gate ch = *fmt++)
2647c478bd9Sstevel@tonic-gate len = len * 10 + ch - '0';
2657c478bd9Sstevel@tonic-gate #else /* _WIDE */
2667c478bd9Sstevel@tonic-gate for (len = 0; isdigit(ch); ch = *fmt++)
2677c478bd9Sstevel@tonic-gate len = len * 10 + ch - '0';
2687c478bd9Sstevel@tonic-gate #endif /* _WIDE */
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (ch == '$') {
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate * positional parameter handling - the number
2737c478bd9Sstevel@tonic-gate * specified in len gives the argument to which
2747c478bd9Sstevel@tonic-gate * the next conversion should be applied.
2757c478bd9Sstevel@tonic-gate * WARNING: This implementation of positional
2767c478bd9Sstevel@tonic-gate * parameters assumes that the sizes of all pointer
2777c478bd9Sstevel@tonic-gate * types are the same. (Code similar to that
2787c478bd9Sstevel@tonic-gate * in the portable doprnt.c should be used if this
2797c478bd9Sstevel@tonic-gate * assumption does not hold for a particular
2807c478bd9Sstevel@tonic-gate * port.)
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate if (fpos) {
2837c478bd9Sstevel@tonic-gate if (_mkarglst(sformat, sargs, arglst) != 0) {
2847c478bd9Sstevel@tonic-gate return (EOF);
2857c478bd9Sstevel@tonic-gate } else {
2867c478bd9Sstevel@tonic-gate fpos = 0;
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate if (len <= MAXARGS) {
2907c478bd9Sstevel@tonic-gate args = arglst[len - 1];
2917c478bd9Sstevel@tonic-gate } else {
2927c478bd9Sstevel@tonic-gate args = arglst[MAXARGS - 1];
2937c478bd9Sstevel@tonic-gate for (len -= MAXARGS; len > 0; len--)
2947c478bd9Sstevel@tonic-gate (void) va_arg(args.ap, void *);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate len = 0;
2977c478bd9Sstevel@tonic-gate ch = *fmt++;
2987c478bd9Sstevel@tonic-gate goto charswitch;
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (len == 0)
3027c478bd9Sstevel@tonic-gate len = MAXINT;
3037c478bd9Sstevel@tonic-gate #ifdef _WIDE
3047c478bd9Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
3057c478bd9Sstevel@tonic-gate (size == 'j') || (size == 't') || (size == 'z'))
3067c478bd9Sstevel@tonic-gate ch = *fmt++;
3077c478bd9Sstevel@tonic-gate #else /* _WIDE */
3087c478bd9Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
3097c478bd9Sstevel@tonic-gate (size == 'w') || (size == 'j') || (size == 't') ||
3107c478bd9Sstevel@tonic-gate (size == 'z'))
3117c478bd9Sstevel@tonic-gate ch = *fmt++;
3127c478bd9Sstevel@tonic-gate #endif /* _WIDE */
3137c478bd9Sstevel@tonic-gate if (size == 'l' && ch == 'l') {
3147c478bd9Sstevel@tonic-gate size = 'm'; /* size = 'm' if long long */
3157c478bd9Sstevel@tonic-gate ch = *fmt++;
3167c478bd9Sstevel@tonic-gate } else if (size == 'h' && ch == 'h') {
3177c478bd9Sstevel@tonic-gate size = 'b'; /* use size = 'b' if char */
3187c478bd9Sstevel@tonic-gate ch = *fmt++;
3197c478bd9Sstevel@tonic-gate } else if ((size == 't') || (size == 'z')) {
3207c478bd9Sstevel@tonic-gate size = 'l';
3217c478bd9Sstevel@tonic-gate } else if (size == 'j') {
3227c478bd9Sstevel@tonic-gate #ifndef _LP64
3237c478bd9Sstevel@tonic-gate /* check scflag for size of u/intmax_t (32-bit libc) */
324d12182d8Srobbin if (!(scflag & _F_INTMAX32)) {
3257c478bd9Sstevel@tonic-gate #endif
3267c478bd9Sstevel@tonic-gate size = 'm';
3277c478bd9Sstevel@tonic-gate #ifndef _LP64
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate if (ch == '\0') {
3327c478bd9Sstevel@tonic-gate return (EOF); /* unexpected end of format */
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate #ifdef _WIDE
3357c478bd9Sstevel@tonic-gate if (ch == '[') {
3367c478bd9Sstevel@tonic-gate wchar_t c;
3377c478bd9Sstevel@tonic-gate size_t len;
3387c478bd9Sstevel@tonic-gate int negflg = 0;
3397c478bd9Sstevel@tonic-gate wchar_t *p;
3407c478bd9Sstevel@tonic-gate wchar_t *wbracket_str;
3417c478bd9Sstevel@tonic-gate size_t wlen, clen;
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate /* p points to the address of '[' */
3447c478bd9Sstevel@tonic-gate p = (wchar_t *)fmt - 1;
3457c478bd9Sstevel@tonic-gate len = 0;
3467c478bd9Sstevel@tonic-gate if (*fmt == '^') {
3477c478bd9Sstevel@tonic-gate len++;
3487c478bd9Sstevel@tonic-gate fmt++;
3497c478bd9Sstevel@tonic-gate negflg = 1;
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) {
3527c478bd9Sstevel@tonic-gate len++;
3537c478bd9Sstevel@tonic-gate fmt++;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate while ((c = *fmt) != ']') {
3567c478bd9Sstevel@tonic-gate if (c == '\0') {
3577c478bd9Sstevel@tonic-gate return (EOF); /* unexpected EOF */
3587c478bd9Sstevel@tonic-gate } else {
3597c478bd9Sstevel@tonic-gate len++;
3607c478bd9Sstevel@tonic-gate fmt++;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate fmt++;
3647c478bd9Sstevel@tonic-gate len += 2;
3657c478bd9Sstevel@tonic-gate wbracket_str = (wchar_t *)
3667c478bd9Sstevel@tonic-gate malloc(sizeof (wchar_t) * (len + 1));
3677c478bd9Sstevel@tonic-gate if (wbracket_str == NULL) {
3687c478bd9Sstevel@tonic-gate errno = ENOMEM;
3697c478bd9Sstevel@tonic-gate return (EOF);
3707c478bd9Sstevel@tonic-gate } else {
3717c478bd9Sstevel@tonic-gate (void) wmemcpy(wbracket_str,
3727c478bd9Sstevel@tonic-gate (const wchar_t *)p, len);
3737c478bd9Sstevel@tonic-gate *(wbracket_str + len) = L'\0';
3747c478bd9Sstevel@tonic-gate if (negflg && *(wbracket_str + 1) == '^') {
3757c478bd9Sstevel@tonic-gate *(wbracket_str + 1) = L'!';
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate wlen = wcslen(wbracket_str);
3797c478bd9Sstevel@tonic-gate clen = wcstombs((char *)NULL, wbracket_str, 0);
3807c478bd9Sstevel@tonic-gate if (clen == (size_t)-1) {
3817c478bd9Sstevel@tonic-gate free(wbracket_str);
3827c478bd9Sstevel@tonic-gate return (EOF);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate bracket_str = (unsigned char *)
3857c478bd9Sstevel@tonic-gate malloc(sizeof (unsigned char) * (clen + 1));
3867c478bd9Sstevel@tonic-gate if (bracket_str == NULL) {
3877c478bd9Sstevel@tonic-gate free(wbracket_str);
3887c478bd9Sstevel@tonic-gate errno = ENOMEM;
3897c478bd9Sstevel@tonic-gate return (EOF);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate clen = wcstombs((char *)bracket_str, wbracket_str,
3927c478bd9Sstevel@tonic-gate wlen + 1);
3937c478bd9Sstevel@tonic-gate free(wbracket_str);
3947c478bd9Sstevel@tonic-gate if (clen == (size_t)-1) {
3957c478bd9Sstevel@tonic-gate free(bracket_str);
3967c478bd9Sstevel@tonic-gate return (EOF);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate #else /* _WIDE */
4007c478bd9Sstevel@tonic-gate if (ch == '[') {
4017c478bd9Sstevel@tonic-gate if (size == 'l') {
4027c478bd9Sstevel@tonic-gate int c, len, i;
4037c478bd9Sstevel@tonic-gate int negflg = 0;
4047c478bd9Sstevel@tonic-gate unsigned char *p;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate p = (unsigned char *)(fmt - 1);
4077c478bd9Sstevel@tonic-gate len = 0;
4087c478bd9Sstevel@tonic-gate if (*fmt == '^') {
4097c478bd9Sstevel@tonic-gate len++;
4107c478bd9Sstevel@tonic-gate fmt++;
4117c478bd9Sstevel@tonic-gate negflg = 1;
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) {
4147c478bd9Sstevel@tonic-gate len++;
4157c478bd9Sstevel@tonic-gate fmt++;
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate while ((c = *fmt) != ']') {
4187c478bd9Sstevel@tonic-gate if (c == '\0') {
4197c478bd9Sstevel@tonic-gate return (EOF);
4207c478bd9Sstevel@tonic-gate } else if (isascii(c)) {
4217c478bd9Sstevel@tonic-gate len++;
4227c478bd9Sstevel@tonic-gate fmt++;
4237c478bd9Sstevel@tonic-gate } else {
4247c478bd9Sstevel@tonic-gate i = mblen((const char *)fmt,
4257c478bd9Sstevel@tonic-gate MB_CUR_MAX);
4267c478bd9Sstevel@tonic-gate if (i <= 0) {
4277c478bd9Sstevel@tonic-gate return (EOF);
4287c478bd9Sstevel@tonic-gate } else {
4297c478bd9Sstevel@tonic-gate len += i;
4307c478bd9Sstevel@tonic-gate fmt += i;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate fmt++;
4357c478bd9Sstevel@tonic-gate len += 2;
4367c478bd9Sstevel@tonic-gate bracket_str = (unsigned char *)
437d12182d8Srobbin malloc(sizeof (unsigned char) * (len + 1));
4387c478bd9Sstevel@tonic-gate if (bracket_str == NULL) {
4397c478bd9Sstevel@tonic-gate errno = ENOMEM;
4407c478bd9Sstevel@tonic-gate return (EOF);
4417c478bd9Sstevel@tonic-gate } else {
4427c478bd9Sstevel@tonic-gate (void) strncpy((char *)bracket_str,
4437c478bd9Sstevel@tonic-gate (const char *)p, len);
4447c478bd9Sstevel@tonic-gate *(bracket_str + len) = '\0';
4457c478bd9Sstevel@tonic-gate if (negflg &&
4467c478bd9Sstevel@tonic-gate *(bracket_str + 1) == '^') {
4477c478bd9Sstevel@tonic-gate *(bracket_str + 1) = '!';
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate } else {
4517c478bd9Sstevel@tonic-gate int t = 0;
4527c478bd9Sstevel@tonic-gate int b, c, d;
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate if (*fmt == '^') {
4557c478bd9Sstevel@tonic-gate t++;
4567c478bd9Sstevel@tonic-gate fmt++;
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate (void) memset(tab, !t, NCHARS);
4597c478bd9Sstevel@tonic-gate if ((c = *fmt) == ']' || c == '-') {
4607c478bd9Sstevel@tonic-gate tab[c] = t;
4617c478bd9Sstevel@tonic-gate fmt++;
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate
4647c478bd9Sstevel@tonic-gate while ((c = *fmt) != ']') {
4657c478bd9Sstevel@tonic-gate if (c == '\0') {
4667c478bd9Sstevel@tonic-gate return (EOF);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate b = *(fmt - 1);
4697c478bd9Sstevel@tonic-gate d = *(fmt + 1);
4707c478bd9Sstevel@tonic-gate if ((c == '-') && (d != ']') &&
4717c478bd9Sstevel@tonic-gate (b < d)) {
4727c478bd9Sstevel@tonic-gate (void) memset(&tab[b], t,
4737c478bd9Sstevel@tonic-gate d - b + 1);
4747c478bd9Sstevel@tonic-gate fmt += 2;
4757c478bd9Sstevel@tonic-gate } else {
4767c478bd9Sstevel@tonic-gate tab[c] = t;
4777c478bd9Sstevel@tonic-gate fmt++;
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate fmt++;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate #endif /* _WIDE */
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate #ifdef _WIDE
4867c478bd9Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) &&
4877c478bd9Sstevel@tonic-gate isupper((int)ch)) { /* no longer documented */
4887c478bd9Sstevel@tonic-gate if (_lib_version == c_issue_4) {
4897c478bd9Sstevel@tonic-gate if (size != 'm' && size != 'L')
4907c478bd9Sstevel@tonic-gate size = 'l';
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate ch = _tolower((int)ch);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate if (ch != 'n' && !flag_eof) {
4957c478bd9Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') {
4967c478bd9Sstevel@tonic-gate while (iswspace(inchar =
4977c478bd9Sstevel@tonic-gate _wd_getwc(&chcount, iop)))
4987c478bd9Sstevel@tonic-gate ;
4997c478bd9Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
5007c478bd9Sstevel@tonic-gate break;
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate #else /* _WIDE */
5057c478bd9Sstevel@tonic-gate if (isupper(ch)) { /* no longer documented */
5067c478bd9Sstevel@tonic-gate if (_lib_version == c_issue_4) {
5077c478bd9Sstevel@tonic-gate if (size != 'm' && size != 'L')
5087c478bd9Sstevel@tonic-gate size = 'l';
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate ch = _tolower(ch);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate if (ch != 'n' && !flag_eof) {
5137c478bd9Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') {
5147c478bd9Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount)))
5157c478bd9Sstevel@tonic-gate ;
5167c478bd9Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF)
5177c478bd9Sstevel@tonic-gate break;
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate switch (ch) {
5237c478bd9Sstevel@tonic-gate case 'C':
5247c478bd9Sstevel@tonic-gate case 'S':
5257c478bd9Sstevel@tonic-gate case 'c':
5267c478bd9Sstevel@tonic-gate case 's':
5277c478bd9Sstevel@tonic-gate #ifdef _WIDE
5287c478bd9Sstevel@tonic-gate if ((size == 'l') || (size == 'C') || (size == 'S'))
5297c478bd9Sstevel@tonic-gate #else /* _WIDE */
5307c478bd9Sstevel@tonic-gate if ((size == 'w') || (size == 'l') || (size == 'C') ||
5317c478bd9Sstevel@tonic-gate (size == 'S'))
5327c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate size = wstring(&chcount, &flag_eof, stow,
5357c478bd9Sstevel@tonic-gate (int)ch, len, iop, &args.ap);
5367c478bd9Sstevel@tonic-gate } else {
5377c478bd9Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow,
5387c478bd9Sstevel@tonic-gate (int)ch, len, tab, iop, &args.ap);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate break;
5417c478bd9Sstevel@tonic-gate case '[':
5427c478bd9Sstevel@tonic-gate if (size == 'l') {
5437c478bd9Sstevel@tonic-gate size = wbrstring(&chcount, &flag_eof, stow,
544d12182d8Srobbin (int)ch, len, iop, bracket_str, &args.ap);
5457c478bd9Sstevel@tonic-gate free(bracket_str);
5467c478bd9Sstevel@tonic-gate bracket_str = NULL;
5477c478bd9Sstevel@tonic-gate } else {
5487c478bd9Sstevel@tonic-gate #ifdef _WIDE
5497c478bd9Sstevel@tonic-gate size = brstring(&chcount, &flag_eof, stow,
550d12182d8Srobbin (int)ch, len, iop, bracket_str, &args.ap);
5517c478bd9Sstevel@tonic-gate free(bracket_str);
5527c478bd9Sstevel@tonic-gate bracket_str = NULL;
5537c478bd9Sstevel@tonic-gate #else /* _WIDE */
5547c478bd9Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow,
5557c478bd9Sstevel@tonic-gate ch, len, tab, iop, &args.ap);
5567c478bd9Sstevel@tonic-gate #endif /* _WIDE */
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate break;
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate case 'n':
5617c478bd9Sstevel@tonic-gate if (stow == 0)
5627c478bd9Sstevel@tonic-gate continue;
5637c478bd9Sstevel@tonic-gate if (size == 'b') /* char */
5647c478bd9Sstevel@tonic-gate *va_arg(args.ap, char *) = (char)chcount;
5657c478bd9Sstevel@tonic-gate else if (size == 'h')
5667c478bd9Sstevel@tonic-gate *va_arg(args.ap, short *) = (short)chcount;
5677c478bd9Sstevel@tonic-gate else if (size == 'l')
5687c478bd9Sstevel@tonic-gate *va_arg(args.ap, long *) = (long)chcount;
5697c478bd9Sstevel@tonic-gate else if (size == 'm') /* long long */
5707c478bd9Sstevel@tonic-gate *va_arg(args.ap, long long *) =
5717c478bd9Sstevel@tonic-gate (long long) chcount;
5727c478bd9Sstevel@tonic-gate else
5737c478bd9Sstevel@tonic-gate *va_arg(args.ap, int *) = (int)chcount;
5747c478bd9Sstevel@tonic-gate continue;
5757c478bd9Sstevel@tonic-gate
5767c478bd9Sstevel@tonic-gate case 'i':
5777c478bd9Sstevel@tonic-gate default:
5787c478bd9Sstevel@tonic-gate size = number(&chcount, &flag_eof, stow, (int)ch,
5797c478bd9Sstevel@tonic-gate len, (int)size, iop, &args.ap);
5807c478bd9Sstevel@tonic-gate break;
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate if (size)
5837c478bd9Sstevel@tonic-gate nmatch += stow;
5847c478bd9Sstevel@tonic-gate else {
5857c478bd9Sstevel@tonic-gate return ((flag_eof && !nmatch) ? EOF : nmatch);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate continue;
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate if (bracket_str)
5907c478bd9Sstevel@tonic-gate free(bracket_str);
5917c478bd9Sstevel@tonic-gate return (nmatch != 0 ? nmatch : EOF); /* end of input */
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate
5947c478bd9Sstevel@tonic-gate /* ****************************************************************** */
5957c478bd9Sstevel@tonic-gate /* Functions to read the input stream in an attempt to match incoming */
5967c478bd9Sstevel@tonic-gate /* data to the current pattern from the main loop of _doscan(). */
5977c478bd9Sstevel@tonic-gate /* ****************************************************************** */
5987c478bd9Sstevel@tonic-gate static int
number(int * chcount,int * flag_eof,int stow,int type,int len,int size,FILE * iop,va_list * listp)5997c478bd9Sstevel@tonic-gate number(int *chcount, int *flag_eof, int stow, int type, int len, int size,
6007c478bd9Sstevel@tonic-gate FILE *iop, va_list *listp)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate char numbuf[64];
6037c478bd9Sstevel@tonic-gate char *np = numbuf;
6047c478bd9Sstevel@tonic-gate int c, base, inchar, lookahead;
6057c478bd9Sstevel@tonic-gate int digitseen = 0, floater = 0, negflg = 0;
6067c478bd9Sstevel@tonic-gate int lc;
6077c478bd9Sstevel@tonic-gate long long lcval = 0LL;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate switch (type) {
6107c478bd9Sstevel@tonic-gate case 'e':
6117c478bd9Sstevel@tonic-gate case 'f':
6127c478bd9Sstevel@tonic-gate case 'g':
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * lc = 0 corresponds to c90 mode: do not recognize
6157c478bd9Sstevel@tonic-gate * hexadecimal fp strings; attempt to push back
6167c478bd9Sstevel@tonic-gate * all unused characters read
6177c478bd9Sstevel@tonic-gate *
6187c478bd9Sstevel@tonic-gate * lc = -1 corresponds to c99 mode: recognize hexa-
6197c478bd9Sstevel@tonic-gate * decimal fp strings; push back at most one
6207c478bd9Sstevel@tonic-gate * unused character
6217c478bd9Sstevel@tonic-gate */
6227c478bd9Sstevel@tonic-gate lc = (__xpg6 & _C99SUSv3_recognize_hexfp)? -1 : 0;
6237c478bd9Sstevel@tonic-gate floater = 1;
6247c478bd9Sstevel@tonic-gate break;
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate case 'a':
6277c478bd9Sstevel@tonic-gate lc = -1;
6287c478bd9Sstevel@tonic-gate floater = 1;
6297c478bd9Sstevel@tonic-gate break;
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate case 'd':
6327c478bd9Sstevel@tonic-gate case 'u':
6337c478bd9Sstevel@tonic-gate case 'i':
6347c478bd9Sstevel@tonic-gate base = 10;
6357c478bd9Sstevel@tonic-gate break;
6367c478bd9Sstevel@tonic-gate case 'o':
6377c478bd9Sstevel@tonic-gate base = 8;
6387c478bd9Sstevel@tonic-gate break;
6397c478bd9Sstevel@tonic-gate case 'p':
6407c478bd9Sstevel@tonic-gate #ifdef _LP64
6417c478bd9Sstevel@tonic-gate size = 'l'; /* pointers are long in LP64 */
6427c478bd9Sstevel@tonic-gate #endif /* _LP64 */
6437c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
6447c478bd9Sstevel@tonic-gate case 'x':
6457c478bd9Sstevel@tonic-gate base = 16;
6467c478bd9Sstevel@tonic-gate break;
6477c478bd9Sstevel@tonic-gate default:
6487c478bd9Sstevel@tonic-gate return (0); /* unrecognized conversion character */
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate if (floater != 0) {
6527c478bd9Sstevel@tonic-gate /*
6537c478bd9Sstevel@tonic-gate * Handle floating point with
6547c478bd9Sstevel@tonic-gate * file_to_decimal.
6557c478bd9Sstevel@tonic-gate */
6567c478bd9Sstevel@tonic-gate decimal_mode dm;
6577c478bd9Sstevel@tonic-gate decimal_record dr;
6587c478bd9Sstevel@tonic-gate fp_exception_field_type efs;
6597c478bd9Sstevel@tonic-gate enum decimal_string_form form;
6607c478bd9Sstevel@tonic-gate char *echar;
6617c478bd9Sstevel@tonic-gate int nread;
6627c478bd9Sstevel@tonic-gate char buffer[1024+1];
6637c478bd9Sstevel@tonic-gate char *nb = buffer;
6647c478bd9Sstevel@tonic-gate
6657c478bd9Sstevel@tonic-gate if (len > 1024)
6667c478bd9Sstevel@tonic-gate len = 1024;
6677c478bd9Sstevel@tonic-gate file_to_decimal(&nb, len, lc, &dr, &form, &echar, iop, &nread);
6687c478bd9Sstevel@tonic-gate if (lc == -1) {
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate * In C99 mode, the entire string read has to be
6717c478bd9Sstevel@tonic-gate * accepted in order to qualify as a match
6727c478bd9Sstevel@tonic-gate */
6737c478bd9Sstevel@tonic-gate if (nb != buffer + nread)
6747c478bd9Sstevel@tonic-gate form = invalid_form;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate if (stow && (form != invalid_form)) {
6777c478bd9Sstevel@tonic-gate #if defined(__sparc)
6787c478bd9Sstevel@tonic-gate dm.rd = _QgetRD();
6797c478bd9Sstevel@tonic-gate if (size == 'L') { /* long double */
6807c478bd9Sstevel@tonic-gate if ((int)form < 0)
6817c478bd9Sstevel@tonic-gate __hex_to_quadruple(&dr, dm.rd,
682d12182d8Srobbin va_arg(*listp, quadruple *), &efs);
6837c478bd9Sstevel@tonic-gate else
6847c478bd9Sstevel@tonic-gate decimal_to_quadruple(
6857c478bd9Sstevel@tonic-gate va_arg(*listp, quadruple *),
6867c478bd9Sstevel@tonic-gate &dm, &dr, &efs);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
6897c478bd9Sstevel@tonic-gate dm.rd = __xgetRD();
6907c478bd9Sstevel@tonic-gate if (size == 'L') { /* long double */
6917c478bd9Sstevel@tonic-gate if ((int)form < 0)
6927c478bd9Sstevel@tonic-gate __hex_to_extended(&dr, dm.rd,
693d12182d8Srobbin va_arg(*listp, extended *), &efs);
6947c478bd9Sstevel@tonic-gate else
6957c478bd9Sstevel@tonic-gate decimal_to_extended(
6967c478bd9Sstevel@tonic-gate va_arg(*listp, extended *),
6977c478bd9Sstevel@tonic-gate &dm, &dr, &efs);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate #else
7007c478bd9Sstevel@tonic-gate #error Unknown architecture
7017c478bd9Sstevel@tonic-gate #endif
7027c478bd9Sstevel@tonic-gate else if (size == 'l') { /* double */
7037c478bd9Sstevel@tonic-gate if ((int)form < 0)
7047c478bd9Sstevel@tonic-gate __hex_to_double(&dr, dm.rd,
7057c478bd9Sstevel@tonic-gate va_arg(*listp, double *), &efs);
7067c478bd9Sstevel@tonic-gate else
7077c478bd9Sstevel@tonic-gate decimal_to_double(
7087c478bd9Sstevel@tonic-gate va_arg(*listp, double *),
7097c478bd9Sstevel@tonic-gate &dm, &dr, &efs);
7107c478bd9Sstevel@tonic-gate } else { /* float */
7117c478bd9Sstevel@tonic-gate if ((int)form < 0)
7127c478bd9Sstevel@tonic-gate __hex_to_single(&dr, dm.rd,
7137c478bd9Sstevel@tonic-gate va_arg(*listp, single *), &efs);
7147c478bd9Sstevel@tonic-gate else
7157c478bd9Sstevel@tonic-gate decimal_to_single((single *)
7167c478bd9Sstevel@tonic-gate va_arg(*listp, single *),
7177c478bd9Sstevel@tonic-gate &dm, &dr, &efs);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate if ((efs & (1 << fp_overflow)) != 0) {
7207c478bd9Sstevel@tonic-gate errno = ERANGE;
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate if ((efs & (1 << fp_underflow)) != 0) {
7237c478bd9Sstevel@tonic-gate errno = ERANGE;
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate (*chcount) += nread; /* Count characters read. */
7277c478bd9Sstevel@tonic-gate c = locgetc((*chcount));
7287c478bd9Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF)
7297c478bd9Sstevel@tonic-gate *flag_eof = 1;
7307c478bd9Sstevel@tonic-gate return ((form == invalid_form) ? 0 : 1);
7317c478bd9Sstevel@tonic-gate /* successful match if non-zero */
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate switch (c = locgetc((*chcount))) {
7357c478bd9Sstevel@tonic-gate case '-':
7367c478bd9Sstevel@tonic-gate negflg++;
7377c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
7387c478bd9Sstevel@tonic-gate case '+':
7397c478bd9Sstevel@tonic-gate if (--len <= 0)
7407c478bd9Sstevel@tonic-gate break;
7417c478bd9Sstevel@tonic-gate if ((c = locgetc((*chcount))) != '0')
7427c478bd9Sstevel@tonic-gate break;
7437c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
7447c478bd9Sstevel@tonic-gate case '0':
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate * If %i or %x, the characters 0x or 0X may optionally precede
7477c478bd9Sstevel@tonic-gate * the sequence of letters and digits (base 16).
7487c478bd9Sstevel@tonic-gate */
7497c478bd9Sstevel@tonic-gate if ((type != 'i' && type != 'x') || (len <= 1))
7507c478bd9Sstevel@tonic-gate break;
7517c478bd9Sstevel@tonic-gate if (((inchar = locgetc((*chcount))) == 'x') ||
7527c478bd9Sstevel@tonic-gate (inchar == 'X')) {
7537c478bd9Sstevel@tonic-gate lookahead = readchar(iop, chcount);
7547c478bd9Sstevel@tonic-gate if (isxdigit(lookahead)) {
7557c478bd9Sstevel@tonic-gate base = 16;
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate if (len <= 2) {
7587c478bd9Sstevel@tonic-gate (void) locungetc((*chcount), lookahead);
7597c478bd9Sstevel@tonic-gate /* Take into account the 'x' */
7607c478bd9Sstevel@tonic-gate len -= 1;
7617c478bd9Sstevel@tonic-gate } else {
7627c478bd9Sstevel@tonic-gate c = lookahead;
7637c478bd9Sstevel@tonic-gate /* Take into account '0x' */
7647c478bd9Sstevel@tonic-gate len -= 2;
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate } else {
7677c478bd9Sstevel@tonic-gate (void) locungetc((*chcount), lookahead);
7687c478bd9Sstevel@tonic-gate (void) locungetc((*chcount), inchar);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate } else {
7717c478bd9Sstevel@tonic-gate /* inchar wans't 'x'. */
7727c478bd9Sstevel@tonic-gate (void) locungetc((*chcount), inchar); /* Put it back. */
7737c478bd9Sstevel@tonic-gate if (type == 'i') /* Only %i accepts an octal. */
7747c478bd9Sstevel@tonic-gate base = 8;
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate for (; --len >= 0; *np++ = (char)c, c = locgetc((*chcount))) {
7787c478bd9Sstevel@tonic-gate if (np > numbuf + 62) {
7797c478bd9Sstevel@tonic-gate errno = ERANGE;
7807c478bd9Sstevel@tonic-gate return (0);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate if (isdigit(c) || base == 16 && isxdigit(c)) {
7837c478bd9Sstevel@tonic-gate int digit = c - (isdigit(c) ? '0' :
7847c478bd9Sstevel@tonic-gate isupper(c) ? 'A' - 10 : 'a' - 10);
7857c478bd9Sstevel@tonic-gate if (digit >= base)
7867c478bd9Sstevel@tonic-gate break;
7877c478bd9Sstevel@tonic-gate if (stow)
7887c478bd9Sstevel@tonic-gate lcval = base * lcval + digit;
7897c478bd9Sstevel@tonic-gate digitseen++;
7907c478bd9Sstevel@tonic-gate continue;
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate break;
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate if (stow && digitseen) {
7967c478bd9Sstevel@tonic-gate /* suppress possible overflow on 2's-comp negation */
7977c478bd9Sstevel@tonic-gate if (negflg && lcval != (1ULL << 63))
7987c478bd9Sstevel@tonic-gate lcval = -lcval;
7997c478bd9Sstevel@tonic-gate switch (size) {
8007c478bd9Sstevel@tonic-gate case 'm':
8017c478bd9Sstevel@tonic-gate *va_arg(*listp, long long *) = lcval;
8027c478bd9Sstevel@tonic-gate break;
8037c478bd9Sstevel@tonic-gate case 'l':
8047c478bd9Sstevel@tonic-gate *va_arg(*listp, long *) = (long)lcval;
8057c478bd9Sstevel@tonic-gate break;
8067c478bd9Sstevel@tonic-gate case 'h':
8077c478bd9Sstevel@tonic-gate *va_arg(*listp, short *) = (short)lcval;
8087c478bd9Sstevel@tonic-gate break;
8097c478bd9Sstevel@tonic-gate case 'b':
8107c478bd9Sstevel@tonic-gate *va_arg(*listp, char *) = (char)lcval;
8117c478bd9Sstevel@tonic-gate break;
8127c478bd9Sstevel@tonic-gate default:
8137c478bd9Sstevel@tonic-gate *va_arg(*listp, int *) = (int)lcval;
8147c478bd9Sstevel@tonic-gate break;
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF)
8187c478bd9Sstevel@tonic-gate *flag_eof = 1;
8197c478bd9Sstevel@tonic-gate return (digitseen); /* successful match if non-zero */
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate /* Get a character. If not using sscanf and at the buffer's end */
8237c478bd9Sstevel@tonic-gate /* then do a direct read(). Characters read via readchar() */
8247c478bd9Sstevel@tonic-gate /* can be pushed back on the input stream by locungetc((*chcount),) */
8257c478bd9Sstevel@tonic-gate /* since there is padding allocated at the end of the stream buffer. */
8267c478bd9Sstevel@tonic-gate static int
readchar(FILE * iop,int * chcount)8277c478bd9Sstevel@tonic-gate readchar(FILE *iop, int *chcount)
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate int inchar;
8307c478bd9Sstevel@tonic-gate char buf[1];
8317c478bd9Sstevel@tonic-gate
832cd62a92dSRobert Mustacchi if ((iop->_flag & _IOWRT) || (iop->_cnt != 0)) {
8337c478bd9Sstevel@tonic-gate inchar = locgetc((*chcount));
834cd62a92dSRobert Mustacchi } else {
835cd62a92dSRobert Mustacchi if (_xread(iop, buf, 1) != 1)
8367c478bd9Sstevel@tonic-gate return (EOF);
8377c478bd9Sstevel@tonic-gate inchar = (int)buf[0];
8387c478bd9Sstevel@tonic-gate (*chcount) += 1;
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate return (inchar);
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate static int
string(int * chcount,int * flag_eof,int stow,int type,int len,char * tab __unused,FILE * iop,va_list * listp)844*4a38094cSToomas Soome string(int *chcount, int *flag_eof, int stow, int type, int len,
845*4a38094cSToomas Soome char *tab __unused, FILE *iop, va_list *listp)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate int ch;
8487c478bd9Sstevel@tonic-gate char *ptr;
8497c478bd9Sstevel@tonic-gate char *start;
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, char *) : NULL;
8527c478bd9Sstevel@tonic-gate if (((type == 'c') || (type == 'C')) && len == MAXINT)
8537c478bd9Sstevel@tonic-gate len = 1;
8547c478bd9Sstevel@tonic-gate #ifdef _WIDE
8557c478bd9Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF &&
8567c478bd9Sstevel@tonic-gate !(((type == 's') || (type == 'S')) && isspace(ch))) {
8577c478bd9Sstevel@tonic-gate #else /* _WIDE */
8587c478bd9Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF &&
8597c478bd9Sstevel@tonic-gate !(((type == 's') || (type == 'S')) &&
8607c478bd9Sstevel@tonic-gate isspace(ch) || type == '[' && tab[ch])) {
8617c478bd9Sstevel@tonic-gate #endif /* _WIDE */
8627c478bd9Sstevel@tonic-gate if (stow)
8637c478bd9Sstevel@tonic-gate *ptr = (char)ch;
8647c478bd9Sstevel@tonic-gate ptr++;
8657c478bd9Sstevel@tonic-gate if (--len <= 0)
8667c478bd9Sstevel@tonic-gate break;
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate if (ch == EOF) {
8697c478bd9Sstevel@tonic-gate (*flag_eof) = 1;
8707c478bd9Sstevel@tonic-gate (*chcount) -= 1;
8717c478bd9Sstevel@tonic-gate } else if (len > 0 && locungetc((*chcount), ch) == EOF)
8727c478bd9Sstevel@tonic-gate (*flag_eof) = 1;
8737c478bd9Sstevel@tonic-gate if (ptr == start)
8747c478bd9Sstevel@tonic-gate return (0); /* no match */
8757c478bd9Sstevel@tonic-gate if (stow && ((type != 'c') && (type != 'C')))
8767c478bd9Sstevel@tonic-gate *ptr = '\0';
8777c478bd9Sstevel@tonic-gate return (1); /* successful match */
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate
8807c478bd9Sstevel@tonic-gate /* This function initializes arglst, to contain the appropriate */
8817c478bd9Sstevel@tonic-gate /* va_list values for the first MAXARGS arguments. */
8827c478bd9Sstevel@tonic-gate /* WARNING: this code assumes that the sizes of all pointer types */
8837c478bd9Sstevel@tonic-gate /* are the same. (Code similar to that in the portable doprnt.c */
8847c478bd9Sstevel@tonic-gate /* should be used if this assumption is not true for a */
8857c478bd9Sstevel@tonic-gate /* particular port.) */
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate #ifdef _WIDE
8887c478bd9Sstevel@tonic-gate static int
8897c478bd9Sstevel@tonic-gate _mkarglst(const wchar_t *fmt, stva_list args, stva_list arglst[])
8907c478bd9Sstevel@tonic-gate #else /* _WIDE */
8917c478bd9Sstevel@tonic-gate static int
8927c478bd9Sstevel@tonic-gate _mkarglst(const char *fmt, stva_list args, stva_list arglst[])
8937c478bd9Sstevel@tonic-gate #endif /* _WIDE */
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate #ifdef _WIDE
8967c478bd9Sstevel@tonic-gate #define STRCHR wcschr
8977c478bd9Sstevel@tonic-gate #define STRSPN wcsspn
8987c478bd9Sstevel@tonic-gate #define ATOI(x) _watoi((wchar_t *)x)
8997c478bd9Sstevel@tonic-gate #define SPNSTR1 L"01234567890"
9007c478bd9Sstevel@tonic-gate #define SPNSTR2 L"# +-.0123456789hL$"
9017c478bd9Sstevel@tonic-gate #else /* _WIDE */
9027c478bd9Sstevel@tonic-gate #define STRCHR strchr
9037c478bd9Sstevel@tonic-gate #define STRSPN strspn
9047c478bd9Sstevel@tonic-gate #define ATOI(x) atoi(x)
9057c478bd9Sstevel@tonic-gate #define SPNSTR1 "01234567890"
9067c478bd9Sstevel@tonic-gate #define SPNSTR2 "# +-.0123456789hL$"
9077c478bd9Sstevel@tonic-gate #endif /* _WIDE */
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate int maxnum, curargno;
9107c478bd9Sstevel@tonic-gate size_t n;
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate maxnum = -1;
9137c478bd9Sstevel@tonic-gate curargno = 0;
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate while ((fmt = STRCHR(fmt, '%')) != NULL) {
9167c478bd9Sstevel@tonic-gate fmt++; /* skip % */
9177c478bd9Sstevel@tonic-gate if (*fmt == '*' || *fmt == '%')
9187c478bd9Sstevel@tonic-gate continue;
9197c478bd9Sstevel@tonic-gate if (fmt[n = STRSPN(fmt, SPNSTR1)] == L'$') {
9207c478bd9Sstevel@tonic-gate /* convert to zero base */
9217c478bd9Sstevel@tonic-gate curargno = ATOI(fmt) - 1;
9227c478bd9Sstevel@tonic-gate fmt += n + 1;
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate if (maxnum < curargno)
9267c478bd9Sstevel@tonic-gate maxnum = curargno;
9277c478bd9Sstevel@tonic-gate curargno++; /* default to next in list */
9287c478bd9Sstevel@tonic-gate
9297c478bd9Sstevel@tonic-gate fmt += STRSPN(fmt, SPNSTR2);
9307c478bd9Sstevel@tonic-gate if (*fmt == '[') {
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 {
9468a67df42SToomas Soome int i;
9478a67df42SToomas Soome
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 */
973d12182d8Srobbin
9747c478bd9Sstevel@tonic-gate #ifdef _WIDE
9757c478bd9Sstevel@tonic-gate static int
wstring(int * chcount,int * flag_eof,int stow,int type,int len,FILE * iop,va_list * listp)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
9837c478bd9Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
9847c478bd9Sstevel@tonic-gate
9857c478bd9Sstevel@tonic-gate if ((type == 'c') && len == MAXINT)
9867c478bd9Sstevel@tonic-gate len = 1;
9877c478bd9Sstevel@tonic-gate while (((wch = _wd_getwc(chcount, iop)) != WEOF) &&
9887c478bd9Sstevel@tonic-gate !(type == 's' && iswspace(wch))) {
9897c478bd9Sstevel@tonic-gate if (stow)
9907c478bd9Sstevel@tonic-gate *ptr = wch;
9917c478bd9Sstevel@tonic-gate ptr++;
9927c478bd9Sstevel@tonic-gate if (--len <= 0)
9937c478bd9Sstevel@tonic-gate break;
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate if (wch == WEOF) {
9967c478bd9Sstevel@tonic-gate *flag_eof = 1;
9977c478bd9Sstevel@tonic-gate (*chcount) -= 1;
9987c478bd9Sstevel@tonic-gate } else {
9997c478bd9Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(chcount, wch, iop) == WEOF)
10007c478bd9Sstevel@tonic-gate *flag_eof = 1;
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate if (ptr == wstart)
10037c478bd9Sstevel@tonic-gate return (0); /* no match */
10047c478bd9Sstevel@tonic-gate if (stow && (type != 'c'))
10057c478bd9Sstevel@tonic-gate *ptr = '\0';
10067c478bd9Sstevel@tonic-gate return (1); /* successful match */
10077c478bd9Sstevel@tonic-gate }
1008d12182d8Srobbin
10097c478bd9Sstevel@tonic-gate #else /* _WIDE */
10107c478bd9Sstevel@tonic-gate static int
wstring(int * chcount,int * flag_eof,int stow,int type,int len,FILE * iop,va_list * listp)10117c478bd9Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type, int len, FILE *iop,
10127c478bd9Sstevel@tonic-gate va_list *listp)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate int wch;
10157c478bd9Sstevel@tonic-gate wchar_t *ptr;
10167c478bd9Sstevel@tonic-gate wchar_t *wstart;
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
10197c478bd9Sstevel@tonic-gate
10207c478bd9Sstevel@tonic-gate if ((type == 'c') && len == MAXINT)
10217c478bd9Sstevel@tonic-gate len = 1;
10227c478bd9Sstevel@tonic-gate while (((wch = _bi_getwc(iop)) != EOF) &&
10237c478bd9Sstevel@tonic-gate !(type == 's' && (isascii(wch) ? isspace(wch) : 0))) {
10247c478bd9Sstevel@tonic-gate (*chcount) += _scrwidth((wchar_t)wch);
10257c478bd9Sstevel@tonic-gate if (stow)
10267c478bd9Sstevel@tonic-gate *ptr = wch;
10277c478bd9Sstevel@tonic-gate ptr++;
10287c478bd9Sstevel@tonic-gate if (--len <= 0)
10297c478bd9Sstevel@tonic-gate break;
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate if (wch == EOF) {
10327c478bd9Sstevel@tonic-gate (*flag_eof) = 1;
10337c478bd9Sstevel@tonic-gate (*chcount) -= 1;
10347c478bd9Sstevel@tonic-gate } else {
10357c478bd9Sstevel@tonic-gate if (len > 0 && _bi_ungetwc(wch, iop) == EOF)
10367c478bd9Sstevel@tonic-gate (*flag_eof) = 1;
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate if (ptr == wstart)
10397c478bd9Sstevel@tonic-gate return (0); /* no match */
10407c478bd9Sstevel@tonic-gate if (stow && (type != 'c'))
10417c478bd9Sstevel@tonic-gate *ptr = '\0';
10427c478bd9Sstevel@tonic-gate return (1); /* successful match */
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate #endif /* _WIDE */
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate #ifdef _WIDE
10477c478bd9Sstevel@tonic-gate static wint_t
_wd_getwc(int * chcount,FILE * iop)10487c478bd9Sstevel@tonic-gate _wd_getwc(int *chcount, FILE *iop)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate wint_t wc;
10517c478bd9Sstevel@tonic-gate int len;
10527c478bd9Sstevel@tonic-gate
10537c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
10547c478bd9Sstevel@tonic-gate /* call from fwscanf, wscanf */
10557c478bd9Sstevel@tonic-gate wc = __fgetwc_xpg5(iop);
10567c478bd9Sstevel@tonic-gate (*chcount)++;
10577c478bd9Sstevel@tonic-gate return (wc);
10587c478bd9Sstevel@tonic-gate } else {
10597c478bd9Sstevel@tonic-gate /* call from swscanf */
10607c478bd9Sstevel@tonic-gate if (*iop->_ptr == '\0')
10617c478bd9Sstevel@tonic-gate return (WEOF);
10627c478bd9Sstevel@tonic-gate len = mbtowc((wchar_t *)&wc, (const char *)iop->_ptr,
10637c478bd9Sstevel@tonic-gate MB_CUR_MAX);
10647c478bd9Sstevel@tonic-gate if (len == -1)
10657c478bd9Sstevel@tonic-gate return (WEOF);
10667c478bd9Sstevel@tonic-gate iop->_ptr += len;
10677c478bd9Sstevel@tonic-gate (*chcount)++;
10687c478bd9Sstevel@tonic-gate return (wc);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate
10727c478bd9Sstevel@tonic-gate static wint_t
_wd_ungetwc(int * chcount,wchar_t wc,FILE * iop)10737c478bd9Sstevel@tonic-gate _wd_ungetwc(int *chcount, wchar_t wc, FILE *iop)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate wint_t ret;
10767c478bd9Sstevel@tonic-gate int len;
10777c478bd9Sstevel@tonic-gate char mbs[MB_LEN_MAX];
10787c478bd9Sstevel@tonic-gate
10797c478bd9Sstevel@tonic-gate if (wc == WEOF)
10807c478bd9Sstevel@tonic-gate return (WEOF);
10817c478bd9Sstevel@tonic-gate
10827c478bd9Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
10837c478bd9Sstevel@tonic-gate /* call from fwscanf, wscanf */
10847c478bd9Sstevel@tonic-gate ret = __ungetwc_xpg5((wint_t)wc, iop);
10857c478bd9Sstevel@tonic-gate if (ret != (wint_t)wc)
10867c478bd9Sstevel@tonic-gate return (WEOF);
10877c478bd9Sstevel@tonic-gate (*chcount)--;
10887c478bd9Sstevel@tonic-gate return (ret);
10897c478bd9Sstevel@tonic-gate } else {
10907c478bd9Sstevel@tonic-gate /* call from swscanf */
10917c478bd9Sstevel@tonic-gate len = wctomb(mbs, wc);
10927c478bd9Sstevel@tonic-gate if (len == -1)
10937c478bd9Sstevel@tonic-gate return (WEOF);
10947c478bd9Sstevel@tonic-gate iop->_ptr -= len;
10957c478bd9Sstevel@tonic-gate (*chcount)--;
10967c478bd9Sstevel@tonic-gate return ((wint_t)wc);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate static int
_watoi(wchar_t * fmt)11017c478bd9Sstevel@tonic-gate _watoi(wchar_t *fmt)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate int n = 0;
11047c478bd9Sstevel@tonic-gate wchar_t ch;
11057c478bd9Sstevel@tonic-gate
11067c478bd9Sstevel@tonic-gate ch = *fmt;
11077c478bd9Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) && isdigit((int)ch)) {
11087c478bd9Sstevel@tonic-gate n = ch - '0';
11097c478bd9Sstevel@tonic-gate while (((ch = *++fmt) >= 0) && (ch < 256) &&
11107c478bd9Sstevel@tonic-gate isdigit((int)ch)) {
11117c478bd9Sstevel@tonic-gate n *= 10;
11127c478bd9Sstevel@tonic-gate n += ch - '0';
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate return (n);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate #endif /* _WIDE */
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate static int
wbrstring(int * chcount,int * flag_eof,int stow,int type __unused,int len,FILE * iop,unsigned char * brstr,va_list * listp)1120*4a38094cSToomas Soome wbrstring(int *chcount, int *flag_eof, int stow, int type __unused,
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
1184*4a38094cSToomas Soome brstring(int *chcount, int *flag_eof, int stow, int type __unused,
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