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 5*6812Sraf * Common Development and Distribution License (the "License"). 6*6812Sraf * 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 */ 21*6812Sraf 22*6812Sraf /* 23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*6812Sraf * Use is subject to license terms. 25*6812Sraf */ 26*6812Sraf 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 32*6812Sraf #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 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 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 */ 1540Sstevel@tonic-gate stva_list args, /* used to step through the argument list */ 1550Sstevel@tonic-gate sargs; /* used to save the 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 = 1990Sstevel@tonic-gate _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++) == '%') { 2070Sstevel@tonic-gate if ((inchar = _wd_getwc(&chcount, iop)) == ch) 2080Sstevel@tonic-gate continue; 2090Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) != WEOF) { 2100Sstevel@tonic-gate return (nmatch); /* failed to match input */ 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate break; 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate #else /* _WIDE */ 2150Sstevel@tonic-gate if (isspace(ch)) { 2160Sstevel@tonic-gate if (!flag_eof) { 2170Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount))) 2180Sstevel@tonic-gate ; 2190Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF) 2200Sstevel@tonic-gate flag_eof = 1; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate continue; 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate if (ch != '%' || (ch = *fmt++) == '%') { 2260Sstevel@tonic-gate if ((inchar = locgetc(chcount)) == ch) 2270Sstevel@tonic-gate continue; 2280Sstevel@tonic-gate if (locungetc(chcount, inchar) != EOF) { 2290Sstevel@tonic-gate return (nmatch); /* failed to match input */ 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate break; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate #endif /* _WIDE */ 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate charswitch: /* target of a goto 8-( */ 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (ch == '*') { 2380Sstevel@tonic-gate stow = 0; 2390Sstevel@tonic-gate ch = *fmt++; 2400Sstevel@tonic-gate } else 2410Sstevel@tonic-gate stow = 1; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate #ifdef _WIDE 2440Sstevel@tonic-gate for (len = 0; ((ch >= 0) && (ch < 256) && isdigit(ch)); 2450Sstevel@tonic-gate ch = *fmt++) 2460Sstevel@tonic-gate len = len * 10 + ch - '0'; 2470Sstevel@tonic-gate #else /* _WIDE */ 2480Sstevel@tonic-gate for (len = 0; isdigit(ch); ch = *fmt++) 2490Sstevel@tonic-gate len = len * 10 + ch - '0'; 2500Sstevel@tonic-gate #endif /* _WIDE */ 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if (ch == '$') { 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * positional parameter handling - the number 2550Sstevel@tonic-gate * specified in len gives the argument to which 2560Sstevel@tonic-gate * the next conversion should be applied. 2570Sstevel@tonic-gate * WARNING: This implementation of positional 2580Sstevel@tonic-gate * parameters assumes that the sizes of all pointer 2590Sstevel@tonic-gate * types are the same. (Code similar to that 2600Sstevel@tonic-gate * in the portable doprnt.c should be used if this 2610Sstevel@tonic-gate * assumption does not hold for a particular 2620Sstevel@tonic-gate * port.) 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate if (fpos) { 2650Sstevel@tonic-gate if (_mkarglst(sformat, sargs, arglst) != 0) { 2660Sstevel@tonic-gate return (EOF); 2670Sstevel@tonic-gate } else { 2680Sstevel@tonic-gate fpos = 0; 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate } 2710Sstevel@tonic-gate if (len <= MAXARGS) { 2720Sstevel@tonic-gate args = arglst[len - 1]; 2730Sstevel@tonic-gate } else { 2740Sstevel@tonic-gate args = arglst[MAXARGS - 1]; 2750Sstevel@tonic-gate for (len -= MAXARGS; len > 0; len--) 2760Sstevel@tonic-gate (void) va_arg(args.ap, void *); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate len = 0; 2790Sstevel@tonic-gate ch = *fmt++; 2800Sstevel@tonic-gate goto charswitch; 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate if (len == 0) 2840Sstevel@tonic-gate len = MAXINT; 2850Sstevel@tonic-gate #ifdef _WIDE 2860Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') || 2870Sstevel@tonic-gate (size == 'j') || (size == 't') || (size == 'z')) 2880Sstevel@tonic-gate ch = *fmt++; 2890Sstevel@tonic-gate #else /* _WIDE */ 2900Sstevel@tonic-gate if ((size = ch) == 'l' || (size == 'h') || (size == 'L') || 2910Sstevel@tonic-gate (size == 'w') || (size == 'j') || (size == 't') || 2920Sstevel@tonic-gate (size == 'z')) 2930Sstevel@tonic-gate ch = *fmt++; 2940Sstevel@tonic-gate #endif /* _WIDE */ 2950Sstevel@tonic-gate if (size == 'l' && ch == 'l') { 2960Sstevel@tonic-gate size = 'm'; /* size = 'm' if long long */ 2970Sstevel@tonic-gate ch = *fmt++; 2980Sstevel@tonic-gate } else if (size == 'h' && ch == 'h') { 2990Sstevel@tonic-gate size = 'b'; /* use size = 'b' if char */ 3000Sstevel@tonic-gate ch = *fmt++; 3010Sstevel@tonic-gate } else if ((size == 't') || (size == 'z')) { 3020Sstevel@tonic-gate size = 'l'; 3030Sstevel@tonic-gate } else if (size == 'j') { 3040Sstevel@tonic-gate #ifndef _LP64 3050Sstevel@tonic-gate /* check scflag for size of u/intmax_t (32-bit libc) */ 3060Sstevel@tonic-gate if (!(scflag & _F_INTMAX32)) 3070Sstevel@tonic-gate { 3080Sstevel@tonic-gate #endif 3090Sstevel@tonic-gate size = 'm'; 3100Sstevel@tonic-gate #ifndef _LP64 3110Sstevel@tonic-gate } 3120Sstevel@tonic-gate #endif 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate if (ch == '\0') { 3150Sstevel@tonic-gate return (EOF); /* unexpected end of format */ 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate #ifdef _WIDE 3180Sstevel@tonic-gate if (ch == '[') { 3190Sstevel@tonic-gate wchar_t c; 3200Sstevel@tonic-gate size_t len; 3210Sstevel@tonic-gate int negflg = 0; 3220Sstevel@tonic-gate wchar_t *p; 3230Sstevel@tonic-gate wchar_t *wbracket_str; 3240Sstevel@tonic-gate size_t wlen, clen; 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate /* p points to the address of '[' */ 3270Sstevel@tonic-gate p = (wchar_t *)fmt - 1; 3280Sstevel@tonic-gate len = 0; 3290Sstevel@tonic-gate if (*fmt == '^') { 3300Sstevel@tonic-gate len++; 3310Sstevel@tonic-gate fmt++; 3320Sstevel@tonic-gate negflg = 1; 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) { 3350Sstevel@tonic-gate len++; 3360Sstevel@tonic-gate fmt++; 3370Sstevel@tonic-gate } 3380Sstevel@tonic-gate while ((c = *fmt) != ']') { 3390Sstevel@tonic-gate if (c == '\0') { 3400Sstevel@tonic-gate return (EOF); /* unexpected EOF */ 3410Sstevel@tonic-gate } else { 3420Sstevel@tonic-gate len++; 3430Sstevel@tonic-gate fmt++; 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate fmt++; 3470Sstevel@tonic-gate len += 2; 3480Sstevel@tonic-gate wbracket_str = (wchar_t *) 3490Sstevel@tonic-gate malloc(sizeof (wchar_t) * (len + 1)); 3500Sstevel@tonic-gate if (wbracket_str == NULL) { 3510Sstevel@tonic-gate errno = ENOMEM; 3520Sstevel@tonic-gate return (EOF); 3530Sstevel@tonic-gate } else { 3540Sstevel@tonic-gate (void) wmemcpy(wbracket_str, 3550Sstevel@tonic-gate (const wchar_t *)p, len); 3560Sstevel@tonic-gate *(wbracket_str + len) = L'\0'; 3570Sstevel@tonic-gate if (negflg && *(wbracket_str + 1) == '^') { 3580Sstevel@tonic-gate *(wbracket_str + 1) = L'!'; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate wlen = wcslen(wbracket_str); 3620Sstevel@tonic-gate clen = wcstombs((char *)NULL, wbracket_str, 0); 3630Sstevel@tonic-gate if (clen == (size_t)-1) { 3640Sstevel@tonic-gate free(wbracket_str); 3650Sstevel@tonic-gate return (EOF); 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate bracket_str = (unsigned char *) 3680Sstevel@tonic-gate malloc(sizeof (unsigned char) * (clen + 1)); 3690Sstevel@tonic-gate if (bracket_str == NULL) { 3700Sstevel@tonic-gate free(wbracket_str); 3710Sstevel@tonic-gate errno = ENOMEM; 3720Sstevel@tonic-gate return (EOF); 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate clen = wcstombs((char *)bracket_str, wbracket_str, 3750Sstevel@tonic-gate wlen + 1); 3760Sstevel@tonic-gate free(wbracket_str); 3770Sstevel@tonic-gate if (clen == (size_t)-1) { 3780Sstevel@tonic-gate free(bracket_str); 3790Sstevel@tonic-gate return (EOF); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate #else /* _WIDE */ 3830Sstevel@tonic-gate if (ch == '[') { 3840Sstevel@tonic-gate if (size == 'l') { 3850Sstevel@tonic-gate int c, len, i; 3860Sstevel@tonic-gate int negflg = 0; 3870Sstevel@tonic-gate unsigned char *p; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate p = (unsigned char *)(fmt - 1); 3900Sstevel@tonic-gate len = 0; 3910Sstevel@tonic-gate if (*fmt == '^') { 3920Sstevel@tonic-gate len++; 3930Sstevel@tonic-gate fmt++; 3940Sstevel@tonic-gate negflg = 1; 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate if (((c = *fmt) == ']') || (c == '-')) { 3970Sstevel@tonic-gate len++; 3980Sstevel@tonic-gate fmt++; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate while ((c = *fmt) != ']') { 4010Sstevel@tonic-gate if (c == '\0') { 4020Sstevel@tonic-gate return (EOF); 4030Sstevel@tonic-gate } else if (isascii(c)) { 4040Sstevel@tonic-gate len++; 4050Sstevel@tonic-gate fmt++; 4060Sstevel@tonic-gate } else { 4070Sstevel@tonic-gate i = mblen((const char *)fmt, 4080Sstevel@tonic-gate MB_CUR_MAX); 4090Sstevel@tonic-gate if (i <= 0) { 4100Sstevel@tonic-gate return (EOF); 4110Sstevel@tonic-gate } else { 4120Sstevel@tonic-gate len += i; 4130Sstevel@tonic-gate fmt += i; 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate } 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate fmt++; 4180Sstevel@tonic-gate len += 2; 4190Sstevel@tonic-gate bracket_str = (unsigned char *) 4200Sstevel@tonic-gate malloc(sizeof (unsigned char) * 4210Sstevel@tonic-gate (len + 1)); 4220Sstevel@tonic-gate if (bracket_str == NULL) { 4230Sstevel@tonic-gate errno = ENOMEM; 4240Sstevel@tonic-gate return (EOF); 4250Sstevel@tonic-gate } else { 4260Sstevel@tonic-gate (void) strncpy((char *)bracket_str, 4270Sstevel@tonic-gate (const char *)p, len); 4280Sstevel@tonic-gate *(bracket_str + len) = '\0'; 4290Sstevel@tonic-gate if (negflg && 4300Sstevel@tonic-gate *(bracket_str + 1) == '^') { 4310Sstevel@tonic-gate *(bracket_str + 1) = '!'; 4320Sstevel@tonic-gate } 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate } else { 4350Sstevel@tonic-gate int t = 0; 4360Sstevel@tonic-gate int b, c, d; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate if (*fmt == '^') { 4390Sstevel@tonic-gate t++; 4400Sstevel@tonic-gate fmt++; 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate (void) memset(tab, !t, NCHARS); 4430Sstevel@tonic-gate if ((c = *fmt) == ']' || c == '-') { 4440Sstevel@tonic-gate tab[c] = t; 4450Sstevel@tonic-gate fmt++; 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate while ((c = *fmt) != ']') { 4490Sstevel@tonic-gate if (c == '\0') { 4500Sstevel@tonic-gate return (EOF); 4510Sstevel@tonic-gate } 4520Sstevel@tonic-gate b = *(fmt - 1); 4530Sstevel@tonic-gate d = *(fmt + 1); 4540Sstevel@tonic-gate if ((c == '-') && (d != ']') && 4550Sstevel@tonic-gate (b < d)) { 4560Sstevel@tonic-gate (void) memset(&tab[b], t, 4570Sstevel@tonic-gate d - b + 1); 4580Sstevel@tonic-gate fmt += 2; 4590Sstevel@tonic-gate } else { 4600Sstevel@tonic-gate tab[c] = t; 4610Sstevel@tonic-gate fmt++; 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate fmt++; 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate #endif /* _WIDE */ 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate #ifdef _WIDE 4700Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) && 4710Sstevel@tonic-gate isupper((int)ch)) { /* no longer documented */ 4720Sstevel@tonic-gate if (_lib_version == c_issue_4) { 4730Sstevel@tonic-gate if (size != 'm' && size != 'L') 4740Sstevel@tonic-gate size = 'l'; 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate ch = _tolower((int)ch); 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate if (ch != 'n' && !flag_eof) { 4790Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') { 4800Sstevel@tonic-gate while (iswspace(inchar = 4810Sstevel@tonic-gate _wd_getwc(&chcount, iop))) 4820Sstevel@tonic-gate ; 4830Sstevel@tonic-gate if (_wd_ungetwc(&chcount, inchar, iop) == WEOF) 4840Sstevel@tonic-gate break; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate #else /* _WIDE */ 4890Sstevel@tonic-gate if (isupper(ch)) { /* no longer documented */ 4900Sstevel@tonic-gate if (_lib_version == c_issue_4) { 4910Sstevel@tonic-gate if (size != 'm' && size != 'L') 4920Sstevel@tonic-gate size = 'l'; 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate ch = _tolower(ch); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate if (ch != 'n' && !flag_eof) { 4970Sstevel@tonic-gate if (ch != 'c' && ch != 'C' && ch != '[') { 4980Sstevel@tonic-gate while (isspace(inchar = locgetc(chcount))) 4990Sstevel@tonic-gate ; 5000Sstevel@tonic-gate if (locungetc(chcount, inchar) == EOF) 5010Sstevel@tonic-gate break; 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate #endif /* _WIDE */ 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate switch (ch) { 5070Sstevel@tonic-gate case 'C': 5080Sstevel@tonic-gate case 'S': 5090Sstevel@tonic-gate case 'c': 5100Sstevel@tonic-gate case 's': 5110Sstevel@tonic-gate #ifdef _WIDE 5120Sstevel@tonic-gate if ((size == 'l') || (size == 'C') || (size == 'S')) 5130Sstevel@tonic-gate #else /* _WIDE */ 5140Sstevel@tonic-gate if ((size == 'w') || (size == 'l') || (size == 'C') || 5150Sstevel@tonic-gate (size == 'S')) 5160Sstevel@tonic-gate #endif /* _WIDE */ 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate size = wstring(&chcount, &flag_eof, stow, 5190Sstevel@tonic-gate (int)ch, len, iop, &args.ap); 5200Sstevel@tonic-gate } else { 5210Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow, 5220Sstevel@tonic-gate (int)ch, len, tab, iop, &args.ap); 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate break; 5250Sstevel@tonic-gate case '[': 5260Sstevel@tonic-gate if (size == 'l') { 5270Sstevel@tonic-gate size = wbrstring(&chcount, &flag_eof, stow, 5280Sstevel@tonic-gate (int)ch, len, iop, bracket_str, 5290Sstevel@tonic-gate &args.ap); 5300Sstevel@tonic-gate free(bracket_str); 5310Sstevel@tonic-gate bracket_str = NULL; 5320Sstevel@tonic-gate } else { 5330Sstevel@tonic-gate #ifdef _WIDE 5340Sstevel@tonic-gate size = brstring(&chcount, &flag_eof, stow, 5350Sstevel@tonic-gate (int)ch, len, iop, bracket_str, 5360Sstevel@tonic-gate &args.ap); 5370Sstevel@tonic-gate free(bracket_str); 5380Sstevel@tonic-gate bracket_str = NULL; 5390Sstevel@tonic-gate #else /* _WIDE */ 5400Sstevel@tonic-gate size = string(&chcount, &flag_eof, stow, 5410Sstevel@tonic-gate ch, len, tab, iop, &args.ap); 5420Sstevel@tonic-gate #endif /* _WIDE */ 5430Sstevel@tonic-gate } 5440Sstevel@tonic-gate break; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate case 'n': 5470Sstevel@tonic-gate if (stow == 0) 5480Sstevel@tonic-gate continue; 5490Sstevel@tonic-gate if (size == 'b') /* char */ 5500Sstevel@tonic-gate *va_arg(args.ap, char *) = (char)chcount; 5510Sstevel@tonic-gate else if (size == 'h') 5520Sstevel@tonic-gate *va_arg(args.ap, short *) = (short)chcount; 5530Sstevel@tonic-gate else if (size == 'l') 5540Sstevel@tonic-gate *va_arg(args.ap, long *) = (long)chcount; 5550Sstevel@tonic-gate else if (size == 'm') /* long long */ 5560Sstevel@tonic-gate *va_arg(args.ap, long long *) = 5570Sstevel@tonic-gate (long long) chcount; 5580Sstevel@tonic-gate else 5590Sstevel@tonic-gate *va_arg(args.ap, int *) = (int)chcount; 5600Sstevel@tonic-gate continue; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate case 'i': 5630Sstevel@tonic-gate default: 5640Sstevel@tonic-gate size = number(&chcount, &flag_eof, stow, (int)ch, 5650Sstevel@tonic-gate len, (int)size, iop, &args.ap); 5660Sstevel@tonic-gate break; 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate if (size) 5690Sstevel@tonic-gate nmatch += stow; 5700Sstevel@tonic-gate else { 5710Sstevel@tonic-gate return ((flag_eof && !nmatch) ? EOF : nmatch); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate continue; 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate if (bracket_str) 5760Sstevel@tonic-gate free(bracket_str); 5770Sstevel@tonic-gate return (nmatch != 0 ? nmatch : EOF); /* end of input */ 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate /* ****************************************************************** */ 5810Sstevel@tonic-gate /* Functions to read the input stream in an attempt to match incoming */ 5820Sstevel@tonic-gate /* data to the current pattern from the main loop of _doscan(). */ 5830Sstevel@tonic-gate /* ****************************************************************** */ 5840Sstevel@tonic-gate static int 5850Sstevel@tonic-gate number(int *chcount, int *flag_eof, int stow, int type, int len, int size, 5860Sstevel@tonic-gate FILE *iop, va_list *listp) 5870Sstevel@tonic-gate { 5880Sstevel@tonic-gate char numbuf[64]; 5890Sstevel@tonic-gate char *np = numbuf; 5900Sstevel@tonic-gate int c, base, inchar, lookahead; 5910Sstevel@tonic-gate int digitseen = 0, floater = 0, negflg = 0; 5920Sstevel@tonic-gate int lc; 5930Sstevel@tonic-gate long long lcval = 0LL; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate switch (type) { 5960Sstevel@tonic-gate case 'e': 5970Sstevel@tonic-gate case 'f': 5980Sstevel@tonic-gate case 'g': 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * lc = 0 corresponds to c90 mode: do not recognize 6010Sstevel@tonic-gate * hexadecimal fp strings; attempt to push back 6020Sstevel@tonic-gate * all unused characters read 6030Sstevel@tonic-gate * 6040Sstevel@tonic-gate * lc = -1 corresponds to c99 mode: recognize hexa- 6050Sstevel@tonic-gate * decimal fp strings; push back at most one 6060Sstevel@tonic-gate * unused character 6070Sstevel@tonic-gate */ 6080Sstevel@tonic-gate lc = (__xpg6 & _C99SUSv3_recognize_hexfp)? -1 : 0; 6090Sstevel@tonic-gate floater = 1; 6100Sstevel@tonic-gate break; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate case 'a': 6130Sstevel@tonic-gate lc = -1; 6140Sstevel@tonic-gate floater = 1; 6150Sstevel@tonic-gate break; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate case 'd': 6180Sstevel@tonic-gate case 'u': 6190Sstevel@tonic-gate case 'i': 6200Sstevel@tonic-gate base = 10; 6210Sstevel@tonic-gate break; 6220Sstevel@tonic-gate case 'o': 6230Sstevel@tonic-gate base = 8; 6240Sstevel@tonic-gate break; 6250Sstevel@tonic-gate case 'p': 6260Sstevel@tonic-gate #ifdef _LP64 6270Sstevel@tonic-gate size = 'l'; /* pointers are long in LP64 */ 6280Sstevel@tonic-gate #endif /* _LP64 */ 6290Sstevel@tonic-gate /* FALLTHROUGH */ 6300Sstevel@tonic-gate case 'x': 6310Sstevel@tonic-gate base = 16; 6320Sstevel@tonic-gate break; 6330Sstevel@tonic-gate default: 6340Sstevel@tonic-gate return (0); /* unrecognized conversion character */ 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate if (floater != 0) { 6380Sstevel@tonic-gate /* 6390Sstevel@tonic-gate * Handle floating point with 6400Sstevel@tonic-gate * file_to_decimal. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate decimal_mode dm; 6430Sstevel@tonic-gate decimal_record dr; 6440Sstevel@tonic-gate fp_exception_field_type efs; 6450Sstevel@tonic-gate enum decimal_string_form form; 6460Sstevel@tonic-gate char *echar; 6470Sstevel@tonic-gate int nread; 6480Sstevel@tonic-gate char buffer[1024+1]; 6490Sstevel@tonic-gate char *nb = buffer; 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate if (len > 1024) 6520Sstevel@tonic-gate len = 1024; 6530Sstevel@tonic-gate file_to_decimal(&nb, len, lc, &dr, &form, &echar, iop, &nread); 6540Sstevel@tonic-gate if (lc == -1) { 6550Sstevel@tonic-gate /* 6560Sstevel@tonic-gate * In C99 mode, the entire string read has to be 6570Sstevel@tonic-gate * accepted in order to qualify as a match 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate if (nb != buffer + nread) 6600Sstevel@tonic-gate form = invalid_form; 6610Sstevel@tonic-gate } 6620Sstevel@tonic-gate if (stow && (form != invalid_form)) { 6630Sstevel@tonic-gate #if defined(__sparc) 6640Sstevel@tonic-gate dm.rd = _QgetRD(); 6650Sstevel@tonic-gate if (size == 'L') { /* long double */ 6660Sstevel@tonic-gate if ((int)form < 0) 6670Sstevel@tonic-gate __hex_to_quadruple(&dr, dm.rd, 6680Sstevel@tonic-gate va_arg(*listp, quadruple *), 6690Sstevel@tonic-gate &efs); 6700Sstevel@tonic-gate else 6710Sstevel@tonic-gate decimal_to_quadruple( 6720Sstevel@tonic-gate va_arg(*listp, quadruple *), 6730Sstevel@tonic-gate &dm, &dr, &efs); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 6760Sstevel@tonic-gate dm.rd = __xgetRD(); 6770Sstevel@tonic-gate if (size == 'L') { /* long double */ 6780Sstevel@tonic-gate if ((int)form < 0) 6790Sstevel@tonic-gate __hex_to_extended(&dr, dm.rd, 6800Sstevel@tonic-gate va_arg(*listp, extended *), 6810Sstevel@tonic-gate &efs); 6820Sstevel@tonic-gate else 6830Sstevel@tonic-gate decimal_to_extended( 6840Sstevel@tonic-gate va_arg(*listp, extended *), 6850Sstevel@tonic-gate &dm, &dr, &efs); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate #else 6880Sstevel@tonic-gate #error Unknown architecture 6890Sstevel@tonic-gate #endif 6900Sstevel@tonic-gate else if (size == 'l') { /* double */ 6910Sstevel@tonic-gate if ((int)form < 0) 6920Sstevel@tonic-gate __hex_to_double(&dr, dm.rd, 6930Sstevel@tonic-gate va_arg(*listp, double *), &efs); 6940Sstevel@tonic-gate else 6950Sstevel@tonic-gate decimal_to_double( 6960Sstevel@tonic-gate va_arg(*listp, double *), 6970Sstevel@tonic-gate &dm, &dr, &efs); 6980Sstevel@tonic-gate } else { /* float */ 6990Sstevel@tonic-gate if ((int)form < 0) 7000Sstevel@tonic-gate __hex_to_single(&dr, dm.rd, 7010Sstevel@tonic-gate va_arg(*listp, single *), &efs); 7020Sstevel@tonic-gate else 7030Sstevel@tonic-gate decimal_to_single((single *) 7040Sstevel@tonic-gate va_arg(*listp, single *), 7050Sstevel@tonic-gate &dm, &dr, &efs); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate if ((efs & (1 << fp_overflow)) != 0) { 7080Sstevel@tonic-gate errno = ERANGE; 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate if ((efs & (1 << fp_underflow)) != 0) { 7110Sstevel@tonic-gate errno = ERANGE; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate (*chcount) += nread; /* Count characters read. */ 7150Sstevel@tonic-gate c = locgetc((*chcount)); 7160Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF) 7170Sstevel@tonic-gate *flag_eof = 1; 7180Sstevel@tonic-gate return ((form == invalid_form) ? 0 : 1); 7190Sstevel@tonic-gate /* successful match if non-zero */ 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate switch (c = locgetc((*chcount))) { 7230Sstevel@tonic-gate case '-': 7240Sstevel@tonic-gate negflg++; 7250Sstevel@tonic-gate /* FALLTHROUGH */ 7260Sstevel@tonic-gate case '+': 7270Sstevel@tonic-gate if (--len <= 0) 7280Sstevel@tonic-gate break; 7290Sstevel@tonic-gate if ((c = locgetc((*chcount))) != '0') 7300Sstevel@tonic-gate break; 7310Sstevel@tonic-gate /* FALLTHROUGH */ 7320Sstevel@tonic-gate case '0': 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * If %i or %x, the characters 0x or 0X may optionally precede 7350Sstevel@tonic-gate * the sequence of letters and digits (base 16). 7360Sstevel@tonic-gate */ 7370Sstevel@tonic-gate if ((type != 'i' && type != 'x') || (len <= 1)) 7380Sstevel@tonic-gate break; 7390Sstevel@tonic-gate if (((inchar = locgetc((*chcount))) == 'x') || 740*6812Sraf (inchar == 'X')) { 7410Sstevel@tonic-gate lookahead = readchar(iop, chcount); 7420Sstevel@tonic-gate if (isxdigit(lookahead)) { 7430Sstevel@tonic-gate base = 16; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate if (len <= 2) { 7460Sstevel@tonic-gate (void) locungetc((*chcount), lookahead); 7470Sstevel@tonic-gate /* Take into account the 'x' */ 7480Sstevel@tonic-gate len -= 1; 7490Sstevel@tonic-gate } else { 7500Sstevel@tonic-gate c = lookahead; 7510Sstevel@tonic-gate /* Take into account '0x' */ 7520Sstevel@tonic-gate len -= 2; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate } else { 7550Sstevel@tonic-gate (void) locungetc((*chcount), lookahead); 7560Sstevel@tonic-gate (void) locungetc((*chcount), inchar); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate } else { 7590Sstevel@tonic-gate /* inchar wans't 'x'. */ 7600Sstevel@tonic-gate (void) locungetc((*chcount), inchar); /* Put it back. */ 7610Sstevel@tonic-gate if (type == 'i') /* Only %i accepts an octal. */ 7620Sstevel@tonic-gate base = 8; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate for (; --len >= 0; *np++ = (char)c, c = locgetc((*chcount))) { 7660Sstevel@tonic-gate if (np > numbuf + 62) { 767*6812Sraf errno = ERANGE; 768*6812Sraf return (0); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate if (isdigit(c) || base == 16 && isxdigit(c)) { 7710Sstevel@tonic-gate int digit = c - (isdigit(c) ? '0' : 772*6812Sraf isupper(c) ? 'A' - 10 : 'a' - 10); 7730Sstevel@tonic-gate if (digit >= base) 7740Sstevel@tonic-gate break; 7750Sstevel@tonic-gate if (stow) 7760Sstevel@tonic-gate lcval = base * lcval + digit; 7770Sstevel@tonic-gate digitseen++; 7780Sstevel@tonic-gate continue; 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate break; 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate if (stow && digitseen) { 7840Sstevel@tonic-gate /* suppress possible overflow on 2's-comp negation */ 7850Sstevel@tonic-gate if (negflg && lcval != (1ULL << 63)) 7860Sstevel@tonic-gate lcval = -lcval; 7870Sstevel@tonic-gate switch (size) { 7880Sstevel@tonic-gate case 'm': 7890Sstevel@tonic-gate *va_arg(*listp, long long *) = lcval; 7900Sstevel@tonic-gate break; 7910Sstevel@tonic-gate case 'l': 7920Sstevel@tonic-gate *va_arg(*listp, long *) = (long)lcval; 7930Sstevel@tonic-gate break; 7940Sstevel@tonic-gate case 'h': 7950Sstevel@tonic-gate *va_arg(*listp, short *) = (short)lcval; 7960Sstevel@tonic-gate break; 7970Sstevel@tonic-gate case 'b': 7980Sstevel@tonic-gate *va_arg(*listp, char *) = (char)lcval; 7990Sstevel@tonic-gate break; 8000Sstevel@tonic-gate default: 8010Sstevel@tonic-gate *va_arg(*listp, int *) = (int)lcval; 8020Sstevel@tonic-gate break; 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate if (locungetc((*chcount), c) == EOF) 806*6812Sraf *flag_eof = 1; 8070Sstevel@tonic-gate return (digitseen); /* successful match if non-zero */ 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate /* Get a character. If not using sscanf and at the buffer's end */ 8110Sstevel@tonic-gate /* then do a direct read(). Characters read via readchar() */ 8120Sstevel@tonic-gate /* can be pushed back on the input stream by locungetc((*chcount),) */ 8130Sstevel@tonic-gate /* since there is padding allocated at the end of the stream buffer. */ 8140Sstevel@tonic-gate static int 8150Sstevel@tonic-gate readchar(FILE *iop, int *chcount) 8160Sstevel@tonic-gate { 8170Sstevel@tonic-gate int inchar; 8180Sstevel@tonic-gate char buf[1]; 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate if ((iop->_flag & _IOWRT) || (iop->_cnt != 0)) 8210Sstevel@tonic-gate inchar = locgetc((*chcount)); 8220Sstevel@tonic-gate else { 8230Sstevel@tonic-gate if (read(FILENO(iop), buf, 1) != 1) 8240Sstevel@tonic-gate return (EOF); 8250Sstevel@tonic-gate inchar = (int)buf[0]; 8260Sstevel@tonic-gate (*chcount) += 1; 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate return (inchar); 8290Sstevel@tonic-gate } 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate static int 8320Sstevel@tonic-gate string(int *chcount, int *flag_eof, int stow, int type, int len, char *tab, 8330Sstevel@tonic-gate FILE *iop, va_list *listp) 8340Sstevel@tonic-gate { 8350Sstevel@tonic-gate int ch; 8360Sstevel@tonic-gate char *ptr; 8370Sstevel@tonic-gate char *start; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, char *) : NULL; 8400Sstevel@tonic-gate if (((type == 'c') || (type == 'C')) && len == MAXINT) 8410Sstevel@tonic-gate len = 1; 8420Sstevel@tonic-gate #ifdef _WIDE 8430Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF && 844*6812Sraf !(((type == 's') || (type == 'S')) && isspace(ch))) { 8450Sstevel@tonic-gate #else /* _WIDE */ 8460Sstevel@tonic-gate while ((ch = locgetc((*chcount))) != EOF && 847*6812Sraf !(((type == 's') || (type == 'S')) && 848*6812Sraf isspace(ch) || type == '[' && tab[ch])) { 8490Sstevel@tonic-gate #endif /* _WIDE */ 8500Sstevel@tonic-gate if (stow) 8510Sstevel@tonic-gate *ptr = (char)ch; 8520Sstevel@tonic-gate ptr++; 8530Sstevel@tonic-gate if (--len <= 0) 8540Sstevel@tonic-gate break; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate if (ch == EOF) { 8570Sstevel@tonic-gate (*flag_eof) = 1; 8580Sstevel@tonic-gate (*chcount) -= 1; 8590Sstevel@tonic-gate } else if (len > 0 && locungetc((*chcount), ch) == EOF) 8600Sstevel@tonic-gate (*flag_eof) = 1; 8610Sstevel@tonic-gate if (ptr == start) 8620Sstevel@tonic-gate return (0); /* no match */ 8630Sstevel@tonic-gate if (stow && ((type != 'c') && (type != 'C'))) 8640Sstevel@tonic-gate *ptr = '\0'; 8650Sstevel@tonic-gate return (1); /* successful match */ 8660Sstevel@tonic-gate } 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* This function initializes arglst, to contain the appropriate */ 8690Sstevel@tonic-gate /* va_list values for the first MAXARGS arguments. */ 8700Sstevel@tonic-gate /* WARNING: this code assumes that the sizes of all pointer types */ 8710Sstevel@tonic-gate /* are the same. (Code similar to that in the portable doprnt.c */ 8720Sstevel@tonic-gate /* should be used if this assumption is not true for a */ 8730Sstevel@tonic-gate /* particular port.) */ 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate #ifdef _WIDE 8760Sstevel@tonic-gate static int 8770Sstevel@tonic-gate _mkarglst(const wchar_t *fmt, stva_list args, stva_list arglst[]) 8780Sstevel@tonic-gate #else /* _WIDE */ 8790Sstevel@tonic-gate static int 8800Sstevel@tonic-gate _mkarglst(const char *fmt, stva_list args, stva_list arglst[]) 8810Sstevel@tonic-gate #endif /* _WIDE */ 8820Sstevel@tonic-gate { 8830Sstevel@tonic-gate #ifdef _WIDE 8840Sstevel@tonic-gate #define STRCHR wcschr 8850Sstevel@tonic-gate #define STRSPN wcsspn 8860Sstevel@tonic-gate #define ATOI(x) _watoi((wchar_t *)x) 8870Sstevel@tonic-gate #define SPNSTR1 L"01234567890" 8880Sstevel@tonic-gate #define SPNSTR2 L"# +-.0123456789hL$" 8890Sstevel@tonic-gate #else /* _WIDE */ 8900Sstevel@tonic-gate #define STRCHR strchr 8910Sstevel@tonic-gate #define STRSPN strspn 8920Sstevel@tonic-gate #define ATOI(x) atoi(x) 8930Sstevel@tonic-gate #define SPNSTR1 "01234567890" 8940Sstevel@tonic-gate #define SPNSTR2 "# +-.0123456789hL$" 8950Sstevel@tonic-gate #endif /* _WIDE */ 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate int maxnum, curargno; 8980Sstevel@tonic-gate size_t n; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate maxnum = -1; 9010Sstevel@tonic-gate curargno = 0; 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate while ((fmt = STRCHR(fmt, '%')) != NULL) { 9040Sstevel@tonic-gate fmt++; /* skip % */ 9050Sstevel@tonic-gate if (*fmt == '*' || *fmt == '%') 9060Sstevel@tonic-gate continue; 9070Sstevel@tonic-gate if (fmt[n = STRSPN(fmt, SPNSTR1)] == L'$') { 9080Sstevel@tonic-gate /* convert to zero base */ 9090Sstevel@tonic-gate curargno = ATOI(fmt) - 1; 9100Sstevel@tonic-gate fmt += n + 1; 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate if (maxnum < curargno) 9140Sstevel@tonic-gate maxnum = curargno; 9150Sstevel@tonic-gate curargno++; /* default to next in list */ 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate fmt += STRSPN(fmt, SPNSTR2); 9180Sstevel@tonic-gate if (*fmt == '[') { 9190Sstevel@tonic-gate int i; 9200Sstevel@tonic-gate fmt++; /* has to be at least on item in scan list */ 9210Sstevel@tonic-gate if (*fmt == ']') { 9220Sstevel@tonic-gate fmt++; 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate while (*fmt != ']') { 9250Sstevel@tonic-gate if (*fmt == L'\0') { 9260Sstevel@tonic-gate return (-1); /* bad format */ 9270Sstevel@tonic-gate #ifdef _WIDE 9280Sstevel@tonic-gate } else { 9290Sstevel@tonic-gate fmt++; 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate #else /* _WIDE */ 9320Sstevel@tonic-gate } else if (isascii(*fmt)) { 9330Sstevel@tonic-gate fmt++; 9340Sstevel@tonic-gate } else { 9350Sstevel@tonic-gate i = mblen((const char *) 9360Sstevel@tonic-gate fmt, MB_CUR_MAX); 9370Sstevel@tonic-gate if (i <= 0) { 9380Sstevel@tonic-gate return (-1); 9390Sstevel@tonic-gate } else { 9400Sstevel@tonic-gate fmt += i; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate #endif /* _WIDE */ 9440Sstevel@tonic-gate } 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate if (maxnum > MAXARGS) 9480Sstevel@tonic-gate maxnum = MAXARGS; 9490Sstevel@tonic-gate for (n = 0; n <= maxnum; n++) { 9500Sstevel@tonic-gate arglst[n] = args; 9510Sstevel@tonic-gate (void) va_arg(args.ap, void *); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate return (0); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate /* 9580Sstevel@tonic-gate * For wide character handling 9590Sstevel@tonic-gate */ 9600Sstevel@tonic-gate #ifdef _WIDE 9610Sstevel@tonic-gate static int 9620Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type, 9630Sstevel@tonic-gate int len, FILE *iop, va_list *listp) 9640Sstevel@tonic-gate { 9650Sstevel@tonic-gate wint_t wch; 9660Sstevel@tonic-gate wchar_t *ptr; 9670Sstevel@tonic-gate wchar_t *wstart; 9680Sstevel@tonic-gate int dummy; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL; 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate if ((type == 'c') && len == MAXINT) 9730Sstevel@tonic-gate len = 1; 9740Sstevel@tonic-gate while (((wch = _wd_getwc(chcount, iop)) != WEOF) && 975*6812Sraf !(type == 's' && iswspace(wch))) { 9760Sstevel@tonic-gate if (stow) 9770Sstevel@tonic-gate *ptr = wch; 9780Sstevel@tonic-gate ptr++; 9790Sstevel@tonic-gate if (--len <= 0) 9800Sstevel@tonic-gate break; 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate if (wch == WEOF) { 9830Sstevel@tonic-gate *flag_eof = 1; 9840Sstevel@tonic-gate (*chcount) -= 1; 9850Sstevel@tonic-gate } else { 9860Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(chcount, wch, iop) == WEOF) 9870Sstevel@tonic-gate *flag_eof = 1; 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate if (ptr == wstart) 9900Sstevel@tonic-gate return (0); /* no match */ 9910Sstevel@tonic-gate if (stow && (type != 'c')) 9920Sstevel@tonic-gate *ptr = '\0'; 9930Sstevel@tonic-gate return (1); /* successful match */ 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate #else /* _WIDE */ 9960Sstevel@tonic-gate static int 9970Sstevel@tonic-gate wstring(int *chcount, int *flag_eof, int stow, int type, int len, FILE *iop, 9980Sstevel@tonic-gate va_list *listp) 9990Sstevel@tonic-gate { 10000Sstevel@tonic-gate int wch; 10010Sstevel@tonic-gate wchar_t *ptr; 10020Sstevel@tonic-gate wchar_t *wstart; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate wstart = ptr = stow ? va_arg(*listp, wchar_t *) : NULL; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if ((type == 'c') && len == MAXINT) 10070Sstevel@tonic-gate len = 1; 10080Sstevel@tonic-gate while (((wch = _bi_getwc(iop)) != EOF) && 1009*6812Sraf !(type == 's' && (isascii(wch) ? isspace(wch) : 0))) { 10100Sstevel@tonic-gate (*chcount) += _scrwidth((wchar_t)wch); 10110Sstevel@tonic-gate if (stow) 10120Sstevel@tonic-gate *ptr = wch; 10130Sstevel@tonic-gate ptr++; 10140Sstevel@tonic-gate if (--len <= 0) 10150Sstevel@tonic-gate break; 10160Sstevel@tonic-gate } 10170Sstevel@tonic-gate if (wch == EOF) { 10180Sstevel@tonic-gate (*flag_eof) = 1; 10190Sstevel@tonic-gate (*chcount) -= 1; 10200Sstevel@tonic-gate } else { 10210Sstevel@tonic-gate if (len > 0 && _bi_ungetwc(wch, iop) == EOF) 10220Sstevel@tonic-gate (*flag_eof) = 1; 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate if (ptr == wstart) 10250Sstevel@tonic-gate return (0); /* no match */ 10260Sstevel@tonic-gate if (stow && (type != 'c')) 10270Sstevel@tonic-gate *ptr = '\0'; 10280Sstevel@tonic-gate return (1); /* successful match */ 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate #endif /* _WIDE */ 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate #ifdef _WIDE 10330Sstevel@tonic-gate static wint_t 10340Sstevel@tonic-gate _wd_getwc(int *chcount, FILE *iop) 10350Sstevel@tonic-gate { 10360Sstevel@tonic-gate wint_t wc; 10370Sstevel@tonic-gate int len; 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) { 10400Sstevel@tonic-gate /* call from fwscanf, wscanf */ 10410Sstevel@tonic-gate wc = __fgetwc_xpg5(iop); 10420Sstevel@tonic-gate (*chcount)++; 10430Sstevel@tonic-gate return (wc); 10440Sstevel@tonic-gate } else { 10450Sstevel@tonic-gate /* call from swscanf */ 10460Sstevel@tonic-gate if (*iop->_ptr == '\0') 10470Sstevel@tonic-gate return (WEOF); 10480Sstevel@tonic-gate len = mbtowc((wchar_t *)&wc, (const char *)iop->_ptr, 1049*6812Sraf MB_CUR_MAX); 10500Sstevel@tonic-gate if (len == -1) 10510Sstevel@tonic-gate return (WEOF); 10520Sstevel@tonic-gate iop->_ptr += len; 10530Sstevel@tonic-gate (*chcount)++; 10540Sstevel@tonic-gate return (wc); 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate static wint_t 10590Sstevel@tonic-gate _wd_ungetwc(int *chcount, wchar_t wc, FILE *iop) 10600Sstevel@tonic-gate { 10610Sstevel@tonic-gate wint_t ret; 10620Sstevel@tonic-gate int len; 10630Sstevel@tonic-gate char mbs[MB_LEN_MAX]; 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate if (wc == WEOF) 10660Sstevel@tonic-gate return (WEOF); 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (!(iop->_flag & _IOWRT)) { 10690Sstevel@tonic-gate /* call from fwscanf, wscanf */ 10700Sstevel@tonic-gate ret = __ungetwc_xpg5((wint_t)wc, iop); 10710Sstevel@tonic-gate if (ret != (wint_t)wc) 10720Sstevel@tonic-gate return (WEOF); 10730Sstevel@tonic-gate (*chcount)--; 10740Sstevel@tonic-gate return (ret); 10750Sstevel@tonic-gate } else { 10760Sstevel@tonic-gate /* call from swscanf */ 10770Sstevel@tonic-gate len = wctomb(mbs, wc); 10780Sstevel@tonic-gate if (len == -1) 10790Sstevel@tonic-gate return (WEOF); 10800Sstevel@tonic-gate iop->_ptr -= len; 10810Sstevel@tonic-gate (*chcount)--; 10820Sstevel@tonic-gate return ((wint_t)wc); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate 10860Sstevel@tonic-gate static int 10870Sstevel@tonic-gate _watoi(wchar_t *fmt) 10880Sstevel@tonic-gate { 10890Sstevel@tonic-gate int n = 0; 10900Sstevel@tonic-gate wchar_t ch; 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate ch = *fmt; 10930Sstevel@tonic-gate if ((ch >= 0) && (ch < 256) && isdigit((int)ch)) { 10940Sstevel@tonic-gate n = ch - '0'; 10950Sstevel@tonic-gate while (((ch = *++fmt) >= 0) && (ch < 256) && 1096*6812Sraf isdigit((int)ch)) { 10970Sstevel@tonic-gate n *= 10; 10980Sstevel@tonic-gate n += ch - '0'; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate return (n); 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate #endif /* _WIDE */ 11040Sstevel@tonic-gate 11050Sstevel@tonic-gate /* ARGSUSED3 */ 11060Sstevel@tonic-gate static int 11070Sstevel@tonic-gate wbrstring(int *chcount, int *flag_eof, int stow, int type, 11080Sstevel@tonic-gate int len, FILE *iop, unsigned char *brstr, va_list *listp) 11090Sstevel@tonic-gate { 11100Sstevel@tonic-gate wint_t wch; 11110Sstevel@tonic-gate int i; 11120Sstevel@tonic-gate char str[MB_LEN_MAX + 1]; /* include null termination */ 11130Sstevel@tonic-gate wchar_t *ptr, *start; 11140Sstevel@tonic-gate #ifdef _WIDE 11150Sstevel@tonic-gate int dummy; 11160Sstevel@tonic-gate #endif /* _WIDE */ 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, wchar_t *) : NULL; 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate #ifdef _WIDE 11210Sstevel@tonic-gate while ((wch = _wd_getwc(&dummy, iop)) != WEOF) { 11220Sstevel@tonic-gate #else /* _WIDE */ 11230Sstevel@tonic-gate while ((wch = _bi_getwc(iop)) != WEOF) { 11240Sstevel@tonic-gate #endif /* _WIDE */ 11250Sstevel@tonic-gate i = wctomb(str, (wchar_t)wch); 11260Sstevel@tonic-gate if (i == -1) { 11270Sstevel@tonic-gate return (0); 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate str[i] = '\0'; 11300Sstevel@tonic-gate if (fnmatch((const char *)brstr, (const char *)str, 1131*6812Sraf FNM_NOESCAPE)) { 11320Sstevel@tonic-gate break; 11330Sstevel@tonic-gate } else { 11340Sstevel@tonic-gate if (len > 0) { 11350Sstevel@tonic-gate #ifdef _WIDE 11360Sstevel@tonic-gate (*chcount)++; 11370Sstevel@tonic-gate #else /* _WIDE */ 11380Sstevel@tonic-gate (*chcount) += _scrwidth(wch); 11390Sstevel@tonic-gate #endif /* _WIDE */ 11400Sstevel@tonic-gate len--; 11410Sstevel@tonic-gate if (stow) { 11420Sstevel@tonic-gate *ptr = wch; 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate ptr++; 11450Sstevel@tonic-gate if (len <= 0) 11460Sstevel@tonic-gate break; 11470Sstevel@tonic-gate } else { 11480Sstevel@tonic-gate break; 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate } 11520Sstevel@tonic-gate if (wch == WEOF) { 11530Sstevel@tonic-gate *flag_eof = 1; 11540Sstevel@tonic-gate } else { 11550Sstevel@tonic-gate #ifdef _WIDE 11560Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF) 11570Sstevel@tonic-gate #else /* _WIDE */ 11580Sstevel@tonic-gate if (len > 0 && _bi_ungetwc(wch, iop) == WEOF) 11590Sstevel@tonic-gate #endif /* _WIDE */ 11600Sstevel@tonic-gate *flag_eof = 1; 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate if (ptr == start) 11630Sstevel@tonic-gate return (0); /* no match */ 11640Sstevel@tonic-gate if (stow) 11650Sstevel@tonic-gate *ptr = L'\0'; 11660Sstevel@tonic-gate return (1); /* successful match */ 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate #ifdef _WIDE 11700Sstevel@tonic-gate static int 11710Sstevel@tonic-gate brstring(int *chcount, int *flag_eof, int stow, int type, 11720Sstevel@tonic-gate int len, FILE *iop, unsigned char *brstr, va_list *listp) 11730Sstevel@tonic-gate { 11740Sstevel@tonic-gate wint_t wch; 11750Sstevel@tonic-gate int i; 11760Sstevel@tonic-gate char str[MB_LEN_MAX + 1]; /* include null termination */ 11770Sstevel@tonic-gate char *ptr, *start, *p; 11780Sstevel@tonic-gate int dummy; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate start = ptr = stow ? va_arg(*listp, char *) : NULL; 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate while ((wch = _wd_getwc(&dummy, iop)) != WEOF) { 11830Sstevel@tonic-gate p = str; 11840Sstevel@tonic-gate i = wctomb(str, (wchar_t)wch); 11850Sstevel@tonic-gate if (i == -1) { 11860Sstevel@tonic-gate return (0); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate str[i] = '\0'; 11890Sstevel@tonic-gate if (fnmatch((const char *)brstr, (const char *)str, 1190*6812Sraf FNM_NOESCAPE)) { 11910Sstevel@tonic-gate break; 11920Sstevel@tonic-gate } else { 11930Sstevel@tonic-gate if (len >= i) { 11940Sstevel@tonic-gate (*chcount)++; 11950Sstevel@tonic-gate len -= i; 11960Sstevel@tonic-gate if (stow) { 11970Sstevel@tonic-gate while (i-- > 0) { 11980Sstevel@tonic-gate *ptr++ = *p++; 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate } else { 12010Sstevel@tonic-gate while (i-- > 0) { 12020Sstevel@tonic-gate ptr++; 12030Sstevel@tonic-gate } 12040Sstevel@tonic-gate } 12050Sstevel@tonic-gate if (len <= 0) 12060Sstevel@tonic-gate break; 12070Sstevel@tonic-gate } else { 12080Sstevel@tonic-gate break; 12090Sstevel@tonic-gate } 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate } 12120Sstevel@tonic-gate if (wch == WEOF) { 12130Sstevel@tonic-gate *flag_eof = 1; 12140Sstevel@tonic-gate } else { 12150Sstevel@tonic-gate if (len > 0 && _wd_ungetwc(&dummy, wch, iop) == WEOF) 12160Sstevel@tonic-gate *flag_eof = 1; 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate if (ptr == start) 12190Sstevel@tonic-gate return (0); /* no match */ 12200Sstevel@tonic-gate if (stow) 12210Sstevel@tonic-gate *ptr = '\0'; 12220Sstevel@tonic-gate return (1); /* successful match */ 12230Sstevel@tonic-gate } 12240Sstevel@tonic-gate #endif /* _WIDE */ 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate /* 12270Sstevel@tonic-gate * Locally define getwc and ungetwc 12280Sstevel@tonic-gate */ 12290Sstevel@tonic-gate static int 12300Sstevel@tonic-gate _bi_getwc(FILE *iop) 12310Sstevel@tonic-gate { 12320Sstevel@tonic-gate int c; 12330Sstevel@tonic-gate wchar_t intcode; 12340Sstevel@tonic-gate int i, nbytes, cur_max; 12350Sstevel@tonic-gate char buff[MB_LEN_MAX]; 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate if ((c = wlocgetc()) == EOF) 12380Sstevel@tonic-gate return (WEOF); 12390Sstevel@tonic-gate 12400Sstevel@tonic-gate if (isascii(c)) /* ASCII code */ 12410Sstevel@tonic-gate return ((wint_t)c); 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate buff[0] = (char)c; 12440Sstevel@tonic-gate 12450Sstevel@tonic-gate cur_max = (int)MB_CUR_MAX; 12460Sstevel@tonic-gate /* MB_CUR_MAX doen't exeed the value of MB_LEN_MAX */ 12470Sstevel@tonic-gate /* So we use MB_CUR_MAX instead of MB_LEN_MAX for */ 12480Sstevel@tonic-gate /* improving the performance. */ 12490Sstevel@tonic-gate for (i = 1; i < cur_max; i++) { 12500Sstevel@tonic-gate c = wlocgetc(); 12510Sstevel@tonic-gate if (c == '\n') { 12520Sstevel@tonic-gate (void) wlocungetc(c); 12530Sstevel@tonic-gate break; 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate if (c == EOF) { 12560Sstevel@tonic-gate /* this still may be a valid multibyte character */ 12570Sstevel@tonic-gate break; 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate buff[i] = (char)c; 12600Sstevel@tonic-gate } 12610Sstevel@tonic-gate 12620Sstevel@tonic-gate if ((nbytes = mbtowc(&intcode, buff, i)) == -1) { 12630Sstevel@tonic-gate /* 12640Sstevel@tonic-gate * If mbtowc fails, the input was not a legal character. 12650Sstevel@tonic-gate * ungetc all but one character. 12660Sstevel@tonic-gate * 12670Sstevel@tonic-gate * Note: the number of pushback characters that 12680Sstevel@tonic-gate * ungetc() can handle must be >= (MB_LEN_MAX - 1). 12690Sstevel@tonic-gate * In Solaris 2.x, the number of pushback 12700Sstevel@tonic-gate * characters is 4. 12710Sstevel@tonic-gate */ 12720Sstevel@tonic-gate while (i-- > 1) { 12730Sstevel@tonic-gate (void) wlocungetc((signed char)buff[i]); 12740Sstevel@tonic-gate } 12750Sstevel@tonic-gate errno = EILSEQ; 12760Sstevel@tonic-gate return (WEOF); /* Illegal EUC sequence. */ 12770Sstevel@tonic-gate } 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate while (i-- > nbytes) { 12800Sstevel@tonic-gate /* 12810Sstevel@tonic-gate * Note: the number of pushback characters that 12820Sstevel@tonic-gate * ungetc() can handle must be >= (MB_LEN_MAX - 1). 12830Sstevel@tonic-gate * In Solaris 2.x, the number of pushback 12840Sstevel@tonic-gate * characters is 4. 12850Sstevel@tonic-gate */ 12860Sstevel@tonic-gate (void) wlocungetc((signed char)buff[i]); 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate return ((int)intcode); 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate static int 12920Sstevel@tonic-gate _bi_ungetwc(wint_t wc, FILE *iop) 12930Sstevel@tonic-gate { 12940Sstevel@tonic-gate char mbs[MB_LEN_MAX]; 12950Sstevel@tonic-gate unsigned char *p; 12960Sstevel@tonic-gate int n; 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0)) 12990Sstevel@tonic-gate return (WEOF); 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate n = wctomb(mbs, (wchar_t)wc); 13020Sstevel@tonic-gate if (n <= 0) 13030Sstevel@tonic-gate return (WEOF); 13040Sstevel@tonic-gate 13050Sstevel@tonic-gate if (iop->_ptr <= iop->_base) { 13060Sstevel@tonic-gate if (iop->_base == NULL) { 13070Sstevel@tonic-gate return (WEOF); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate if ((iop->_ptr == iop->_base) && (iop->_cnt == 0)) { 13100Sstevel@tonic-gate ++iop->_ptr; 13110Sstevel@tonic-gate } else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) { 13120Sstevel@tonic-gate return (WEOF); 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate p = (unsigned char *)(mbs+n-1); /* p points the last byte */ 13170Sstevel@tonic-gate /* if _IOWRT is set to iop->_flag, it means this is */ 13180Sstevel@tonic-gate /* an invocation from sscanf(), and in that time we */ 13190Sstevel@tonic-gate /* don't touch iop->_cnt. Otherwise, which means an */ 13200Sstevel@tonic-gate /* invocation from fscanf() or scanf(), we touch iop->_cnt */ 13210Sstevel@tonic-gate if ((iop->_flag & _IOWRT) == 0) { 13220Sstevel@tonic-gate /* scanf() and fscanf() */ 13230Sstevel@tonic-gate iop->_cnt += n; 13240Sstevel@tonic-gate while (n--) { 13250Sstevel@tonic-gate *--iop->_ptr = *(p--); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate } else { 13280Sstevel@tonic-gate /* sscanf() */ 13290Sstevel@tonic-gate iop->_ptr -= n; 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate return (wc); 13320Sstevel@tonic-gate } 1333