xref: /illumos-gate/usr/src/lib/libc/port/locale/ungetwc.c (revision 5aec55eb)
1*4297a3b0SGarrett D'Amore /*
2*4297a3b0SGarrett D'Amore  * CDDL HEADER START
3*4297a3b0SGarrett D'Amore  *
4*4297a3b0SGarrett D'Amore  * The contents of this file are subject to the terms of the
5*4297a3b0SGarrett D'Amore  * Common Development and Distribution License, Version 1.0 only
6*4297a3b0SGarrett D'Amore  * (the "License").  You may not use this file except in compliance
7*4297a3b0SGarrett D'Amore  * with the License.
8*4297a3b0SGarrett D'Amore  *
9*4297a3b0SGarrett D'Amore  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*4297a3b0SGarrett D'Amore  * or http://www.opensolaris.org/os/licensing.
11*4297a3b0SGarrett D'Amore  * See the License for the specific language governing permissions
12*4297a3b0SGarrett D'Amore  * and limitations under the License.
13*4297a3b0SGarrett D'Amore  *
14*4297a3b0SGarrett D'Amore  * When distributing Covered Code, include this CDDL HEADER in each
15*4297a3b0SGarrett D'Amore  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*4297a3b0SGarrett D'Amore  * If applicable, add the following below this CDDL HEADER, with the
17*4297a3b0SGarrett D'Amore  * fields enclosed by brackets "[]" replaced with your own identifying
18*4297a3b0SGarrett D'Amore  * information: Portions Copyright [yyyy] [name of copyright owner]
19*4297a3b0SGarrett D'Amore  *
20*4297a3b0SGarrett D'Amore  * CDDL HEADER END
21*4297a3b0SGarrett D'Amore  */
22*4297a3b0SGarrett D'Amore /*
23*4297a3b0SGarrett D'Amore  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*4297a3b0SGarrett D'Amore  * Use is subject to license terms.
25*4297a3b0SGarrett D'Amore  */
26*4297a3b0SGarrett D'Amore 
27*4297a3b0SGarrett D'Amore /*	Copyright (c) 1986 AT&T	*/
28*4297a3b0SGarrett D'Amore /*	  All Rights Reserved  	*/
29*4297a3b0SGarrett D'Amore 
30*4297a3b0SGarrett D'Amore /*
31*4297a3b0SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
32*4297a3b0SGarrett D'Amore  */
33*4297a3b0SGarrett D'Amore 
34*4297a3b0SGarrett D'Amore /*	This module is created for NLS on Sep.03.86		*/
35*4297a3b0SGarrett D'Amore 
36*4297a3b0SGarrett D'Amore /*
37*4297a3b0SGarrett D'Amore  * Ungetwc saves the process code c into the one character buffer
38*4297a3b0SGarrett D'Amore  * associated with an input stream "iop". That character, c,
39*4297a3b0SGarrett D'Amore  * will be returned by the next getwc call on that stream.
40*4297a3b0SGarrett D'Amore  */
41*4297a3b0SGarrett D'Amore 
42*4297a3b0SGarrett D'Amore #include "lint.h"
43*4297a3b0SGarrett D'Amore #include "file64.h"
44*4297a3b0SGarrett D'Amore #include "mse_int.h"
45*4297a3b0SGarrett D'Amore #include <stdio.h>
46*4297a3b0SGarrett D'Amore #include <stdlib.h>
47*4297a3b0SGarrett D'Amore #include <widec.h>
48*4297a3b0SGarrett D'Amore #include <limits.h>
49*4297a3b0SGarrett D'Amore #include <errno.h>
50*4297a3b0SGarrett D'Amore #include "libc.h"
51*4297a3b0SGarrett D'Amore #include "stdiom.h"
52*4297a3b0SGarrett D'Amore #include "mse.h"
53*4297a3b0SGarrett D'Amore 
54*4297a3b0SGarrett D'Amore static wint_t
__ungetwc_impl(wint_t wc,FILE * iop,int orient)55*4297a3b0SGarrett D'Amore __ungetwc_impl(wint_t wc, FILE *iop, int orient)
56*4297a3b0SGarrett D'Amore {
57*4297a3b0SGarrett D'Amore 	char		mbs[MB_LEN_MAX];
58*4297a3b0SGarrett D'Amore 	unsigned char	*p;
59*4297a3b0SGarrett D'Amore 	int		n;
60*4297a3b0SGarrett D'Amore 	rmutex_t	*lk;
61*4297a3b0SGarrett D'Amore 
62*4297a3b0SGarrett D'Amore 	FLOCKFILE(lk, iop);
63*4297a3b0SGarrett D'Amore 
64*4297a3b0SGarrett D'Amore 	if (orient && GET_NO_MODE(iop)) {
65*4297a3b0SGarrett D'Amore 		_setorientation(iop, _WC_MODE);
66*4297a3b0SGarrett D'Amore 	}
67*4297a3b0SGarrett D'Amore 	if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0)) {
68*4297a3b0SGarrett D'Amore 		FUNLOCKFILE(lk);
69*4297a3b0SGarrett D'Amore 		return (WEOF);
70*4297a3b0SGarrett D'Amore 	}
71*4297a3b0SGarrett D'Amore 
72*4297a3b0SGarrett D'Amore 	n = wctomb(mbs, (wchar_t)wc);
73*4297a3b0SGarrett D'Amore 	if (n <= 0) {
74*4297a3b0SGarrett D'Amore 		FUNLOCKFILE(lk);
75*4297a3b0SGarrett D'Amore 		return (WEOF);
76*4297a3b0SGarrett D'Amore 	}
77*4297a3b0SGarrett D'Amore 
78*4297a3b0SGarrett D'Amore 	if (iop->_ptr <= iop->_base) {
79*4297a3b0SGarrett D'Amore 		if (iop->_base == NULL) {
80*4297a3b0SGarrett D'Amore 			FUNLOCKFILE(lk);
81*4297a3b0SGarrett D'Amore 			return (WEOF);
82*4297a3b0SGarrett D'Amore 		}
83*4297a3b0SGarrett D'Amore 		if (iop->_ptr == iop->_base && iop->_cnt == 0) {
84*4297a3b0SGarrett D'Amore 			++iop->_ptr;
85*4297a3b0SGarrett D'Amore 		} else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) {
86*4297a3b0SGarrett D'Amore 			FUNLOCKFILE(lk);
87*4297a3b0SGarrett D'Amore 			return (WEOF);
88*4297a3b0SGarrett D'Amore 		}
89*4297a3b0SGarrett D'Amore 	}
90*4297a3b0SGarrett D'Amore 
91*4297a3b0SGarrett D'Amore 	p = (unsigned char *)(mbs + n - 1);
92*4297a3b0SGarrett D'Amore 	while (n--) {
93*4297a3b0SGarrett D'Amore 		*--(iop)->_ptr = (*p--);
94*4297a3b0SGarrett D'Amore 		++(iop)->_cnt;
95*4297a3b0SGarrett D'Amore 	}
96*4297a3b0SGarrett D'Amore 	iop->_flag &= ~_IOEOF;
97*4297a3b0SGarrett D'Amore 	FUNLOCKFILE(lk);
98*4297a3b0SGarrett D'Amore 	return (wc);
99*4297a3b0SGarrett D'Amore }
100*4297a3b0SGarrett D'Amore 
101*4297a3b0SGarrett D'Amore 
102*4297a3b0SGarrett D'Amore wint_t
__ungetwc_xpg5(wint_t wc,FILE * iop)103*4297a3b0SGarrett D'Amore __ungetwc_xpg5(wint_t wc, FILE *iop)
104*4297a3b0SGarrett D'Amore {
105*4297a3b0SGarrett D'Amore 	return (__ungetwc_impl(wc, iop, 1));
106*4297a3b0SGarrett D'Amore }
107*4297a3b0SGarrett D'Amore 
108*4297a3b0SGarrett D'Amore wint_t
ungetwc(wint_t wc,FILE * iop)109*4297a3b0SGarrett D'Amore ungetwc(wint_t wc, FILE *iop)
110*4297a3b0SGarrett D'Amore {
111*4297a3b0SGarrett D'Amore 	return (__ungetwc_impl(wc, iop, 0));
112*4297a3b0SGarrett D'Amore }
113