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