xref: /onnv-gate/usr/src/lib/libc/port/i18n/__ungetwc_xpg5.c (revision 0:68f95e015346)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1986 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*	This module is created for NLS on Sep.03.86		*/
34 
35 /*
36  * Ungetwc saves the process code c into the one character buffer
37  * associated with an input stream "iop". That character, c,
38  * will be returned by the next getwc call on that stream.
39  */
40 
41 #include "lint.h"
42 #include "file64.h"
43 #include "mse_int.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <widec.h>
47 #include <limits.h>
48 #include <errno.h>
49 #include "libc.h"
50 #include "stdiom.h"
51 #include "mse.h"
52 
53 wint_t
__ungetwc_xpg5(wint_t wc,FILE * iop)54 __ungetwc_xpg5(wint_t wc, FILE *iop)
55 {
56 	char	mbs[MB_LEN_MAX];
57 	unsigned char	*p;
58 	int	n;
59 	void	*lc;
60 	int	(*fp_wctomb)(void *, char *, wchar_t);
61 	rmutex_t	*lk;
62 
63 	FLOCKFILE(lk, iop);
64 
65 	if (_set_orientation_wide(iop, &lc,
66 	    (void (*(*))(void))&fp_wctomb, FP_WCTOMB) == -1) {
67 		errno = EBADF;
68 		FUNLOCKFILE(lk);
69 		return (WEOF);
70 	}
71 
72 	if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0)) {
73 		FUNLOCKFILE(lk);
74 		return (WEOF);
75 	}
76 
77 	n = fp_wctomb(lc, mbs, (wchar_t)wc);
78 	if (n <= 0) {
79 		FUNLOCKFILE(lk);
80 		return (WEOF);
81 	}
82 
83 	if (iop->_ptr <= iop->_base) {
84 		if (iop->_base == NULL) {
85 			FUNLOCKFILE(lk);
86 			return (WEOF);
87 		}
88 		if (iop->_ptr == iop->_base && iop->_cnt == 0) {
89 			++iop->_ptr;
90 		} else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) {
91 			FUNLOCKFILE(lk);
92 			return (WEOF);
93 		}
94 	}
95 
96 	p = (unsigned char *)(mbs + n - 1);
97 	while (n--) {
98 		*--(iop)->_ptr = (*p--);
99 		++(iop)->_cnt;
100 	}
101 	iop->_flag &= ~_IOEOF;
102 	FUNLOCKFILE(lk);
103 	return (wc);
104 }
105