Home | History | Annotate | Line # | Download | only in stdio
vfwscanf.c revision 1.10
      1  1.10     joerg /*	$NetBSD: vfwscanf.c,v 1.10 2013/05/17 12:55:57 joerg Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1990, 1993
      5   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to Berkeley by
      8   1.1  christos  * Chris Torek.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19   1.1  christos  *    must display the following acknowledgement:
     20   1.1  christos  *	This product includes software developed by the University of
     21   1.1  christos  *	California, Berkeley and its contributors.
     22   1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     23   1.1  christos  *    may be used to endorse or promote products derived from this software
     24   1.1  christos  *    without specific prior written permission.
     25   1.1  christos  *
     26   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1  christos  * SUCH DAMAGE.
     37   1.1  christos  */
     38   1.1  christos 
     39   1.1  christos #include <sys/cdefs.h>
     40   1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     41   1.1  christos #if 0
     42   1.1  christos static char sccsid[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
     43   1.1  christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
     44   1.1  christos #else
     45  1.10     joerg __RCSID("$NetBSD: vfwscanf.c,v 1.10 2013/05/17 12:55:57 joerg Exp $");
     46   1.1  christos #endif
     47   1.1  christos #endif /* LIBC_SCCS and not lint */
     48   1.1  christos 
     49   1.1  christos #include "namespace.h"
     50   1.1  christos #include <ctype.h>
     51   1.1  christos #include <inttypes.h>
     52   1.7  christos #include <assert.h>
     53   1.1  christos #include <stdio.h>
     54   1.1  christos #include <stdlib.h>
     55   1.1  christos #include <stddef.h>
     56   1.1  christos #include <stdarg.h>
     57   1.1  christos #include <string.h>
     58   1.1  christos #include <limits.h>
     59   1.1  christos #include <wchar.h>
     60   1.1  christos #include <wctype.h>
     61   1.1  christos 
     62   1.1  christos #include "reentrant.h"
     63   1.1  christos #include "local.h"
     64   1.1  christos 
     65   1.1  christos #include <locale.h>
     66   1.9     joerg #include "setlocale_local.h"
     67   1.1  christos 
     68   1.1  christos #define	BUF		513	/* Maximum length of numeric string. */
     69   1.1  christos 
     70   1.1  christos /*
     71   1.1  christos  * Flags used during conversion.
     72   1.1  christos  */
     73   1.1  christos #define	LONG		0x01	/* l: long or double */
     74   1.1  christos #define	LONGDBL		0x02	/* L: long double */
     75   1.1  christos #define	SHORT		0x04	/* h: short */
     76   1.1  christos #define	SUPPRESS	0x08	/* *: suppress assignment */
     77   1.1  christos #define	POINTER		0x10	/* p: void * (as hex) */
     78   1.1  christos #define	NOSKIP		0x20	/* [ or c: do not skip blanks */
     79   1.1  christos #define	LONGLONG	0x400	/* ll: quad_t (+ deprecated q: quad) */
     80   1.1  christos #define	INTMAXT		0x800	/* j: intmax_t */
     81   1.1  christos #define	PTRDIFFT	0x1000	/* t: ptrdiff_t */
     82   1.1  christos #define	SIZET		0x2000	/* z: size_t */
     83   1.1  christos #define	SHORTSHORT	0x4000	/* hh: char */
     84   1.1  christos #define	UNSIGNED	0x8000	/* %[oupxX] conversions */
     85   1.1  christos 
     86   1.1  christos /*
     87   1.1  christos  * The following are used in integral conversions only:
     88   1.1  christos  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
     89   1.1  christos  */
     90   1.1  christos #define	SIGNOK		0x40	/* +/- is (still) legal */
     91   1.1  christos #define	NDIGITS		0x80	/* no digits detected */
     92   1.1  christos #define	PFXOK		0x100	/* 0x prefix is (still) legal */
     93   1.1  christos #define	NZDIGITS	0x200	/* no zero digits detected */
     94   1.1  christos #define	HAVESIGN	0x10000	/* sign detected */
     95   1.1  christos 
     96   1.1  christos /*
     97   1.1  christos  * Conversion types.
     98   1.1  christos  */
     99   1.1  christos #define	CT_CHAR		0	/* %c conversion */
    100   1.1  christos #define	CT_CCL		1	/* %[...] conversion */
    101   1.1  christos #define	CT_STRING	2	/* %s conversion */
    102   1.1  christos #define	CT_INT		3	/* %[dioupxX] conversion */
    103   1.1  christos #define	CT_FLOAT	4	/* %[efgEFG] conversion */
    104   1.1  christos 
    105   1.9     joerg static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
    106   1.1  christos 
    107   1.1  christos #define	INCCL(_c)	\
    108   1.1  christos 	(cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
    109   1.1  christos 	(wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
    110   1.1  christos 
    111   1.1  christos /*
    112   1.1  christos  * MT-safe version.
    113   1.1  christos  */
    114   1.1  christos int
    115   1.1  christos vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
    116   1.1  christos {
    117  1.10     joerg 	return vfwscanf_l(fp, _current_locale(), fmt, ap);
    118   1.9     joerg }
    119   1.9     joerg 
    120   1.9     joerg int
    121   1.9     joerg vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
    122   1.9     joerg     va_list ap)
    123   1.9     joerg {
    124   1.1  christos 	int ret;
    125   1.1  christos 
    126   1.1  christos 	FLOCKFILE(fp);
    127   1.1  christos 	_SET_ORIENTATION(fp, 1);
    128   1.9     joerg 	ret = __vfwscanf_unlocked_l(fp, loc, fmt, ap);
    129   1.1  christos 	FUNLOCKFILE(fp);
    130   1.8  christos 	return ret;
    131   1.1  christos }
    132   1.1  christos 
    133   1.3  christos #define SCANF_SKIP_SPACE() \
    134   1.3  christos do { \
    135   1.3  christos 	wint_t tc; \
    136   1.3  christos  \
    137   1.9     joerg 	while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace_l(tc, loc)) \
    138   1.3  christos 		continue; \
    139   1.3  christos 	if (tc != WEOF) \
    140   1.3  christos 		ungetwc(tc, fp); \
    141   1.3  christos } while (/*CONSTCOND*/ 0)
    142   1.3  christos 
    143   1.1  christos /*
    144   1.1  christos  * Non-MT-safe version.
    145   1.1  christos  */
    146   1.1  christos int
    147   1.9     joerg __vfwscanf_unlocked_l(FILE * __restrict fp, locale_t loc,
    148   1.9     joerg     const wchar_t * __restrict fmt, va_list ap)
    149   1.1  christos {
    150   1.1  christos 	wint_t c;		/* character from format, or conversion */
    151   1.1  christos 	size_t width;		/* field width, or 0 */
    152   1.1  christos 	wchar_t *p;		/* points into all kinds of strings */
    153   1.1  christos 	int n;			/* handy integer */
    154   1.1  christos 	int flags;		/* flags as defined above */
    155   1.1  christos 	wchar_t *p0;		/* saves original value of p when necessary */
    156   1.1  christos 	int nassigned;		/* number of fields assigned */
    157   1.1  christos 	int nconversions;	/* number of conversions */
    158   1.7  christos 	size_t nread;		/* number of characters consumed from fp */
    159   1.1  christos 	int base;		/* base argument to conversion function */
    160   1.1  christos 	wchar_t buf[BUF];	/* buffer for numeric conversions */
    161   1.1  christos 	const wchar_t *ccls;	/* character class start */
    162   1.1  christos 	const wchar_t *ccle;	/* character class end */
    163   1.1  christos 	int cclcompl;		/* ccl is complemented? */
    164   1.1  christos 	wint_t wi;		/* handy wint_t */
    165   1.1  christos 	char *mbp;		/* multibyte string pointer for %c %s %[ */
    166   1.1  christos 	size_t nconv;		/* number of bytes in mb. conversion */
    167   1.1  christos 	static const mbstate_t initial;
    168   1.1  christos 	mbstate_t mbs;
    169   1.9     joerg 	char mbbuf[MB_LEN_MAX];	/* temporary mb. character buffer */
    170   1.1  christos 	/* `basefix' is used to avoid `if' tests in the integer scanner */
    171   1.1  christos 	static short basefix[17] =
    172   1.1  christos 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    173   1.1  christos 
    174   1.1  christos 	nassigned = 0;
    175   1.1  christos 	nconversions = 0;
    176   1.1  christos 	nread = 0;
    177   1.1  christos 	ccls = ccle = NULL;
    178   1.2     lukem 	base = 0;
    179   1.2     lukem 	cclcompl = 0;
    180   1.2     lukem 	mbp = NULL;
    181   1.1  christos 	for (;;) {
    182   1.1  christos 		c = *fmt++;
    183   1.1  christos 		if (c == 0)
    184   1.8  christos 			return nassigned;
    185   1.9     joerg 		if (iswspace_l(c, loc)) {
    186   1.1  christos 			while ((c = __fgetwc_unlock(fp)) != WEOF &&
    187   1.9     joerg 			    iswspace_l(c, loc))
    188   1.1  christos 				;
    189   1.1  christos 			if (c != WEOF)
    190   1.1  christos 				ungetwc(c, fp);
    191   1.1  christos 			continue;
    192   1.1  christos 		}
    193   1.1  christos 		if (c != '%')
    194   1.1  christos 			goto literal;
    195   1.1  christos 		width = 0;
    196   1.1  christos 		flags = 0;
    197   1.1  christos 		/*
    198   1.1  christos 		 * switch on the format.  continue if done;
    199   1.1  christos 		 * break once format type is derived.
    200   1.1  christos 		 */
    201   1.1  christos again:		c = *fmt++;
    202   1.1  christos 		switch (c) {
    203   1.1  christos 		case '%':
    204   1.3  christos 			SCANF_SKIP_SPACE();
    205   1.1  christos literal:
    206   1.1  christos 			if ((wi = __fgetwc_unlock(fp)) == WEOF)
    207   1.1  christos 				goto input_failure;
    208   1.1  christos 			if (wi != c) {
    209   1.1  christos 				ungetwc(wi, fp);
    210   1.1  christos 				goto input_failure;
    211   1.1  christos 			}
    212   1.1  christos 			nread++;
    213   1.1  christos 			continue;
    214   1.1  christos 
    215   1.1  christos 		case '*':
    216   1.1  christos 			flags |= SUPPRESS;
    217   1.1  christos 			goto again;
    218   1.1  christos 		case 'j':
    219   1.1  christos 			flags |= INTMAXT;
    220   1.1  christos 			goto again;
    221   1.1  christos 		case 'l':
    222   1.1  christos 			if (flags & LONG) {
    223   1.1  christos 				flags &= ~LONG;
    224   1.1  christos 				flags |= LONGLONG;
    225   1.1  christos 			} else
    226   1.1  christos 				flags |= LONG;
    227   1.1  christos 			goto again;
    228   1.1  christos 		case 'q':
    229   1.1  christos 			flags |= LONGLONG;	/* not quite */
    230   1.1  christos 			goto again;
    231   1.1  christos 		case 't':
    232   1.1  christos 			flags |= PTRDIFFT;
    233   1.1  christos 			goto again;
    234   1.1  christos 		case 'z':
    235   1.1  christos 			flags |= SIZET;
    236   1.1  christos 			goto again;
    237   1.1  christos 		case 'L':
    238   1.1  christos 			flags |= LONGDBL;
    239   1.1  christos 			goto again;
    240   1.1  christos 		case 'h':
    241   1.1  christos 			if (flags & SHORT) {
    242   1.1  christos 				flags &= ~SHORT;
    243   1.1  christos 				flags |= SHORTSHORT;
    244   1.1  christos 			} else
    245   1.1  christos 				flags |= SHORT;
    246   1.1  christos 			goto again;
    247   1.1  christos 
    248   1.1  christos 		case '0': case '1': case '2': case '3': case '4':
    249   1.1  christos 		case '5': case '6': case '7': case '8': case '9':
    250   1.1  christos 			width = width * 10 + c - '0';
    251   1.1  christos 			goto again;
    252   1.1  christos 
    253   1.1  christos 		/*
    254   1.1  christos 		 * Conversions.
    255   1.1  christos 		 */
    256   1.1  christos 		case 'd':
    257   1.1  christos 			c = CT_INT;
    258   1.1  christos 			base = 10;
    259   1.1  christos 			break;
    260   1.1  christos 
    261   1.1  christos 		case 'i':
    262   1.1  christos 			c = CT_INT;
    263   1.1  christos 			base = 0;
    264   1.1  christos 			break;
    265   1.1  christos 
    266   1.1  christos 		case 'o':
    267   1.1  christos 			c = CT_INT;
    268   1.1  christos 			flags |= UNSIGNED;
    269   1.1  christos 			base = 8;
    270   1.1  christos 			break;
    271   1.1  christos 
    272   1.1  christos 		case 'u':
    273   1.1  christos 			c = CT_INT;
    274   1.1  christos 			flags |= UNSIGNED;
    275   1.1  christos 			base = 10;
    276   1.1  christos 			break;
    277   1.1  christos 
    278   1.1  christos 		case 'X':
    279   1.1  christos 		case 'x':
    280   1.1  christos 			flags |= PFXOK;	/* enable 0x prefixing */
    281   1.1  christos 			c = CT_INT;
    282   1.1  christos 			flags |= UNSIGNED;
    283   1.1  christos 			base = 16;
    284   1.1  christos 			break;
    285   1.1  christos 
    286   1.1  christos #ifndef NO_FLOATING_POINT
    287   1.1  christos 		case 'A': case 'E': case 'F': case 'G':
    288   1.1  christos 		case 'a': case 'e': case 'f': case 'g':
    289   1.1  christos 			c = CT_FLOAT;
    290   1.1  christos 			break;
    291   1.1  christos #endif
    292   1.1  christos 
    293   1.1  christos 		case 'S':
    294   1.1  christos 			flags |= LONG;
    295   1.1  christos 			/* FALLTHROUGH */
    296   1.1  christos 		case 's':
    297   1.1  christos 			c = CT_STRING;
    298   1.1  christos 			break;
    299   1.1  christos 
    300   1.1  christos 		case '[':
    301   1.1  christos 			ccls = fmt;
    302   1.1  christos 			if (*fmt == '^') {
    303   1.1  christos 				cclcompl = 1;
    304   1.1  christos 				fmt++;
    305   1.1  christos 			} else
    306   1.1  christos 				cclcompl = 0;
    307   1.1  christos 			if (*fmt == ']')
    308   1.1  christos 				fmt++;
    309   1.1  christos 			while (*fmt != '\0' && *fmt != ']')
    310   1.1  christos 				fmt++;
    311   1.1  christos 			ccle = fmt;
    312   1.1  christos 			fmt++;
    313   1.1  christos 			flags |= NOSKIP;
    314   1.1  christos 			c = CT_CCL;
    315   1.1  christos 			break;
    316   1.1  christos 
    317   1.1  christos 		case 'C':
    318   1.1  christos 			flags |= LONG;
    319   1.1  christos 			/* FALLTHROUGH */
    320   1.1  christos 		case 'c':
    321   1.1  christos 			flags |= NOSKIP;
    322   1.1  christos 			c = CT_CHAR;
    323   1.1  christos 			break;
    324   1.1  christos 
    325   1.1  christos 		case 'p':	/* pointer format is like hex */
    326   1.1  christos 			flags |= POINTER | PFXOK;
    327   1.1  christos 			c = CT_INT;		/* assumes sizeof(uintmax_t) */
    328   1.1  christos 			flags |= UNSIGNED;	/*      >= sizeof(uintptr_t) */
    329   1.1  christos 			base = 16;
    330   1.1  christos 			break;
    331   1.1  christos 
    332   1.1  christos 		case 'n':
    333   1.1  christos 			nconversions++;
    334   1.1  christos 			if (flags & SUPPRESS)	/* ??? */
    335   1.1  christos 				continue;
    336   1.1  christos 			if (flags & SHORTSHORT)
    337   1.7  christos 				*va_arg(ap, char *) = (char)nread;
    338   1.1  christos 			else if (flags & SHORT)
    339   1.7  christos 				*va_arg(ap, short *) = (short)nread;
    340   1.1  christos 			else if (flags & LONG)
    341   1.1  christos 				*va_arg(ap, long *) = nread;
    342   1.1  christos 			else if (flags & LONGLONG)
    343   1.1  christos 				*va_arg(ap, quad_t *) = nread;
    344   1.1  christos 			else if (flags & INTMAXT)
    345   1.1  christos 				*va_arg(ap, intmax_t *) = nread;
    346   1.1  christos 			else if (flags & SIZET)
    347   1.1  christos 				*va_arg(ap, size_t *) = nread;
    348   1.1  christos 			else if (flags & PTRDIFFT)
    349   1.1  christos 				*va_arg(ap, ptrdiff_t *) = nread;
    350   1.1  christos 			else
    351   1.7  christos 				*va_arg(ap, int *) = (int)nread;
    352   1.1  christos 			continue;
    353   1.1  christos 
    354   1.1  christos 		default:
    355   1.1  christos 			goto match_failure;
    356   1.1  christos 
    357   1.1  christos 		/*
    358   1.1  christos 		 * Disgusting backwards compatibility hack.	XXX
    359   1.1  christos 		 */
    360   1.1  christos 		case '\0':	/* compat */
    361   1.8  christos 			return EOF;
    362   1.1  christos 		}
    363   1.1  christos 
    364   1.1  christos 		/*
    365   1.1  christos 		 * Consume leading white space, except for formats
    366   1.1  christos 		 * that suppress this.
    367   1.1  christos 		 */
    368   1.1  christos 		if ((flags & NOSKIP) == 0) {
    369   1.9     joerg 			while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    370   1.9     joerg 			       iswspace_l(wi, loc))
    371   1.1  christos 				nread++;
    372   1.1  christos 			if (wi == WEOF)
    373   1.1  christos 				goto input_failure;
    374   1.1  christos 			ungetwc(wi, fp);
    375   1.1  christos 		}
    376   1.1  christos 
    377   1.1  christos 		/*
    378   1.1  christos 		 * Do the conversion.
    379   1.1  christos 		 */
    380   1.1  christos 		switch (c) {
    381   1.1  christos 
    382   1.1  christos 		case CT_CHAR:
    383   1.1  christos 			/* scan arbitrary characters (sets NOSKIP) */
    384   1.1  christos 			if (width == 0)
    385   1.1  christos 				width = 1;
    386   1.1  christos 			if (flags & LONG) {
    387   1.1  christos 				if (!(flags & SUPPRESS))
    388   1.1  christos 					p = va_arg(ap, wchar_t *);
    389   1.1  christos 				n = 0;
    390   1.1  christos 				while (width-- != 0 &&
    391   1.1  christos 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
    392   1.1  christos 					if (!(flags & SUPPRESS))
    393   1.1  christos 						*p++ = (wchar_t)wi;
    394   1.1  christos 					n++;
    395   1.1  christos 				}
    396   1.1  christos 				if (n == 0)
    397   1.1  christos 					goto input_failure;
    398   1.1  christos 				nread += n;
    399   1.1  christos 				if (!(flags & SUPPRESS))
    400   1.1  christos 					nassigned++;
    401   1.1  christos 			} else {
    402   1.1  christos 				if (!(flags & SUPPRESS))
    403   1.1  christos 					mbp = va_arg(ap, char *);
    404   1.1  christos 				n = 0;
    405   1.1  christos 				mbs = initial;
    406   1.1  christos 				while (width != 0 &&
    407   1.1  christos 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
    408   1.9     joerg 					if (width >= MB_CUR_MAX_L(loc) &&
    409   1.1  christos 					    !(flags & SUPPRESS)) {
    410   1.9     joerg 						nconv = wcrtomb_l(mbp, wi,
    411   1.9     joerg 						    &mbs, loc);
    412   1.1  christos 						if (nconv == (size_t)-1)
    413   1.1  christos 							goto input_failure;
    414   1.1  christos 					} else {
    415   1.9     joerg 						nconv = wcrtomb_l(mbbuf, wi,
    416   1.9     joerg 						    &mbs, loc);
    417   1.1  christos 						if (nconv == (size_t)-1)
    418   1.1  christos 							goto input_failure;
    419   1.1  christos 						if (nconv > width) {
    420   1.1  christos 							ungetwc(wi, fp);
    421   1.1  christos 							break;
    422   1.1  christos 						}
    423   1.1  christos 						if (!(flags & SUPPRESS))
    424   1.1  christos 							memcpy(mbp, mbbuf,
    425   1.1  christos 							    nconv);
    426   1.1  christos 					}
    427   1.1  christos 					if (!(flags & SUPPRESS))
    428   1.1  christos 						mbp += nconv;
    429   1.1  christos 					width -= nconv;
    430   1.1  christos 					n++;
    431   1.1  christos 				}
    432   1.1  christos 				if (n == 0)
    433   1.1  christos 					goto input_failure;
    434   1.1  christos 				nread += n;
    435   1.1  christos 				if (!(flags & SUPPRESS))
    436   1.1  christos 					nassigned++;
    437   1.1  christos 			}
    438   1.1  christos 			nconversions++;
    439   1.1  christos 			break;
    440   1.1  christos 
    441   1.1  christos 		case CT_CCL:
    442   1.1  christos 			/* scan a (nonempty) character class (sets NOSKIP) */
    443   1.1  christos 			if (width == 0)
    444   1.1  christos 				width = (size_t)~0;	/* `infinity' */
    445   1.1  christos 			/* take only those things in the class */
    446   1.1  christos 			if ((flags & SUPPRESS) && (flags & LONG)) {
    447   1.1  christos 				n = 0;
    448   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    449   1.1  christos 				    width-- != 0 && INCCL(wi))
    450   1.1  christos 					n++;
    451   1.1  christos 				if (wi != WEOF)
    452   1.1  christos 					ungetwc(wi, fp);
    453   1.1  christos 				if (n == 0)
    454   1.1  christos 					goto match_failure;
    455   1.1  christos 			} else if (flags & LONG) {
    456   1.1  christos 				p0 = p = va_arg(ap, wchar_t *);
    457   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    458   1.1  christos 				    width-- != 0 && INCCL(wi))
    459   1.1  christos 					*p++ = (wchar_t)wi;
    460   1.1  christos 				if (wi != WEOF)
    461   1.1  christos 					ungetwc(wi, fp);
    462   1.7  christos 				_DIAGASSERT(__type_fit(int, p - p0));
    463   1.7  christos 				n = (int)(p - p0);
    464   1.1  christos 				if (n == 0)
    465   1.1  christos 					goto match_failure;
    466   1.1  christos 				*p = 0;
    467   1.1  christos 				nassigned++;
    468   1.1  christos 			} else {
    469   1.1  christos 				if (!(flags & SUPPRESS))
    470   1.1  christos 					mbp = va_arg(ap, char *);
    471   1.1  christos 				n = 0;
    472   1.1  christos 				mbs = initial;
    473   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    474   1.1  christos 				    width != 0 && INCCL(wi)) {
    475   1.9     joerg 					if (width >= MB_CUR_MAX_L(loc) &&
    476   1.1  christos 					   !(flags & SUPPRESS)) {
    477   1.9     joerg 						nconv = wcrtomb_l(mbp, wi,
    478   1.9     joerg 						    &mbs, loc);
    479   1.1  christos 						if (nconv == (size_t)-1)
    480   1.1  christos 							goto input_failure;
    481   1.1  christos 					} else {
    482   1.9     joerg 						nconv = wcrtomb_l(mbbuf, wi,
    483   1.9     joerg 						    &mbs, loc);
    484   1.1  christos 						if (nconv == (size_t)-1)
    485   1.1  christos 							goto input_failure;
    486   1.1  christos 						if (nconv > width)
    487   1.1  christos 							break;
    488   1.1  christos 						if (!(flags & SUPPRESS))
    489   1.1  christos 							memcpy(mbp, mbbuf,
    490   1.1  christos 							    nconv);
    491   1.1  christos 					}
    492   1.1  christos 					if (!(flags & SUPPRESS))
    493   1.1  christos 						mbp += nconv;
    494   1.1  christos 					width -= nconv;
    495   1.1  christos 					n++;
    496   1.1  christos 				}
    497   1.1  christos 				if (wi != WEOF)
    498   1.1  christos 					ungetwc(wi, fp);
    499   1.1  christos 				if (!(flags & SUPPRESS)) {
    500   1.1  christos 					*mbp = 0;
    501   1.1  christos 					nassigned++;
    502   1.1  christos 				}
    503   1.1  christos 			}
    504   1.1  christos 			nread += n;
    505   1.1  christos 			nconversions++;
    506   1.1  christos 			break;
    507   1.1  christos 
    508   1.1  christos 		case CT_STRING:
    509   1.1  christos 			/* like CCL, but zero-length string OK, & no NOSKIP */
    510   1.1  christos 			if (width == 0)
    511   1.1  christos 				width = (size_t)~0;
    512   1.1  christos 			if ((flags & SUPPRESS) && (flags & LONG)) {
    513   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    514   1.1  christos 				    width-- != 0 &&
    515   1.9     joerg 				    !iswspace_l(wi, loc))
    516   1.1  christos 					nread++;
    517   1.1  christos 				if (wi != WEOF)
    518   1.1  christos 					ungetwc(wi, fp);
    519   1.1  christos 			} else if (flags & LONG) {
    520   1.1  christos 				p0 = p = va_arg(ap, wchar_t *);
    521   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    522   1.1  christos 				    width-- != 0 &&
    523   1.9     joerg 				    !iswspace_l(wi, loc)) {
    524   1.1  christos 					*p++ = (wchar_t)wi;
    525   1.1  christos 					nread++;
    526   1.1  christos 				}
    527   1.1  christos 				if (wi != WEOF)
    528   1.1  christos 					ungetwc(wi, fp);
    529   1.1  christos 				*p = '\0';
    530   1.1  christos 				nassigned++;
    531   1.1  christos 			} else {
    532   1.1  christos 				if (!(flags & SUPPRESS))
    533   1.1  christos 					mbp = va_arg(ap, char *);
    534   1.1  christos 				mbs = initial;
    535   1.1  christos 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
    536   1.1  christos 				    width != 0 &&
    537   1.9     joerg 				    !iswspace_l(wi, loc)) {
    538   1.9     joerg 					if (width >= MB_CUR_MAX_L(loc) &&
    539   1.1  christos 					    !(flags & SUPPRESS)) {
    540   1.9     joerg 						nconv = wcrtomb_l(mbp, wi,
    541   1.9     joerg 						    &mbs, loc);
    542   1.1  christos 						if (nconv == (size_t)-1)
    543   1.1  christos 							goto input_failure;
    544   1.1  christos 					} else {
    545   1.9     joerg 						nconv = wcrtomb_l(mbbuf, wi,
    546   1.9     joerg 						    &mbs, loc);
    547   1.1  christos 						if (nconv == (size_t)-1)
    548   1.1  christos 							goto input_failure;
    549   1.1  christos 						if (nconv > width)
    550   1.1  christos 							break;
    551   1.1  christos 						if (!(flags & SUPPRESS))
    552   1.1  christos 							memcpy(mbp, mbbuf,
    553   1.1  christos 							    nconv);
    554   1.1  christos 					}
    555   1.1  christos 					if (!(flags & SUPPRESS))
    556   1.1  christos 						mbp += nconv;
    557   1.1  christos 					width -= nconv;
    558   1.1  christos 					nread++;
    559   1.1  christos 				}
    560   1.1  christos 				if (wi != WEOF)
    561   1.1  christos 					ungetwc(wi, fp);
    562   1.1  christos 				if (!(flags & SUPPRESS)) {
    563   1.1  christos 					*mbp = 0;
    564   1.1  christos 					nassigned++;
    565   1.1  christos 				}
    566   1.1  christos 			}
    567   1.1  christos 			nconversions++;
    568   1.1  christos 			continue;
    569   1.1  christos 
    570   1.1  christos 		case CT_INT:
    571   1.1  christos 			/* scan an integer as if by the conversion function */
    572   1.1  christos 			if (width == 0 || width > sizeof(buf) /
    573   1.1  christos 			    sizeof(*buf) - 1)
    574   1.1  christos 				width = sizeof(buf) / sizeof(*buf) - 1;
    575   1.1  christos 			flags |= SIGNOK | NDIGITS | NZDIGITS;
    576   1.1  christos 			for (p = buf; width; width--) {
    577   1.1  christos 				c = __fgetwc_unlock(fp);
    578   1.1  christos 				/*
    579   1.1  christos 				 * Switch on the character; `goto ok'
    580   1.1  christos 				 * if we accept it as a part of number.
    581   1.1  christos 				 */
    582   1.1  christos 				switch (c) {
    583   1.1  christos 
    584   1.1  christos 				/*
    585   1.1  christos 				 * The digit 0 is always legal, but is
    586   1.1  christos 				 * special.  For %i conversions, if no
    587   1.1  christos 				 * digits (zero or nonzero) have been
    588   1.1  christos 				 * scanned (only signs), we will have
    589   1.1  christos 				 * base==0.  In that case, we should set
    590   1.1  christos 				 * it to 8 and enable 0x prefixing.
    591   1.1  christos 				 * Also, if we have not scanned zero digits
    592   1.1  christos 				 * before this, do not turn off prefixing
    593   1.1  christos 				 * (someone else will turn it off if we
    594   1.1  christos 				 * have scanned any nonzero digits).
    595   1.1  christos 				 */
    596   1.1  christos 				case '0':
    597   1.1  christos 					if (base == 0) {
    598   1.1  christos 						base = 8;
    599   1.1  christos 						flags |= PFXOK;
    600   1.1  christos 					}
    601   1.1  christos 					if (flags & NZDIGITS)
    602   1.1  christos 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
    603   1.1  christos 					else
    604   1.1  christos 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
    605   1.1  christos 					goto ok;
    606   1.1  christos 
    607   1.1  christos 				/* 1 through 7 always legal */
    608   1.1  christos 				case '1': case '2': case '3':
    609   1.1  christos 				case '4': case '5': case '6': case '7':
    610   1.1  christos 					base = basefix[base];
    611   1.1  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    612   1.1  christos 					goto ok;
    613   1.1  christos 
    614   1.1  christos 				/* digits 8 and 9 ok iff decimal or hex */
    615   1.1  christos 				case '8': case '9':
    616   1.1  christos 					base = basefix[base];
    617   1.1  christos 					if (base <= 8)
    618   1.1  christos 						break;	/* not legal here */
    619   1.1  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    620   1.1  christos 					goto ok;
    621   1.1  christos 
    622   1.1  christos 				/* letters ok iff hex */
    623   1.1  christos 				case 'A': case 'B': case 'C':
    624   1.1  christos 				case 'D': case 'E': case 'F':
    625   1.1  christos 				case 'a': case 'b': case 'c':
    626   1.1  christos 				case 'd': case 'e': case 'f':
    627   1.1  christos 					/* no need to fix base here */
    628   1.1  christos 					if (base <= 10)
    629   1.1  christos 						break;	/* not legal here */
    630   1.1  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    631   1.1  christos 					goto ok;
    632   1.1  christos 
    633   1.1  christos 				/* sign ok only as first character */
    634   1.1  christos 				case '+': case '-':
    635   1.1  christos 					if (flags & SIGNOK) {
    636   1.1  christos 						flags &= ~SIGNOK;
    637   1.1  christos 						flags |= HAVESIGN;
    638   1.1  christos 						goto ok;
    639   1.1  christos 					}
    640   1.1  christos 					break;
    641   1.1  christos 
    642   1.1  christos 				/*
    643   1.1  christos 				 * x ok iff flag still set & 2nd char (or
    644   1.1  christos 				 * 3rd char if we have a sign).
    645   1.1  christos 				 */
    646   1.1  christos 				case 'x': case 'X':
    647   1.1  christos 					if (flags & PFXOK && p ==
    648   1.1  christos 					    buf + 1 + !!(flags & HAVESIGN)) {
    649   1.1  christos 						base = 16;	/* if %i */
    650   1.1  christos 						flags &= ~PFXOK;
    651   1.1  christos 						goto ok;
    652   1.1  christos 					}
    653   1.1  christos 					break;
    654   1.1  christos 				}
    655   1.1  christos 
    656   1.1  christos 				/*
    657   1.1  christos 				 * If we got here, c is not a legal character
    658   1.1  christos 				 * for a number.  Stop accumulating digits.
    659   1.1  christos 				 */
    660   1.1  christos 				if (c != WEOF)
    661   1.1  christos 					ungetwc(c, fp);
    662   1.1  christos 				break;
    663   1.1  christos 		ok:
    664   1.1  christos 				/*
    665   1.1  christos 				 * c is legal: store it and look at the next.
    666   1.1  christos 				 */
    667   1.1  christos 				*p++ = (wchar_t)c;
    668   1.1  christos 			}
    669   1.1  christos 			/*
    670   1.1  christos 			 * If we had only a sign, it is no good; push
    671   1.1  christos 			 * back the sign.  If the number ends in `x',
    672   1.1  christos 			 * it was [sign] '0' 'x', so push back the x
    673   1.1  christos 			 * and treat it as [sign] '0'.
    674   1.1  christos 			 */
    675   1.1  christos 			if (flags & NDIGITS) {
    676   1.1  christos 				if (p > buf)
    677   1.1  christos 					ungetwc(*--p, fp);
    678   1.1  christos 				goto match_failure;
    679   1.1  christos 			}
    680   1.1  christos 			c = p[-1];
    681   1.1  christos 			if (c == 'x' || c == 'X') {
    682   1.1  christos 				--p;
    683   1.1  christos 				ungetwc(c, fp);
    684   1.1  christos 			}
    685   1.1  christos 			if ((flags & SUPPRESS) == 0) {
    686   1.1  christos 				uintmax_t res;
    687   1.1  christos 
    688   1.1  christos 				*p = 0;
    689   1.1  christos 				if ((flags & UNSIGNED) == 0)
    690   1.9     joerg 				    res = wcstoimax_l(buf, NULL, base, loc);
    691   1.1  christos 				else
    692   1.9     joerg 				    res = wcstoumax_l(buf, NULL, base, loc);
    693   1.1  christos 				if (flags & POINTER)
    694   1.1  christos 					*va_arg(ap, void **) =
    695   1.1  christos 							(void *)(uintptr_t)res;
    696   1.1  christos 				else if (flags & SHORTSHORT)
    697   1.1  christos 					*va_arg(ap, char *) = (char)res;
    698   1.1  christos 				else if (flags & SHORT)
    699   1.1  christos 					*va_arg(ap, short *) = (short)res;
    700   1.1  christos 				else if (flags & LONG)
    701   1.1  christos 					*va_arg(ap, long *) = (long)res;
    702   1.1  christos 				else if (flags & LONGLONG)
    703   1.1  christos 					*va_arg(ap, quad_t *) = res;
    704   1.1  christos 				else if (flags & INTMAXT)
    705   1.1  christos 					*va_arg(ap, intmax_t *) = res;
    706   1.1  christos 				else if (flags & PTRDIFFT)
    707   1.1  christos 					*va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
    708   1.1  christos 				else if (flags & SIZET)
    709   1.1  christos 					*va_arg(ap, size_t *) = (size_t)res;
    710   1.1  christos 				else
    711   1.1  christos 					*va_arg(ap, int *) = (int)res;
    712   1.1  christos 				nassigned++;
    713   1.1  christos 			}
    714   1.7  christos 			_DIAGASSERT(__type_fit(int, p - buf));
    715   1.7  christos 			nread += (int)(p - buf);
    716   1.1  christos 			nconversions++;
    717   1.1  christos 			break;
    718   1.1  christos 
    719   1.1  christos #ifndef NO_FLOATING_POINT
    720   1.1  christos 		case CT_FLOAT:
    721   1.1  christos 			/* scan a floating point number as if by strtod */
    722   1.1  christos 			if (width == 0 || width > sizeof(buf) /
    723   1.1  christos 			    sizeof(*buf) - 1)
    724   1.1  christos 				width = sizeof(buf) / sizeof(*buf) - 1;
    725   1.9     joerg 			if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
    726   1.1  christos 				goto match_failure;
    727   1.1  christos 			if ((flags & SUPPRESS) == 0) {
    728   1.1  christos 				if (flags & LONGDBL) {
    729   1.9     joerg 					long double res = wcstold_l(buf, &p,
    730   1.9     joerg 					    loc);
    731   1.1  christos 					*va_arg(ap, long double *) = res;
    732   1.1  christos 				} else
    733   1.1  christos 				if (flags & LONG) {
    734   1.9     joerg 					double res = wcstod_l(buf, &p, loc);
    735   1.1  christos 					*va_arg(ap, double *) = res;
    736   1.1  christos 				} else {
    737   1.9     joerg 					float res = wcstof_l(buf, &p, loc);
    738   1.1  christos 					*va_arg(ap, float *) = res;
    739   1.1  christos 				}
    740   1.1  christos #ifdef DEBUG
    741   1.6  christos 				if (p - buf != (ptrdiff_t)width)
    742   1.1  christos 					abort();
    743   1.1  christos #endif
    744   1.1  christos 				nassigned++;
    745   1.1  christos 			}
    746   1.1  christos 			nread += width;
    747   1.1  christos 			nconversions++;
    748   1.1  christos 			break;
    749   1.1  christos #endif /* !NO_FLOATING_POINT */
    750   1.1  christos 		}
    751   1.1  christos 	}
    752   1.1  christos input_failure:
    753   1.8  christos 	return nconversions != 0 ? nassigned : EOF;
    754   1.1  christos match_failure:
    755   1.8  christos 	return nassigned;
    756   1.1  christos }
    757   1.1  christos 
    758   1.1  christos #ifndef NO_FLOATING_POINT
    759   1.1  christos static int
    760   1.9     joerg parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t loc)
    761   1.1  christos {
    762   1.1  christos 	wchar_t *commit, *p;
    763   1.1  christos 	int infnanpos = 0;
    764   1.1  christos 	enum {
    765   1.1  christos 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
    766   1.1  christos 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
    767   1.1  christos 	} state = S_START;
    768   1.1  christos 	wchar_t c;
    769   1.9     joerg 	wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
    770   1.1  christos 	int gotmantdig = 0, ishex = 0;
    771   1.1  christos 
    772   1.1  christos 	/*
    773   1.1  christos 	 * We set commit = p whenever the string we have read so far
    774   1.1  christos 	 * constitutes a valid representation of a floating point
    775   1.1  christos 	 * number by itself.  At some point, the parse will complete
    776   1.1  christos 	 * or fail, and we will ungetc() back to the last commit point.
    777   1.1  christos 	 * To ensure that the file offset gets updated properly, it is
    778   1.1  christos 	 * always necessary to read at least one character that doesn't
    779   1.1  christos 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
    780   1.1  christos 	 */
    781   1.1  christos 	commit = buf - 1;
    782   1.1  christos 	c = WEOF;
    783   1.1  christos 	for (p = buf; p < end; ) {
    784   1.1  christos 		if ((c = __fgetwc_unlock(fp)) == WEOF)
    785   1.1  christos 			break;
    786   1.1  christos reswitch:
    787   1.1  christos 		switch (state) {
    788   1.1  christos 		case S_START:
    789   1.1  christos 			state = S_GOTSIGN;
    790   1.1  christos 			if (c == '-' || c == '+')
    791   1.1  christos 				break;
    792   1.1  christos 			else
    793   1.1  christos 				goto reswitch;
    794   1.1  christos 		case S_GOTSIGN:
    795   1.1  christos 			switch (c) {
    796   1.1  christos 			case '0':
    797   1.1  christos 				state = S_MAYBEHEX;
    798   1.1  christos 				commit = p;
    799   1.1  christos 				break;
    800   1.1  christos 			case 'I':
    801   1.1  christos 			case 'i':
    802   1.1  christos 				state = S_INF;
    803   1.1  christos 				break;
    804   1.1  christos 			case 'N':
    805   1.1  christos 			case 'n':
    806   1.1  christos 				state = S_NAN;
    807   1.1  christos 				break;
    808   1.1  christos 			default:
    809   1.1  christos 				state = S_DIGITS;
    810   1.1  christos 				goto reswitch;
    811   1.1  christos 			}
    812   1.1  christos 			break;
    813   1.1  christos 		case S_INF:
    814   1.1  christos 			if (infnanpos > 6 ||
    815   1.1  christos 			    (c != "nfinity"[infnanpos] &&
    816   1.1  christos 			     c != "NFINITY"[infnanpos]))
    817   1.1  christos 				goto parsedone;
    818   1.1  christos 			if (infnanpos == 1 || infnanpos == 6)
    819   1.1  christos 				commit = p;	/* inf or infinity */
    820   1.1  christos 			infnanpos++;
    821   1.1  christos 			break;
    822   1.1  christos 		case S_NAN:
    823   1.1  christos 			switch (infnanpos) {
    824   1.1  christos 			case -1:	/* XXX kludge to deal with nan(...) */
    825   1.1  christos 				goto parsedone;
    826   1.1  christos 			case 0:
    827   1.1  christos 				if (c != 'A' && c != 'a')
    828   1.1  christos 					goto parsedone;
    829   1.1  christos 				break;
    830   1.1  christos 			case 1:
    831   1.1  christos 				if (c != 'N' && c != 'n')
    832   1.1  christos 					goto parsedone;
    833   1.1  christos 				else
    834   1.1  christos 					commit = p;
    835   1.1  christos 				break;
    836   1.1  christos 			case 2:
    837   1.1  christos 				if (c != '(')
    838   1.1  christos 					goto parsedone;
    839   1.1  christos 				break;
    840   1.1  christos 			default:
    841   1.1  christos 				if (c == ')') {
    842   1.1  christos 					commit = p;
    843   1.1  christos 					infnanpos = -2;
    844   1.9     joerg 				} else if (!iswalnum_l(c, loc) && c != '_')
    845   1.1  christos 					goto parsedone;
    846   1.1  christos 				break;
    847   1.1  christos 			}
    848   1.1  christos 			infnanpos++;
    849   1.1  christos 			break;
    850   1.1  christos 		case S_MAYBEHEX:
    851   1.1  christos 			state = S_DIGITS;
    852   1.1  christos 			if (c == 'X' || c == 'x') {
    853   1.1  christos 				ishex = 1;
    854   1.1  christos 				break;
    855   1.1  christos 			} else {	/* we saw a '0', but no 'x' */
    856   1.1  christos 				gotmantdig = 1;
    857   1.1  christos 				goto reswitch;
    858   1.1  christos 			}
    859   1.1  christos 		case S_DIGITS:
    860   1.9     joerg 			if ((ishex && iswxdigit_l(c, loc)) ||
    861   1.9     joerg 			    iswdigit_l(c, loc))
    862   1.1  christos 				gotmantdig = 1;
    863   1.1  christos 			else {
    864   1.1  christos 				state = S_FRAC;
    865   1.1  christos 				if (c != decpt)
    866   1.1  christos 					goto reswitch;
    867   1.1  christos 			}
    868   1.1  christos 			if (gotmantdig)
    869   1.1  christos 				commit = p;
    870   1.1  christos 			break;
    871   1.1  christos 		case S_FRAC:
    872   1.1  christos 			if (((c == 'E' || c == 'e') && !ishex) ||
    873   1.1  christos 			    ((c == 'P' || c == 'p') && ishex)) {
    874   1.1  christos 				if (!gotmantdig)
    875   1.1  christos 					goto parsedone;
    876   1.1  christos 				else
    877   1.1  christos 					state = S_EXP;
    878   1.9     joerg 			} else if ((ishex && iswxdigit_l(c, loc)) ||
    879   1.9     joerg 			    iswdigit_l(c, loc)) {
    880   1.1  christos 				commit = p;
    881   1.1  christos 				gotmantdig = 1;
    882   1.1  christos 			} else
    883   1.1  christos 				goto parsedone;
    884   1.1  christos 			break;
    885   1.1  christos 		case S_EXP:
    886   1.1  christos 			state = S_EXPDIGITS;
    887   1.1  christos 			if (c == '-' || c == '+')
    888   1.1  christos 				break;
    889   1.1  christos 			else
    890   1.1  christos 				goto reswitch;
    891   1.1  christos 		case S_EXPDIGITS:
    892   1.9     joerg 			if (iswdigit_l(c, loc))
    893   1.1  christos 				commit = p;
    894   1.1  christos 			else
    895   1.1  christos 				goto parsedone;
    896   1.1  christos 			break;
    897   1.1  christos 		default:
    898   1.1  christos 			abort();
    899   1.1  christos 		}
    900   1.1  christos 		*p++ = c;
    901   1.1  christos 		c = WEOF;
    902   1.1  christos 	}
    903   1.1  christos 
    904   1.1  christos parsedone:
    905   1.1  christos 	if (c != WEOF)
    906   1.1  christos 		ungetwc(c, fp);
    907   1.1  christos 	while (commit < --p)
    908   1.1  christos 		ungetwc(*p, fp);
    909   1.1  christos 	*++commit = '\0';
    910   1.7  christos 	_DIAGASSERT(__type_fit(int, commit - buf));
    911   1.7  christos 	return (int)(commit - buf);
    912   1.1  christos }
    913   1.1  christos #endif
    914