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 * Use is subject to license terms. 33 */ 34 35 /* This module is created for NLS on Sep.03.86 */ 36 37 /* 38 * Ungetwc saves the process code c into the one character buffer 39 * associated with an input stream "iop". That character, c, 40 * will be returned by the next getwc call on that stream. 41 */ 42 43 #include "lint.h" 44 #include "file64.h" 45 #include "mse_int.h" 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <widec.h> 49 #include <limits.h> 50 #include <errno.h> 51 #include "libc.h" 52 #include "stdiom.h" 53 #include "mse.h" 54 55 static wint_t 56 __ungetwc_impl(wint_t wc, FILE *iop, int orient) 57 { 58 char mbs[MB_LEN_MAX]; 59 unsigned char *p; 60 int n; 61 rmutex_t *lk; 62 63 FLOCKFILE(lk, iop); 64 65 if (orient && GET_NO_MODE(iop)) { 66 _setorientation(iop, _WC_MODE); 67 } 68 if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0)) { 69 FUNLOCKFILE(lk); 70 return (WEOF); 71 } 72 73 n = wctomb(mbs, (wchar_t)wc); 74 if (n <= 0) { 75 FUNLOCKFILE(lk); 76 return (WEOF); 77 } 78 79 if (iop->_ptr <= iop->_base) { 80 if (iop->_base == NULL) { 81 FUNLOCKFILE(lk); 82 return (WEOF); 83 } 84 if (iop->_ptr == iop->_base && iop->_cnt == 0) { 85 ++iop->_ptr; 86 } else if ((iop->_ptr - n) < (iop->_base - PUSHBACK)) { 87 FUNLOCKFILE(lk); 88 return (WEOF); 89 } 90 } 91 92 p = (unsigned char *)(mbs + n - 1); 93 while (n--) { 94 *--(iop)->_ptr = (*p--); 95 ++(iop)->_cnt; 96 } 97 iop->_flag &= ~_IOEOF; 98 FUNLOCKFILE(lk); 99 return (wc); 100 } 101 102 103 wint_t 104 __ungetwc_xpg5(wint_t wc, FILE *iop) 105 { 106 return (__ungetwc_impl(wc, iop, 1)); 107 } 108 109 wint_t 110 ungetwc(wint_t wc, FILE *iop) 111 { 112 return (__ungetwc_impl(wc, iop, 0)); 113 } 114