10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56812Sraf * Common Development and Distribution License (the "License").
66812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
226812Sraf /*
236812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
246812Sraf * Use is subject to license terms.
256812Sraf */
266812Sraf
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
326812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include "mtlib.h"
350Sstevel@tonic-gate #include "file64.h"
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <stdarg.h>
390Sstevel@tonic-gate #include <values.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <math.h>
440Sstevel@tonic-gate #include <thread.h>
450Sstevel@tonic-gate #include <synch.h>
460Sstevel@tonic-gate #include <stdlib.h>
470Sstevel@tonic-gate #include <fnmatch.h>
480Sstevel@tonic-gate #include <limits.h>
490Sstevel@tonic-gate #include <wchar.h>
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate #include "libc.h"
520Sstevel@tonic-gate #include "stdiom.h"
530Sstevel@tonic-gate #include "xpg6.h"
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define NCHARS (1 << BITSPERBYTE)
560Sstevel@tonic-gate
570Sstevel@tonic-gate /* if the _IOWRT flag is set, this must be a call from sscanf */
580Sstevel@tonic-gate #define locgetc(cnt) (cnt += 1, (iop->_flag & _IOWRT) ? \
590Sstevel@tonic-gate ((*iop->_ptr == '\0') ? EOF : *iop->_ptr++) : \
600Sstevel@tonic-gate GETC(iop))
610Sstevel@tonic-gate #define locungetc(cnt, x) (cnt -= 1, (x == EOF) ? EOF : \
620Sstevel@tonic-gate ((iop->_flag & _IOWRT) ? *(--iop->_ptr) : \
630Sstevel@tonic-gate (++iop->_cnt, *(--iop->_ptr))))
640Sstevel@tonic-gate
650Sstevel@tonic-gate #define wlocgetc() ((iop->_flag & _IOWRT) ? \
660Sstevel@tonic-gate ((*iop->_ptr == '\0') ? EOF : *iop->_ptr++) : \
670Sstevel@tonic-gate GETC(iop))
680Sstevel@tonic-gate #define wlocungetc(x) ((x == EOF) ? EOF : \
690Sstevel@tonic-gate ((iop->_flag & _IOWRT) ? *(--iop->_ptr) : \
700Sstevel@tonic-gate UNGETC(x, iop)))
710Sstevel@tonic-gate
720Sstevel@tonic-gate #define MAXARGS 30 /* max. number of args for fast positional paramters */
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * stva_list is used to subvert C's restriction that a variable with an
760Sstevel@tonic-gate * array type can not appear on the left hand side of an assignment operator.
770Sstevel@tonic-gate * By putting the array inside a structure, the functionality of assigning to
780Sstevel@tonic-gate * the whole array through a simple assignment is achieved..
790Sstevel@tonic-gate */
800Sstevel@tonic-gate typedef struct stva_list {
810Sstevel@tonic-gate va_list ap;
820Sstevel@tonic-gate } stva_list;
830Sstevel@tonic-gate
840Sstevel@tonic-gate static int number(int *, int *, int, int, int, int, FILE *, va_list *);
850Sstevel@tonic-gate static int readchar(FILE *, int *);
860Sstevel@tonic-gate static int string(int *, int *, int, int, int, char *, FILE *, va_list *);
870Sstevel@tonic-gate static int wstring(int *, int *, int, int, int, FILE *, va_list *);
880Sstevel@tonic-gate static int wbrstring(int *, int *, int, int, int, FILE *,
890Sstevel@tonic-gate unsigned char *, va_list *);
900Sstevel@tonic-gate #ifdef _WIDE
910Sstevel@tonic-gate static int brstring(int *, int *, int, int, int, FILE *,
920Sstevel@tonic-gate unsigned char *, va_list *);
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate static int _bi_getwc(FILE *);
950Sstevel@tonic-gate static int _bi_ungetwc(wint_t, FILE *);
960Sstevel@tonic-gate
970Sstevel@tonic-gate #ifdef _WIDE
980Sstevel@tonic-gate static int _mkarglst(const wchar_t *, stva_list, stva_list[]);
990Sstevel@tonic-gate static wint_t _wd_getwc(int *, FILE *);
1000Sstevel@tonic-gate static wint_t _wd_ungetwc(int *, wchar_t, FILE *);
1010Sstevel@tonic-gate static int _watoi(wchar_t *);
1020Sstevel@tonic-gate #else /* _WIDE */
1030Sstevel@tonic-gate static int _mkarglst(const char *, stva_list, stva_list[]);
1040Sstevel@tonic-gate #endif /* _WIDE */
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate #ifndef _WIDE
1070Sstevel@tonic-gate int
_doscan(FILE * iop,const char * fmt,va_list va_Alist)1080Sstevel@tonic-gate _doscan(FILE *iop, const char *fmt, va_list va_Alist)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate int ret;
1110Sstevel@tonic-gate rmutex_t *lk;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (iop->_flag & _IOWRT)
1140Sstevel@tonic-gate ret = __doscan_u(iop, fmt, va_Alist, 0);
1150Sstevel@tonic-gate else {
1160Sstevel@tonic-gate FLOCKFILE(lk, iop);
1170Sstevel@tonic-gate ret = __doscan_u(iop, fmt, va_Alist, 0);
1180Sstevel@tonic-gate FUNLOCKFILE(lk);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate return (ret);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate #endif /* _WIDE */
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /* ARGSUSED3 */
1250Sstevel@tonic-gate #ifdef _WIDE
1260Sstevel@tonic-gate int
__wdoscan_u(FILE * iop,const wchar_t * fmt,va_list va_Alist,int scflag)1270Sstevel@tonic-gate __wdoscan_u(FILE *iop, const wchar_t *fmt, va_list va_Alist, int scflag)
1280Sstevel@tonic-gate #else /* _WIDE */
1290Sstevel@tonic-gate int
1300Sstevel@tonic-gate __doscan_u(FILE *iop, const char *sfmt, va_list va_Alist, int scflag)
1310Sstevel@tonic-gate #endif /* _WIDE */
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate #ifdef _WIDE
1340Sstevel@tonic-gate wchar_t ch;
1350Sstevel@tonic-gate wchar_t inchar, size;
1360Sstevel@tonic-gate int nmatch = 0, len, stow;
1370Sstevel@tonic-gate #else /* _WIDE */
1380Sstevel@tonic-gate int ch;
1390Sstevel@tonic-gate int nmatch = 0, len, inchar, stow, size;
1400Sstevel@tonic-gate #endif /* _WIDE */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate unsigned char *bracket_str = NULL;
1430Sstevel@tonic-gate int chcount, flag_eof;
1440Sstevel@tonic-gate char tab[NCHARS];
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /* variables for postional parameters */
1470Sstevel@tonic-gate #ifdef _WIDE
1480Sstevel@tonic-gate const wchar_t *sformat = fmt; /* save the beginning of the format */
1490Sstevel@tonic-gate #else /* _WIDE */
1500Sstevel@tonic-gate const unsigned char *fmt = (const unsigned char *)sfmt;
1510Sstevel@tonic-gate const char *sformat = sfmt; /* save the beginning of the format */
1520Sstevel@tonic-gate #endif /* _WIDE */
1530Sstevel@tonic-gate int fpos = 1; /* 1 if first postional parameter */
154*7074Srobbin stva_list args; /* used to step through the argument list */
155*7074Srobbin stva_list sargs; /* used to save start of the argument list */
1560Sstevel@tonic-gate stva_list arglst[MAXARGS];
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * array giving the appropriate values
1590Sstevel@tonic-gate * for va_arg() to retrieve the
1600Sstevel@tonic-gate * corresponding argument:
1610Sstevel@tonic-gate * arglst[0] is the first argument
1620Sstevel@tonic-gate * arglst[1] is the second argument,etc.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate /* Check if readable stream */
1650Sstevel@tonic-gate if (!(iop->_flag & (_IOREAD | _IORW))) {
1660Sstevel@tonic-gate errno = EBADF;
1670Sstevel@tonic-gate return (EOF);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Initialize args and sargs to the start of the argument list.
1720Sstevel@tonic-gate * We don't know any portable way to copy an arbitrary C object
1730Sstevel@tonic-gate * so we use a system-specific routine(probably a macro) from
1740Sstevel@tonic-gate * stdarg.h. (Remember that if va_list is an array, in_args will
1750Sstevel@tonic-gate * be a pointer and &in_args won't be what we would want for
1760Sstevel@tonic-gate * memcpy.)
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate va_copy(args.ap, va_Alist);
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate sargs = args;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate chcount = 0; flag_eof = 0;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * ****************************************************
1860Sstevel@tonic-gate * Main loop: reads format to determine a pattern,
1870Sstevel@tonic-gate * and then goes to read input stream
1880Sstevel@tonic-gate * in attempt to match the pattern.
1890Sstevel@tonic-gate * ****************************************************
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate for (; ; ) {
1920Sstevel@tonic-gate if ((ch = *fmt++) == '\0') {
1930Sstevel@tonic-gate return (nmatch); /* end of format */
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate #ifdef _WIDE
1960Sstevel@tonic-gate if (iswspace(ch)) {
1970Sstevel@tonic-gate if (!flag_eof) {
1980Sstevel@tonic-gate while (iswspace(inchar =
199*7074Srobbin _wd_getwc(&chcount, iop)))
2000Sstevel@tonic-gate ;
2010Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
2020Sstevel@tonic-gate flag_eof = 1;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate continue;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate if (ch != '%' || (ch = *fmt++) == '%') {
207*7074Srobbin if (ch == '%') {
208*7074Srobbin if (!flag_eof) {
209*7074Srobbin while (iswspace(inchar =
210*7074Srobbin _wd_getwc(&chcount, iop)))
211*7074Srobbin ;
212*7074Srobbin if (_wd_ungetwc(&chcount, inchar, iop)
213*7074Srobbin == WEOF)
214*7074Srobbin flag_eof = 1;
215*7074Srobbin }
216*7074Srobbin }
2170Sstevel@tonic-gate if ((inchar = _wd_getwc(&chcount, iop)) == ch)
2180Sstevel@tonic-gate continue;
2190Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) != WEOF) {
2200Sstevel@tonic-gate return (nmatch); /* failed to match input */
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate break;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate #else /* _WIDE */
2250Sstevel@tonic-gate if (isspace(ch)) {
2260Sstevel@tonic-gate if (!flag_eof) {
2270Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount)))
2280Sstevel@tonic-gate ;
2290Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF)
2300Sstevel@tonic-gate flag_eof = 1;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate continue;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate if (ch != '%' || (ch = *fmt++) == '%') {
236*7074Srobbin if (ch == '%') {
237*7074Srobbin if (!flag_eof) {
238*7074Srobbin while (isspace(inchar =
239*7074Srobbin locgetc(chcount)))
240*7074Srobbin ;
241*7074Srobbin if (locungetc(chcount, inchar) == EOF)
242*7074Srobbin flag_eof = 1;
243*7074Srobbin }
244*7074Srobbin }
2450Sstevel@tonic-gate if ((inchar = locgetc(chcount)) == ch)
2460Sstevel@tonic-gate continue;
2470Sstevel@tonic-gate if (locungetc(chcount, inchar) != EOF) {
2480Sstevel@tonic-gate return (nmatch); /* failed to match input */
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate break;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate #endif /* _WIDE */
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate charswitch: /* target of a goto 8-( */
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate if (ch == '*') {
2570Sstevel@tonic-gate stow = 0;
2580Sstevel@tonic-gate ch = *fmt++;
2590Sstevel@tonic-gate } else
2600Sstevel@tonic-gate stow = 1;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate #ifdef _WIDE
2630Sstevel@tonic-gate for (len = 0; ((ch >= 0) && (ch < 256) && isdigit(ch));
264*7074Srobbin ch = *fmt++)
2650Sstevel@tonic-gate len = len * 10 + ch - '0';
2660Sstevel@tonic-gate #else /* _WIDE */
2670Sstevel@tonic-gate for (len = 0; isdigit(ch); ch = *fmt++)
2680Sstevel@tonic-gate len = len * 10 + ch - '0';
2690Sstevel@tonic-gate #endif /* _WIDE */
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if (ch == '$') {
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * positional parameter handling - the number
2740Sstevel@tonic-gate * specified in len gives the argument to which
2750Sstevel@tonic-gate * the next conversion should be applied.
2760Sstevel@tonic-gate * WARNING: This implementation of positional
2770Sstevel@tonic-gate * parameters assumes that the sizes of all pointer
2780Sstevel@tonic-gate * types are the same. (Code similar to that
2790Sstevel@tonic-gate * in the portable doprnt.c should be used if this
2800Sstevel@tonic-gate * assumption does not hold for a particular
2810Sstevel@tonic-gate * port.)
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate if (fpos) {
2840Sstevel@tonic-gate if (_mkarglst(sformat, sargs, arglst) != 0) {
2850Sstevel@tonic-gate return (EOF);
2860Sstevel@tonic-gate } else {
2870Sstevel@tonic-gate fpos = 0;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate if (len <= MAXARGS) {
2910Sstevel@tonic-gate args = arglst[len - 1];
2920Sstevel@tonic-gate } else {
2930Sstevel@tonic-gate args = arglst[MAXARGS - 1];
2940Sstevel@tonic-gate for (len -= MAXARGS; len > 0; len--)
2950Sstevel@tonic-gate (void) va_arg(args.ap, void *);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate len = 0;
2980Sstevel@tonic-gate ch = *fmt++;
2990Sstevel@tonic-gate goto charswitch;
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate if (len == 0)
3030Sstevel@tonic-gate len = MAXINT;
3040Sstevel@tonic-gate #ifdef _WIDE
3050Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
306*7074Srobbin (size == 'j') || (size == 't') || (size == 'z'))
3070Sstevel@tonic-gate ch = *fmt++;
3080Sstevel@tonic-gate #else /* _WIDE */
3090Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') ||
310*7074Srobbin (size == 'w') || (size == 'j') || (size == 't') ||
311*7074Srobbin (size == 'z'))
3120Sstevel@tonic-gate ch = *fmt++;
3130Sstevel@tonic-gate #endif /* _WIDE */
3140Sstevel@tonic-gate if (size == 'l' && ch == 'l') {
3150Sstevel@tonic-gate size = 'm'; /* size = 'm' if long long */
3160Sstevel@tonic-gate ch = *fmt++;
3170Sstevel@tonic-gate } else if (size == 'h' && ch == 'h') {
3180Sstevel@tonic-gate size = 'b'; /* use size = 'b' if char */
3190Sstevel@tonic-gate ch = *fmt++;
3200Sstevel@tonic-gate } else if ((size == 't') || (size == 'z')) {
3210Sstevel@tonic-gate size = 'l';
3220Sstevel@tonic-gate } else if (size == 'j') {
3230Sstevel@tonic-gate #ifndef _LP64
3240Sstevel@tonic-gate /* check scflag for size of u/intmax_t (32-bit libc) */
325*7074Srobbin if (!(scflag & _F_INTMAX32)) {
3260Sstevel@tonic-gate #endif
3270Sstevel@tonic-gate size = 'm';
3280Sstevel@tonic-gate #ifndef _LP64
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate #endif
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate if (ch == '\0') {
3330Sstevel@tonic-gate return (EOF); /* unexpected end of format */
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate #ifdef _WIDE
3360Sstevel@tonic-gate if (ch == '[') {
3370Sstevel@tonic-gate wchar_t c;
3380Sstevel@tonic-gate size_t len;
3390Sstevel@tonic-gate int negflg = 0;
3400Sstevel@tonic-gate wchar_t *p;
3410Sstevel@tonic-gate wchar_t *wbracket_str;
3420Sstevel@tonic-gate size_t wlen, clen;
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate /* p points to the address of '[' */
3450Sstevel@tonic-gate p = (wchar_t *)fmt - 1;
3460Sstevel@tonic-gate len = 0;
3470Sstevel@tonic-gate if (*fmt == '^') {
3480Sstevel@tonic-gate len++;
3490Sstevel@tonic-gate fmt++;
3500Sstevel@tonic-gate negflg = 1;
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) {
3530Sstevel@tonic-gate len++;
3540Sstevel@tonic-gate fmt++;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate while ((c = *fmt) != ']') {
3570Sstevel@tonic-gate if (c == '\0') {
3580Sstevel@tonic-gate return (EOF); /* unexpected EOF */
3590Sstevel@tonic-gate } else {
3600Sstevel@tonic-gate len++;
3610Sstevel@tonic-gate fmt++;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate fmt++;
3650Sstevel@tonic-gate len += 2;
3660Sstevel@tonic-gate wbracket_str = (wchar_t *)
367*7074Srobbin malloc(sizeof (wchar_t) * (len + 1));
3680Sstevel@tonic-gate if (wbracket_str == NULL) {
3690Sstevel@tonic-gate errno = ENOMEM;
3700Sstevel@tonic-gate return (EOF);
3710Sstevel@tonic-gate } else {
3720Sstevel@tonic-gate (void) wmemcpy(wbracket_str,
373*7074Srobbin (const wchar_t *)p, len);
3740Sstevel@tonic-gate *(wbracket_str + len) = L'\0';
3750Sstevel@tonic-gate if (negflg && *(wbracket_str + 1) == '^') {
3760Sstevel@tonic-gate *(wbracket_str + 1) = L'!';
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate wlen = wcslen(wbracket_str);
3800Sstevel@tonic-gate clen = wcstombs((char *)NULL, wbracket_str, 0);
3810Sstevel@tonic-gate if (clen == (size_t)-1) {
3820Sstevel@tonic-gate free(wbracket_str);
3830Sstevel@tonic-gate return (EOF);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate bracket_str = (unsigned char *)
386*7074Srobbin malloc(sizeof (unsigned char) * (clen + 1));
3870Sstevel@tonic-gate if (bracket_str == NULL) {
3880Sstevel@tonic-gate free(wbracket_str);
3890Sstevel@tonic-gate errno = ENOMEM;
3900Sstevel@tonic-gate return (EOF);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate clen = wcstombs((char *)bracket_str, wbracket_str,
393*7074Srobbin wlen + 1);
3940Sstevel@tonic-gate free(wbracket_str);
3950Sstevel@tonic-gate if (clen == (size_t)-1) {
3960Sstevel@tonic-gate free(bracket_str);
3970Sstevel@tonic-gate return (EOF);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate #else /* _WIDE */
4010Sstevel@tonic-gate if (ch == '[') {
4020Sstevel@tonic-gate if (size == 'l') {
4030Sstevel@tonic-gate int c, len, i;
4040Sstevel@tonic-gate int negflg = 0;
4050Sstevel@tonic-gate unsigned char *p;
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate p = (unsigned char *)(fmt - 1);
4080Sstevel@tonic-gate len = 0;
4090Sstevel@tonic-gate if (*fmt == '^') {
4100Sstevel@tonic-gate len++;
4110Sstevel@tonic-gate fmt++;
4120Sstevel@tonic-gate negflg = 1;
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) {
4150Sstevel@tonic-gate len++;
4160Sstevel@tonic-gate fmt++;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate while ((c = *fmt) != ']') {
4190Sstevel@tonic-gate if (c == '\0') {
4200Sstevel@tonic-gate return (EOF);
4210Sstevel@tonic-gate } else if (isascii(c)) {
4220Sstevel@tonic-gate len++;
4230Sstevel@tonic-gate fmt++;
4240Sstevel@tonic-gate } else {
4250Sstevel@tonic-gate i = mblen((const char *)fmt,
426*7074Srobbin MB_CUR_MAX);
4270Sstevel@tonic-gate if (i <= 0) {
4280Sstevel@tonic-gate return (EOF);
4290Sstevel@tonic-gate } else {
4300Sstevel@tonic-gate len += i;
4310Sstevel@tonic-gate fmt += i;
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate fmt++;
4360Sstevel@tonic-gate len += 2;
4370Sstevel@tonic-gate bracket_str = (unsigned char *)
438*7074Srobbin malloc(sizeof (unsigned char) * (len + 1));
4390Sstevel@tonic-gate if (bracket_str == NULL) {
4400Sstevel@tonic-gate errno = ENOMEM;
4410Sstevel@tonic-gate return (EOF);
4420Sstevel@tonic-gate } else {
4430Sstevel@tonic-gate (void) strncpy((char *)bracket_str,
444*7074Srobbin (const char *)p, len);
4450Sstevel@tonic-gate *(bracket_str + len) = '\0';
4460Sstevel@tonic-gate if (negflg &&
4470Sstevel@tonic-gate *(bracket_str + 1) == '^') {
4480Sstevel@tonic-gate *(bracket_str + 1) = '!';
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate } else {
4520Sstevel@tonic-gate int t = 0;
4530Sstevel@tonic-gate int b, c, d;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate if (*fmt == '^') {
4560Sstevel@tonic-gate t++;
4570Sstevel@tonic-gate fmt++;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate (void) memset(tab, !t, NCHARS);
4600Sstevel@tonic-gate if ((c = *fmt) == ']' || c == '-') {
4610Sstevel@tonic-gate tab[c] = t;
4620Sstevel@tonic-gate fmt++;
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate while ((c = *fmt) != ']') {
4660Sstevel@tonic-gate if (c == '\0') {
4670Sstevel@tonic-gate return (EOF);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate b = *(fmt - 1);
4700Sstevel@tonic-gate d = *(fmt + 1);
4710Sstevel@tonic-gate if ((c == '-') && (d != ']') &&
472*7074Srobbin (b < d)) {
4730Sstevel@tonic-gate (void) memset(&tab[b], t,
474*7074Srobbin d - b + 1);
4750Sstevel@tonic-gate fmt += 2;
4760Sstevel@tonic-gate } else {
4770Sstevel@tonic-gate tab[c] = t;
4780Sstevel@tonic-gate fmt++;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate fmt++;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate #endif /* _WIDE */
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate #ifdef _WIDE
4870Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) &&
488*7074Srobbin isupper((int)ch)) { /* no longer documented */
4890Sstevel@tonic-gate if (_lib_version == c_issue_4) {
4900Sstevel@tonic-gate if (size != 'm' && size != 'L')
4910Sstevel@tonic-gate size = 'l';
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate ch = _tolower((int)ch);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate if (ch != 'n' && !flag_eof) {
4960Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') {
4970Sstevel@tonic-gate while (iswspace(inchar =
498*7074Srobbin _wd_getwc(&chcount, iop)))
4990Sstevel@tonic-gate ;
5000Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) == WEOF)
5010Sstevel@tonic-gate break;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate #else /* _WIDE */
5060Sstevel@tonic-gate if (isupper(ch)) { /* no longer documented */
5070Sstevel@tonic-gate if (_lib_version == c_issue_4) {
5080Sstevel@tonic-gate if (size != 'm' && size != 'L')
5090Sstevel@tonic-gate size = 'l';
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate ch = _tolower(ch);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate if (ch != 'n' && !flag_eof) {
5140Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') {
5150Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount)))
5160Sstevel@tonic-gate ;
5170Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF)
5180Sstevel@tonic-gate break;
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate #endif /* _WIDE */
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate switch (ch) {
5240Sstevel@tonic-gate case 'C':
5250Sstevel@tonic-gate case 'S':
5260Sstevel@tonic-gate case 'c':
5270Sstevel@tonic-gate case 's':
5280Sstevel@tonic-gate #ifdef _WIDE
5290Sstevel@tonic-gate if ((size == 'l') || (size == 'C') || (size == 'S'))
5300Sstevel@tonic-gate #else /* _WIDE */
5310Sstevel@tonic-gate if ((size == 'w') || (size == 'l') || (size == 'C') ||
532*7074Srobbin (size == 'S'))
5330Sstevel@tonic-gate #endif /* _WIDE */
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate size = wstring(&chcount, &flag_eof, stow,
536*7074Srobbin (int)ch, len, iop, &args.ap);
5370Sstevel@tonic-gate } else {
5380Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow,
539*7074Srobbin (int)ch, len, tab, iop, &args.ap);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate break;
5420Sstevel@tonic-gate case '[':
5430Sstevel@tonic-gate if (size == 'l') {
5440Sstevel@tonic-gate size = wbrstring(&chcount, &flag_eof, stow,
545*7074Srobbin (int)ch, len, iop, bracket_str, &args.ap);
5460Sstevel@tonic-gate free(bracket_str);
5470Sstevel@tonic-gate bracket_str = NULL;
5480Sstevel@tonic-gate } else {
5490Sstevel@tonic-gate #ifdef _WIDE
5500Sstevel@tonic-gate size = brstring(&chcount, &flag_eof, stow,
551*7074Srobbin (int)ch, len, iop, bracket_str, &args.ap);
5520Sstevel@tonic-gate free(bracket_str);
5530Sstevel@tonic-gate bracket_str = NULL;
5540Sstevel@tonic-gate #else /* _WIDE */
5550Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow,
556*7074Srobbin ch, len, tab, iop, &args.ap);
5570Sstevel@tonic-gate #endif /* _WIDE */
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate break;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate case 'n':
5620Sstevel@tonic-gate if (stow == 0)
5630Sstevel@tonic-gate continue;
5640Sstevel@tonic-gate if (size == 'b') /* char */
5650Sstevel@tonic-gate *va_arg(args.ap, char *) = (char)chcount;
5660Sstevel@tonic-gate else if (size == 'h')
5670Sstevel@tonic-gate *va_arg(args.ap, short *) = (short)chcount;
5680Sstevel@tonic-gate else if (size == 'l')
5690Sstevel@tonic-gate *va_arg(args.ap, long *) = (long)chcount;
5700Sstevel@tonic-gate else if (size == 'm') /* long long */
5710Sstevel@tonic-gate *va_arg(args.ap, long long *) =
572*7074Srobbin (long long) chcount;
5730Sstevel@tonic-gate else
5740Sstevel@tonic-gate *va_arg(args.ap, int *) = (int)chcount;
5750Sstevel@tonic-gate continue;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate case 'i':
5780Sstevel@tonic-gate default:
5790Sstevel@tonic-gate size = number(&chcount, &flag_eof, stow, (int)ch,
580*7074Srobbin len, (int)size, iop, &args.ap);
5810Sstevel@tonic-gate break;
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate if (size)
5840Sstevel@tonic-gate nmatch += stow;
5850Sstevel@tonic-gate else {
5860Sstevel@tonic-gate return ((flag_eof && !nmatch) ? EOF : nmatch);
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate continue;
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate if (bracket_str)
5910Sstevel@tonic-gate free(bracket_str);
5920Sstevel@tonic-gate return (nmatch != 0 ? nmatch : EOF); /* end of input */
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate /* ****************************************************************** */
5960Sstevel@tonic-gate /* Functions to read the input stream in an attempt to match incoming */
5970Sstevel@tonic-gate /* data to the current pattern from the main loop of _doscan(). */
5980Sstevel@tonic-gate /* ****************************************************************** */
5990Sstevel@tonic-gate static int
number(int * chcount,int * flag_eof,int stow,int type,int len,int size,FILE * iop,va_list * listp)6000Sstevel@tonic-gate number(int *chcount, int *flag_eof, int stow, int type, int len, int size,
6010Sstevel@tonic-gate FILE *iop, va_list *listp)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate char numbuf[64];
6040Sstevel@tonic-gate char *np = numbuf;
6050Sstevel@tonic-gate int c, base, inchar, lookahead;
606*7074Srobbin int digitseen = 0, floater = 0, negflg = 0;
607*7074Srobbin int lc;
6080Sstevel@tonic-gate long long lcval = 0LL;
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate switch (type) {
6110Sstevel@tonic-gate case 'e':
6120Sstevel@tonic-gate case 'f':
6130Sstevel@tonic-gate case 'g':
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * lc = 0 corresponds to c90 mode: do not recognize
6160Sstevel@tonic-gate * hexadecimal fp strings; attempt to push back
6170Sstevel@tonic-gate * all unused characters read
6180Sstevel@tonic-gate *
6190Sstevel@tonic-gate * lc = -1 corresponds to c99 mode: recognize hexa-
6200Sstevel@tonic-gate * decimal fp strings; push back at most one
6210Sstevel@tonic-gate * unused character
6220Sstevel@tonic-gate */
6230Sstevel@tonic-gate lc = (__xpg6 & _C99SUSv3_recognize_hexfp)? -1 : 0;
6240Sstevel@tonic-gate floater = 1;
6250Sstevel@tonic-gate break;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate case 'a':
6280Sstevel@tonic-gate lc = -1;
6290Sstevel@tonic-gate floater = 1;
6300Sstevel@tonic-gate break;
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate case 'd':
6330Sstevel@tonic-gate case 'u':
6340Sstevel@tonic-gate case 'i':
6350Sstevel@tonic-gate base = 10;
6360Sstevel@tonic-gate break;
6370Sstevel@tonic-gate case 'o':
6380Sstevel@tonic-gate base = 8;
6390Sstevel@tonic-gate break;
6400Sstevel@tonic-gate case 'p':
6410Sstevel@tonic-gate #ifdef _LP64
6420Sstevel@tonic-gate size = 'l'; /* pointers are long in LP64 */
6430Sstevel@tonic-gate #endif /* _LP64 */
6440Sstevel@tonic-gate /* FALLTHROUGH */
6450Sstevel@tonic-gate case 'x':
6460Sstevel@tonic-gate base = 16;
6470Sstevel@tonic-gate break;
6480Sstevel@tonic-gate default:
6490Sstevel@tonic-gate return (0); /* unrecognized conversion character */
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate if (floater != 0) {
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate * Handle floating point with
6550Sstevel@tonic-gate * file_to_decimal.
6560Sstevel@tonic-gate */
6570Sstevel@tonic-gate decimal_mode dm;
6580Sstevel@tonic-gate decimal_record dr;
6590Sstevel@tonic-gate fp_exception_field_type efs;
6600Sstevel@tonic-gate enum decimal_string_form form;
6610Sstevel@tonic-gate char *echar;
6620Sstevel@tonic-gate int nread;
6630Sstevel@tonic-gate char buffer[1024+1];
6640Sstevel@tonic-gate char *nb = buffer;
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate if (len > 1024)
6670Sstevel@tonic-gate len = 1024;
6680Sstevel@tonic-gate file_to_decimal(&nb, len, lc, &dr, &form, &echar, iop, &nread);
6690Sstevel@tonic-gate if (lc == -1) {
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate * In C99 mode, the entire string read has to be
6720Sstevel@tonic-gate * accepted in order to qualify as a match
6730Sstevel@tonic-gate */
6740Sstevel@tonic-gate if (nb != buffer + nread)
6750Sstevel@tonic-gate form = invalid_form;
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate if (stow && (form != invalid_form)) {
6780Sstevel@tonic-gate #if defined(__sparc)
6790Sstevel@tonic-gate dm.rd = _QgetRD();
6800Sstevel@tonic-gate if (size == 'L') { /* long double */
6810Sstevel@tonic-gate if ((int)form < 0)
6820Sstevel@tonic-gate __hex_to_quadruple(&dr, dm.rd,
683*7074Srobbin va_arg(*listp, quadruple *), &efs);
6840Sstevel@tonic-gate else
6850Sstevel@tonic-gate decimal_to_quadruple(
6860Sstevel@tonic-gate va_arg(*listp, quadruple *),
6870Sstevel@tonic-gate &dm, &dr, &efs);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
6900Sstevel@tonic-gate dm.rd = __xgetRD();
6910Sstevel@tonic-gate if (size == 'L') { /* long double */
6920Sstevel@tonic-gate if ((int)form < 0)
6930Sstevel@tonic-gate __hex_to_extended(&dr, dm.rd,
694*7074Srobbin va_arg(*listp, extended *), &efs);
6950Sstevel@tonic-gate else
6960Sstevel@tonic-gate decimal_to_extended(
6970Sstevel@tonic-gate va_arg(*listp, extended *),
6980Sstevel@tonic-gate &dm, &dr, &efs);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate #else
7010Sstevel@tonic-gate #error Unknown architecture
7020Sstevel@tonic-gate #endif
7030Sstevel@tonic-gate else if (size == 'l') { /* double */
7040Sstevel@tonic-gate if ((int)form < 0)
7050Sstevel@tonic-gate __hex_to_double(&dr, dm.rd,
7060Sstevel@tonic-gate va_arg(*listp, double *), &efs);
7070Sstevel@tonic-gate else
7080Sstevel@tonic-gate decimal_to_double(
7090Sstevel@tonic-gate va_arg(*listp, double *),
7100Sstevel@tonic-gate &dm, &dr, &efs);
7110Sstevel@tonic-gate } else { /* float */
7120Sstevel@tonic-gate if ((int)form < 0)
7130Sstevel@tonic-gate __hex_to_single(&dr, dm.rd,
7140Sstevel@tonic-gate va_arg(*listp, single *), &efs);
7150Sstevel@tonic-gate else
7160Sstevel@tonic-gate decimal_to_single((single *)
7170Sstevel@tonic-gate va_arg(*listp, single *),
7180Sstevel@tonic-gate &dm, &dr, &efs);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate if ((efs & (1 << fp_overflow)) != 0) {
7210Sstevel@tonic-gate errno = ERANGE;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate if ((efs & (1 << fp_underflow)) != 0) {
7240Sstevel@tonic-gate errno = ERANGE;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate (*chcount) += nread; /* Count characters read. */
7280Sstevel@tonic-gate c = locgetc((*chcount));
7290Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF)
7300Sstevel@tonic-gate *flag_eof = 1;
7310Sstevel@tonic-gate return ((form == invalid_form) ? 0 : 1);
7320Sstevel@tonic-gate /* successful match if non-zero */
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate switch (c = locgetc((*chcount))) {
7360Sstevel@tonic-gate case '-':
7370Sstevel@tonic-gate negflg++;
7380Sstevel@tonic-gate /* FALLTHROUGH */
7390Sstevel@tonic-gate case '+':
7400Sstevel@tonic-gate if (--len <= 0)
7410Sstevel@tonic-gate break;
7420Sstevel@tonic-gate if ((c = locgetc((*chcount))) != '0')
7430Sstevel@tonic-gate break;
7440Sstevel@tonic-gate /* FALLTHROUGH */
7450Sstevel@tonic-gate case '0':
7460Sstevel@tonic-gate /*
7470Sstevel@tonic-gate * If %i or %x, the characters 0x or 0X may optionally precede
7480Sstevel@tonic-gate * the sequence of letters and digits (base 16).
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate if ((type != 'i' && type != 'x') || (len <= 1))
7510Sstevel@tonic-gate break;
7520Sstevel@tonic-gate if (((inchar = locgetc((*chcount))) == 'x') ||
7536812Sraf (inchar == 'X')) {
7540Sstevel@tonic-gate lookahead = readchar(iop, chcount);
7550Sstevel@tonic-gate if (isxdigit(lookahead)) {
7560Sstevel@tonic-gate base = 16;
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate if (len <= 2) {
7590Sstevel@tonic-gate (void) locungetc((*chcount), lookahead);
7600Sstevel@tonic-gate /* Take into account the 'x' */
7610Sstevel@tonic-gate len -= 1;
7620Sstevel@tonic-gate } else {
7630Sstevel@tonic-gate c = lookahead;
7640Sstevel@tonic-gate /* Take into account '0x' */
7650Sstevel@tonic-gate len -= 2;
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate } else {
7680Sstevel@tonic-gate (void) locungetc((*chcount), lookahead);
7690Sstevel@tonic-gate (void) locungetc((*chcount), inchar);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate } else {
7720Sstevel@tonic-gate /* inchar wans't 'x'. */
7730Sstevel@tonic-gate (void) locungetc((*chcount), inchar); /* Put it back. */
7740Sstevel@tonic-gate if (type == 'i') /* Only %i accepts an octal. */
7750Sstevel@tonic-gate base = 8;
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate for (; --len >= 0; *np++ = (char)c, c = locgetc((*chcount))) {
7790Sstevel@tonic-gate if (np > numbuf + 62) {
7806812Sraf errno = ERANGE;
7816812Sraf return (0);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate if (isdigit(c) || base == 16 && isxdigit(c)) {
7840Sstevel@tonic-gate int digit = c - (isdigit(c) ? '0' :
7856812Sraf isupper(c) ? 'A' - 10 : 'a' - 10);
7860Sstevel@tonic-gate if (digit >= base)
7870Sstevel@tonic-gate break;
7880Sstevel@tonic-gate if (stow)
7890Sstevel@tonic-gate lcval = base * lcval + digit;
7900Sstevel@tonic-gate digitseen++;
7910Sstevel@tonic-gate continue;
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate break;
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate if (stow && digitseen) {
7970Sstevel@tonic-gate /* suppress possible overflow on 2's-comp negation */
7980Sstevel@tonic-gate if (negflg && lcval != (1ULL << 63))
7990Sstevel@tonic-gate lcval = -lcval;
8000Sstevel@tonic-gate switch (size) {
8010Sstevel@tonic-gate case 'm':
8020Sstevel@tonic-gate *va_arg(*listp, long long *) = lcval;
8030Sstevel@tonic-gate break;
8040Sstevel@tonic-gate case 'l':
8050Sstevel@tonic-gate *va_arg(*listp, long *) = (long)lcval;
8060Sstevel@tonic-gate break;
8070Sstevel@tonic-gate case 'h':
8080Sstevel@tonic-gate *va_arg(*listp, short *) = (short)lcval;
8090Sstevel@tonic-gate break;
8100Sstevel@tonic-gate case 'b':
8110Sstevel@tonic-gate *va_arg(*listp, char *) = (char)lcval;
8120Sstevel@tonic-gate break;
8130Sstevel@tonic-gate default:
8140Sstevel@tonic-gate *va_arg(*listp, int *) = (int)lcval;
8150Sstevel@tonic-gate break;
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF)
8196812Sraf *flag_eof = 1;
8200Sstevel@tonic-gate return (digitseen); /* successful match if non-zero */
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate /* Get a character. If not using sscanf and at the buffer's end */
8240Sstevel@tonic-gate /* then do a direct read(). Characters read via readchar() */
8250Sstevel@tonic-gate /* can be pushed back on the input stream by locungetc((*chcount),) */
8260Sstevel@tonic-gate /* since there is padding allocated at the end of the stream buffer. */
8270Sstevel@tonic-gate static int
readchar(FILE * iop,int * chcount)8280Sstevel@tonic-gate readchar(FILE *iop, int *chcount)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate int inchar;
8310Sstevel@tonic-gate char buf[1];
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate if ((iop->_flag & _IOWRT) || (iop->_cnt != 0))
8340Sstevel@tonic-gate inchar = locgetc((*chcount));
8350Sstevel@tonic-gate else {
8360Sstevel@tonic-gate if (read(FILENO(iop), buf, 1) != 1)
8370Sstevel@tonic-gate return (EOF);
8380Sstevel@tonic-gate inchar = (int)buf[0];
8390Sstevel@tonic-gate (*chcount) += 1;
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate return (inchar);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate static int
string(int * chcount,int * flag_eof,int stow,int type,int len,char * tab,FILE * iop,va_list * listp)8450Sstevel@tonic-gate string(int *chcount, int *flag_eof, int stow, int type, int len, char *tab,
8460Sstevel@tonic-gate FILE *iop, va_list *listp)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate int ch;
8490Sstevel@tonic-gate char *ptr;
8500Sstevel@tonic-gate char *start;
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, char *) : NULL;
8530Sstevel@tonic-gate if (((type == 'c') || (type == 'C')) && len == MAXINT)
8540Sstevel@tonic-gate len = 1;
8550Sstevel@tonic-gate #ifdef _WIDE
8560Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF &&
8576812Sraf !(((type == 's') || (type == 'S')) && isspace(ch))) {
8580Sstevel@tonic-gate #else /* _WIDE */
8590Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF &&
8606812Sraf !(((type == 's') || (type == 'S')) &&
8616812Sraf isspace(ch) || type == '[' && tab[ch])) {
8620Sstevel@tonic-gate #endif /* _WIDE */
8630Sstevel@tonic-gate if (stow)
8640Sstevel@tonic-gate *ptr = (char)ch;
8650Sstevel@tonic-gate ptr++;
8660Sstevel@tonic-gate if (--len <= 0)
8670Sstevel@tonic-gate break;
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate if (ch == EOF) {
8700Sstevel@tonic-gate (*flag_eof) = 1;
8710Sstevel@tonic-gate (*chcount) -= 1;
8720Sstevel@tonic-gate } else if (len > 0 && locungetc((*chcount), ch) == EOF)
8730Sstevel@tonic-gate (*flag_eof) = 1;
8740Sstevel@tonic-gate if (ptr == start)
8750Sstevel@tonic-gate return (0); /* no match */
8760Sstevel@tonic-gate if (stow && ((type != 'c') && (type != 'C')))
8770Sstevel@tonic-gate *ptr = '\0';
8780Sstevel@tonic-gate return (1); /* successful match */
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate
8810Sstevel@tonic-gate /* This function initializes arglst, to contain the appropriate */
8820Sstevel@tonic-gate /* va_list values for the first MAXARGS arguments. */
8830Sstevel@tonic-gate /* WARNING: this code assumes that the sizes of all pointer types */
8840Sstevel@tonic-gate /* are the same. (Code similar to that in the portable doprnt.c */
8850Sstevel@tonic-gate /* should be used if this assumption is not true for a */
8860Sstevel@tonic-gate /* particular port.) */
8870Sstevel@tonic-gate
8880Sstevel@tonic-gate #ifdef _WIDE
8890Sstevel@tonic-gate static int
8900Sstevel@tonic-gate _mkarglst(const wchar_t *fmt, stva_list args, stva_list arglst[])
8910Sstevel@tonic-gate #else /* _WIDE */
8920Sstevel@tonic-gate static int
8930Sstevel@tonic-gate _mkarglst(const char *fmt, stva_list args, stva_list arglst[])
8940Sstevel@tonic-gate #endif /* _WIDE */
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate #ifdef _WIDE
8970Sstevel@tonic-gate #define STRCHR wcschr
8980Sstevel@tonic-gate #define STRSPN wcsspn
8990Sstevel@tonic-gate #define ATOI(x) _watoi((wchar_t *)x)
9000Sstevel@tonic-gate #define SPNSTR1 L"01234567890"
9010Sstevel@tonic-gate #define SPNSTR2 L"# +-.0123456789hL$"
9020Sstevel@tonic-gate #else /* _WIDE */
9030Sstevel@tonic-gate #define STRCHR strchr
9040Sstevel@tonic-gate #define STRSPN strspn
9050Sstevel@tonic-gate #define ATOI(x) atoi(x)
9060Sstevel@tonic-gate #define SPNSTR1 "01234567890"
9070Sstevel@tonic-gate #define SPNSTR2 "# +-.0123456789hL$"
9080Sstevel@tonic-gate #endif /* _WIDE */
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate int maxnum, curargno;
9110Sstevel@tonic-gate size_t n;
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate maxnum = -1;
9140Sstevel@tonic-gate curargno = 0;
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate while ((fmt = STRCHR(fmt, '%')) != NULL) {
9170Sstevel@tonic-gate fmt++; /* skip % */
9180Sstevel@tonic-gate if (*fmt == '*' || *fmt == '%')
9190Sstevel@tonic-gate continue;
9200Sstevel@tonic-gate if (fmt[n = STRSPN(fmt, SPNSTR1)] == L'$') {
9210Sstevel@tonic-gate /* convert to zero base */
9220Sstevel@tonic-gate curargno = ATOI(fmt) - 1;
9230Sstevel@tonic-gate fmt += n + 1;
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate if (maxnum < curargno)
9270Sstevel@tonic-gate maxnum = curargno;
9280Sstevel@tonic-gate curargno++; /* default to next in list */
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate fmt += STRSPN(fmt, SPNSTR2);
9310Sstevel@tonic-gate if (*fmt == '[') {
932*7074Srobbin int i;
9330Sstevel@tonic-gate fmt++; /* has to be at least on item in scan list */
9340Sstevel@tonic-gate if (*fmt == ']') {
9350Sstevel@tonic-gate fmt++;
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate while (*fmt != ']') {
9380Sstevel@tonic-gate if (*fmt == L'\0') {
9390Sstevel@tonic-gate return (-1); /* bad format */
9400Sstevel@tonic-gate #ifdef _WIDE
9410Sstevel@tonic-gate } else {
9420Sstevel@tonic-gate fmt++;
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate #else /* _WIDE */
9450Sstevel@tonic-gate } else if (isascii(*fmt)) {
9460Sstevel@tonic-gate fmt++;
9470Sstevel@tonic-gate } else {
9480Sstevel@tonic-gate i = mblen((const char *)
949*7074Srobbin fmt, MB_CUR_MAX);
9500Sstevel@tonic-gate if (i <= 0) {
9510Sstevel@tonic-gate return (-1);
9520Sstevel@tonic-gate } else {
9530Sstevel@tonic-gate fmt += i;
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate #endif /* _WIDE */
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate if (maxnum > MAXARGS)
9610Sstevel@tonic-gate maxnum = MAXARGS;
9620Sstevel@tonic-gate for (n = 0; n <= maxnum; n++) {
9630Sstevel@tonic-gate arglst[n] = args;
9640Sstevel@tonic-gate (void) va_arg(args.ap, void *);
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate return (0);
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate * For wide character handling
9720Sstevel@tonic-gate */
973*7074Srobbin
9740Sstevel@tonic-gate #ifdef _WIDE
9750Sstevel@tonic-gate static int
wstring(int * chcount,int * flag_eof,int stow,int type,int len,FILE * iop,va_list * listp)9760Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type,
9770Sstevel@tonic-gate int len, FILE *iop, va_list *listp)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate wint_t wch;
9800Sstevel@tonic-gate wchar_t *ptr;
9810Sstevel@tonic-gate wchar_t *wstart;
9820Sstevel@tonic-gate int dummy;
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate if ((type == 'c') && len == MAXINT)
9870Sstevel@tonic-gate len = 1;
9880Sstevel@tonic-gate while (((wch = _wd_getwc(chcount, iop)) != WEOF) &&
9896812Sraf !(type == 's' && iswspace(wch))) {
9900Sstevel@tonic-gate if (stow)
9910Sstevel@tonic-gate *ptr = wch;
9920Sstevel@tonic-gate ptr++;
9930Sstevel@tonic-gate if (--len <= 0)
9940Sstevel@tonic-gate break;
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate if (wch == WEOF) {
9970Sstevel@tonic-gate *flag_eof = 1;
9980Sstevel@tonic-gate (*chcount) -= 1;
9990Sstevel@tonic-gate } else {
10000Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(chcount, wch, iop) == WEOF)
10010Sstevel@tonic-gate *flag_eof = 1;
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate if (ptr == wstart)
10040Sstevel@tonic-gate return (0); /* no match */
10050Sstevel@tonic-gate if (stow && (type != 'c'))
10060Sstevel@tonic-gate *ptr = '\0';
10070Sstevel@tonic-gate return (1); /* successful match */
10080Sstevel@tonic-gate }
1009*7074Srobbin
10100Sstevel@tonic-gate #else /* _WIDE */
10110Sstevel@tonic-gate static int
wstring(int * chcount,int * flag_eof,int stow,int type,int len,FILE * iop,va_list * listp)10120Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type, int len, FILE *iop,
10130Sstevel@tonic-gate va_list *listp)
10140Sstevel@tonic-gate {
10150Sstevel@tonic-gate int wch;
10160Sstevel@tonic-gate wchar_t *ptr;
10170Sstevel@tonic-gate wchar_t *wstart;
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate if ((type == 'c') && len == MAXINT)
10220Sstevel@tonic-gate len = 1;
10230Sstevel@tonic-gate while (((wch = _bi_getwc(iop)) != EOF) &&
10246812Sraf !(type == 's' && (isascii(wch) ? isspace(wch) : 0))) {
10250Sstevel@tonic-gate (*chcount) += _scrwidth((wchar_t)wch);
10260Sstevel@tonic-gate if (stow)
10270Sstevel@tonic-gate *ptr = wch;
10280Sstevel@tonic-gate ptr++;
10290Sstevel@tonic-gate if (--len <= 0)
10300Sstevel@tonic-gate break;
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate if (wch == EOF) {
10330Sstevel@tonic-gate (*flag_eof) = 1;
10340Sstevel@tonic-gate (*chcount) -= 1;
10350Sstevel@tonic-gate } else {
10360Sstevel@tonic-gate if (len > 0 && _bi_ungetwc(wch, iop) == EOF)
10370Sstevel@tonic-gate (*flag_eof) = 1;
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate if (ptr == wstart)
10400Sstevel@tonic-gate return (0); /* no match */
10410Sstevel@tonic-gate if (stow && (type != 'c'))
10420Sstevel@tonic-gate *ptr = '\0';
10430Sstevel@tonic-gate return (1); /* successful match */
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate #endif /* _WIDE */
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate #ifdef _WIDE
10480Sstevel@tonic-gate static wint_t
_wd_getwc(int * chcount,FILE * iop)10490Sstevel@tonic-gate _wd_getwc(int *chcount, FILE *iop)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate wint_t wc;
10520Sstevel@tonic-gate int len;
10530Sstevel@tonic-gate
10540Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
10550Sstevel@tonic-gate /* call from fwscanf, wscanf */
10560Sstevel@tonic-gate wc = __fgetwc_xpg5(iop);
10570Sstevel@tonic-gate (*chcount)++;
10580Sstevel@tonic-gate return (wc);
10590Sstevel@tonic-gate } else {
10600Sstevel@tonic-gate /* call from swscanf */
10610Sstevel@tonic-gate if (*iop->_ptr == '\0')
10620Sstevel@tonic-gate return (WEOF);
10630Sstevel@tonic-gate len = mbtowc((wchar_t *)&wc, (const char *)iop->_ptr,
10646812Sraf MB_CUR_MAX);
10650Sstevel@tonic-gate if (len == -1)
10660Sstevel@tonic-gate return (WEOF);
10670Sstevel@tonic-gate iop->_ptr += len;
10680Sstevel@tonic-gate (*chcount)++;
10690Sstevel@tonic-gate return (wc);
10700Sstevel@tonic-gate }
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate
10730Sstevel@tonic-gate static wint_t
_wd_ungetwc(int * chcount,wchar_t wc,FILE * iop)10740Sstevel@tonic-gate _wd_ungetwc(int *chcount, wchar_t wc, FILE *iop)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate wint_t ret;
10770Sstevel@tonic-gate int len;
10780Sstevel@tonic-gate char mbs[MB_LEN_MAX];
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate if (wc == WEOF)
10810Sstevel@tonic-gate return (WEOF);
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) {
10840Sstevel@tonic-gate /* call from fwscanf, wscanf */
10850Sstevel@tonic-gate ret = __ungetwc_xpg5((wint_t)wc, iop);
10860Sstevel@tonic-gate if (ret != (wint_t)wc)
10870Sstevel@tonic-gate return (WEOF);
10880Sstevel@tonic-gate (*chcount)--;
10890Sstevel@tonic-gate return (ret);
10900Sstevel@tonic-gate } else {
10910Sstevel@tonic-gate /* call from swscanf */
10920Sstevel@tonic-gate len = wctomb(mbs, wc);
10930Sstevel@tonic-gate if (len == -1)
10940Sstevel@tonic-gate return (WEOF);
10950Sstevel@tonic-gate iop->_ptr -= len;
10960Sstevel@tonic-gate (*chcount)--;
10970Sstevel@tonic-gate return ((wint_t)wc);
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate static int
_watoi(wchar_t * fmt)11020Sstevel@tonic-gate _watoi(wchar_t *fmt)
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate int n = 0;
11050Sstevel@tonic-gate wchar_t ch;
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate ch = *fmt;
11080Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) && isdigit((int)ch)) {
11090Sstevel@tonic-gate n = ch - '0';
11100Sstevel@tonic-gate while (((ch = *++fmt) >= 0) && (ch < 256) &&
11116812Sraf isdigit((int)ch)) {
11120Sstevel@tonic-gate n *= 10;
11130Sstevel@tonic-gate n += ch - '0';
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate return (n);
11170Sstevel@tonic-gate }
11180Sstevel@tonic-gate #endif /* _WIDE */
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate /* ARGSUSED3 */
11210Sstevel@tonic-gate static int
wbrstring(int * chcount,int * flag_eof,int stow,int type,int len,FILE * iop,unsigned char * brstr,va_list * listp)11220Sstevel@tonic-gate wbrstring(int *chcount, int *flag_eof, int stow, int type,
11230Sstevel@tonic-gate int len, FILE *iop, unsigned char *brstr, va_list *listp)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate wint_t wch;
11260Sstevel@tonic-gate int i;
11270Sstevel@tonic-gate char str[MB_LEN_MAX + 1]; /* include null termination */
11280Sstevel@tonic-gate wchar_t *ptr, *start;
11290Sstevel@tonic-gate #ifdef _WIDE
11300Sstevel@tonic-gate int dummy;
11310Sstevel@tonic-gate #endif /* _WIDE */
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, wchar_t *) : NULL;
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate #ifdef _WIDE
11360Sstevel@tonic-gate while ((wch = _wd_getwc(&dummy, iop)) != WEOF) {
11370Sstevel@tonic-gate #else /* _WIDE */
11380Sstevel@tonic-gate while ((wch = _bi_getwc(iop)) != WEOF) {
11390Sstevel@tonic-gate #endif /* _WIDE */
11400Sstevel@tonic-gate i = wctomb(str, (wchar_t)wch);
11410Sstevel@tonic-gate if (i == -1) {
11420Sstevel@tonic-gate return (0);
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate str[i] = '\0';
11450Sstevel@tonic-gate if (fnmatch((const char *)brstr, (const char *)str,
11466812Sraf FNM_NOESCAPE)) {
11470Sstevel@tonic-gate break;
11480Sstevel@tonic-gate } else {
11490Sstevel@tonic-gate if (len > 0) {
11500Sstevel@tonic-gate #ifdef _WIDE
11510Sstevel@tonic-gate (*chcount)++;
11520Sstevel@tonic-gate #else /* _WIDE */
11530Sstevel@tonic-gate (*chcount) += _scrwidth(wch);
11540Sstevel@tonic-gate #endif /* _WIDE */
11550Sstevel@tonic-gate len--;
11560Sstevel@tonic-gate if (stow) {
11570Sstevel@tonic-gate *ptr = wch;
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate ptr++;
11600Sstevel@tonic-gate if (len <= 0)
11610Sstevel@tonic-gate break;
11620Sstevel@tonic-gate } else {
11630Sstevel@tonic-gate break;
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate if (wch == WEOF) {
11680Sstevel@tonic-gate *flag_eof = 1;
11690Sstevel@tonic-gate } else {
11700Sstevel@tonic-gate #ifdef _WIDE
11710Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF)
11720Sstevel@tonic-gate #else /* _WIDE */
11730Sstevel@tonic-gate if (len > 0 && _bi_ungetwc(wch, iop) == WEOF)
11740Sstevel@tonic-gate #endif /* _WIDE */
11750Sstevel@tonic-gate *flag_eof = 1;
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate if (ptr == start)
11780Sstevel@tonic-gate return (0); /* no match */
11790Sstevel@tonic-gate if (stow)
11800Sstevel@tonic-gate *ptr = L'\0';
11810Sstevel@tonic-gate return (1); /* successful match */
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate #ifdef _WIDE
11850Sstevel@tonic-gate static int
11860Sstevel@tonic-gate brstring(int *chcount, int *flag_eof, int stow, int type,
11870Sstevel@tonic-gate int len, FILE *iop, unsigned char *brstr, va_list *listp)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate wint_t wch;
11900Sstevel@tonic-gate int i;
11910Sstevel@tonic-gate char str[MB_LEN_MAX + 1]; /* include null termination */
11920Sstevel@tonic-gate char *ptr, *start, *p;
11930Sstevel@tonic-gate int dummy;
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, char *) : NULL;
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate while ((wch = _wd_getwc(&dummy, iop)) != WEOF) {
11980Sstevel@tonic-gate p = str;
11990Sstevel@tonic-gate i = wctomb(str, (wchar_t)wch);
12000Sstevel@tonic-gate if (i == -1) {
12010Sstevel@tonic-gate return (0);
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate str[i] = '\0';
12040Sstevel@tonic-gate if (fnmatch((const char *)brstr, (const char *)str,
12056812Sraf FNM_NOESCAPE)) {
12060Sstevel@tonic-gate break;
12070Sstevel@tonic-gate } else {
12080Sstevel@tonic-gate if (len >= i) {
12090Sstevel@tonic-gate (*chcount)++;
12100Sstevel@tonic-gate len -= i;
12110Sstevel@tonic-gate if (stow) {
12120Sstevel@tonic-gate while (i-- > 0) {
12130Sstevel@tonic-gate *ptr++ = *p++;
12140Sstevel@tonic-gate }
12150Sstevel@tonic-gate } else {
12160Sstevel@tonic-gate while (i-- > 0) {
12170Sstevel@tonic-gate ptr++;
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate if (len <= 0)
12210Sstevel@tonic-gate break;
12220Sstevel@tonic-gate } else {
12230Sstevel@tonic-gate break;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate if (wch == WEOF) {
12280Sstevel@tonic-gate *flag_eof = 1;
12290Sstevel@tonic-gate } else {
12300Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF)
12310Sstevel@tonic-gate *flag_eof = 1;
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate if (ptr == start)
12340Sstevel@tonic-gate return (0); /* no match */
12350Sstevel@tonic-gate if (stow)
12360Sstevel@tonic-gate *ptr = '\0';
12370Sstevel@tonic-gate return (1); /* successful match */
12380Sstevel@tonic-gate }
12390Sstevel@tonic-gate #endif /* _WIDE */
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate * Locally define getwc and ungetwc
12430Sstevel@tonic-gate */
12440Sstevel@tonic-gate static int
12450Sstevel@tonic-gate _bi_getwc(FILE *iop)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate int c;
12480Sstevel@tonic-gate wchar_t intcode;
12490Sstevel@tonic-gate int i, nbytes, cur_max;
12500Sstevel@tonic-gate char buff[MB_LEN_MAX];
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate if ((c = wlocgetc()) == EOF)
12530Sstevel@tonic-gate return (WEOF);
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate if (isascii(c)) /* ASCII code */
12560Sstevel@tonic-gate return ((wint_t)c);
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate buff[0] = (char)c;
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate cur_max = (int)MB_CUR_MAX;
12610Sstevel@tonic-gate /* MB_CUR_MAX doen't exeed the value of MB_LEN_MAX */
12620Sstevel@tonic-gate /* So we use MB_CUR_MAX instead of MB_LEN_MAX for */
12630Sstevel@tonic-gate /* improving the performance. */
12640Sstevel@tonic-gate for (i = 1; i < cur_max; i++) {
12650Sstevel@tonic-gate c = wlocgetc();
12660Sstevel@tonic-gate if (c == '\n') {
12670Sstevel@tonic-gate (void) wlocungetc(c);
12680Sstevel@tonic-gate break;
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate if (c == EOF) {
12710Sstevel@tonic-gate /* this still may be a valid multibyte character */
12720Sstevel@tonic-gate break;
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate buff[i] = (char)c;
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate if ((nbytes = mbtowc(&intcode, buff, i)) == -1) {
12780Sstevel@tonic-gate /*
12790Sstevel@tonic-gate * If mbtowc fails, the input was not a legal character.
12800Sstevel@tonic-gate * ungetc all but one character.
12810Sstevel@tonic-gate *
12820Sstevel@tonic-gate * Note: the number of pushback characters that
12830Sstevel@tonic-gate * ungetc() can handle must be >= (MB_LEN_MAX - 1).
12840Sstevel@tonic-gate * In Solaris 2.x, the number of pushback
12850Sstevel@tonic-gate * characters is 4.
12860Sstevel@tonic-gate */
12870Sstevel@tonic-gate while (i-- > 1) {
12880Sstevel@tonic-gate (void) wlocungetc((signed char)buff[i]);
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate errno = EILSEQ;
12910Sstevel@tonic-gate return (WEOF); /* Illegal EUC sequence. */
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate while (i-- > nbytes) {
12950Sstevel@tonic-gate /*
12960Sstevel@tonic-gate * Note: the number of pushback characters that
12970Sstevel@tonic-gate * ungetc() can handle must be >= (MB_LEN_MAX - 1).
12980Sstevel@tonic-gate * In Solaris 2.x, the number of pushback
12990Sstevel@tonic-gate * characters is 4.
13000Sstevel@tonic-gate */
13010Sstevel@tonic-gate (void) wlocungetc((signed char)buff[i]);
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate return ((int)intcode);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate static int
13070Sstevel@tonic-gate _bi_ungetwc(wint_t wc, FILE *iop)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate char mbs[MB_LEN_MAX];
13100Sstevel@tonic-gate unsigned char *p;
13110Sstevel@tonic-gate int n;
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0))
13140Sstevel@tonic-gate return (WEOF);
13150Sstevel@tonic-gate
13160Sstevel@tonic-gate n = wctomb(mbs, (wchar_t)wc);
13170Sstevel@tonic-gate if (n <= 0)
13180Sstevel@tonic-gate return (WEOF);
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate if (iop->_ptr <= iop->_base) {
13210Sstevel@tonic-gate if (iop->_base == NULL) {
13220Sstevel@tonic-gate return (WEOF);
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate if ((iop->_ptr == iop->_base) && (iop->_cnt == 0)) {
13250Sstevel@tonic-gate ++iop->_ptr;
13260Sstevel@tonic-gate } else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) {
13270Sstevel@tonic-gate return (WEOF);
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate
13310Sstevel@tonic-gate p = (unsigned char *)(mbs+n-1); /* p points the last byte */
13320Sstevel@tonic-gate /* if _IOWRT is set to iop->_flag, it means this is */
13330Sstevel@tonic-gate /* an invocation from sscanf(), and in that time we */
13340Sstevel@tonic-gate /* don't touch iop->_cnt. Otherwise, which means an */
13350Sstevel@tonic-gate /* invocation from fscanf() or scanf(), we touch iop->_cnt */
13360Sstevel@tonic-gate if ((iop->_flag & _IOWRT) == 0) {
13370Sstevel@tonic-gate /* scanf() and fscanf() */
13380Sstevel@tonic-gate iop->_cnt += n;
13390Sstevel@tonic-gate while (n--) {
13400Sstevel@tonic-gate *--iop->_ptr = *(p--);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate } else {
13430Sstevel@tonic-gate /* sscanf() */
13440Sstevel@tonic-gate iop->_ptr -= n;
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate return (wc);
13470Sstevel@tonic-gate }
1348