5158 sed dumps core in new multibyte code
[illumos-gate.git] / usr / src / lib / libc / port / locale / ungetwc.c
blob66cc332704161a0afdd530fad5d26697f2f79d63
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1986 AT&T */
28 /* All Rights Reserved */
31 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
34 /* This module is created for NLS on Sep.03.86 */
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.
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"
54 static wint_t
55 __ungetwc_impl(wint_t wc, FILE *iop, int orient)
57 char mbs[MB_LEN_MAX];
58 unsigned char *p;
59 int n;
60 rmutex_t *lk;
62 FLOCKFILE(lk, iop);
64 if (orient && GET_NO_MODE(iop)) {
65 _setorientation(iop, _WC_MODE);
67 if ((wc == WEOF) || ((iop->_flag & _IOREAD) == 0)) {
68 FUNLOCKFILE(lk);
69 return (WEOF);
72 n = wctomb(mbs, (wchar_t)wc);
73 if (n <= 0) {
74 FUNLOCKFILE(lk);
75 return (WEOF);
78 if (iop->_ptr <= iop->_base) {
79 if (iop->_base == NULL) {
80 FUNLOCKFILE(lk);
81 return (WEOF);
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);
91 p = (unsigned char *)(mbs + n - 1);
92 while (n--) {
93 *--(iop)->_ptr = (*p--);
94 ++(iop)->_cnt;
96 iop->_flag &= ~_IOEOF;
97 FUNLOCKFILE(lk);
98 return (wc);
102 wint_t
103 __ungetwc_xpg5(wint_t wc, FILE *iop)
105 return (__ungetwc_impl(wc, iop, 1));
108 wint_t
109 ungetwc(wint_t wc, FILE *iop)
111 return (__ungetwc_impl(wc, iop, 0));