6429 SMB domain join doesn't work with libreSSL
[unleashed.git] / usr / src / lib / libcurses / screen / mbtranslate.c
blob13a1947c532b29f80cf101656c0a0405691c425b
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
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1997, by Sun Microsystems, Inc.
28 * All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include "curses_inc.h"
39 * Translate process code to byte-equivalent
40 * Return the length of the byte-equivalent string
44 * use _curs_wctomb() instead of _code2byte(code, bytes)
49 * Translate a set of byte to a single process code
53 * use _curs_mbtowc() instead of wchar_t _byte2code(bytes)
58 * Translate a string of wchar_t to a byte string.
59 * code: the input code string
60 * byte: if not NULL, space to store the output string
61 * n: maximum number of codes to be translated.
63 char
64 *_strcode2byte(wchar_t *code, char *byte, int n)
66 char *bufp;
67 wchar_t *endcode;
68 static char *buf;
69 static int bufsize;
71 /* compute the length of the code string */
72 if (n < 0)
73 for (n = 0; code[n] != 0; ++n)
76 /* get space to store the translated string */
77 if (!byte && (n*CSMAX+1) > bufsize) {
78 if (buf)
79 free(buf);
80 bufsize = n * CSMAX + 1;
81 if ((buf = malloc(bufsize * sizeof (char))) == NULL)
82 bufsize = 0;
85 /* no space to do it */
86 if (!byte && !buf)
87 return (NULL);
89 /* start the translation */
90 bufp = byte ? byte : buf;
91 endcode = code+n;
92 while (code < endcode && *code) {
93 bufp += _curs_wctomb(bufp, *code & TRIM);
94 ++code;
96 *bufp = '\0';
98 return (byte ? byte : buf);
104 * Translate a byte-string to a wchar_t string.
106 wchar_t
107 *_strbyte2code(char *byte, wchar_t *code, int n)
109 char *endbyte;
110 wchar_t *bufp;
111 static wchar_t *buf;
112 static int bufsize;
114 if (n < 0)
115 for (n = 0; byte[n] != '\0'; ++n)
118 if (!code && (n + 1) > bufsize) {
119 if (buf)
120 free((char *)buf);
121 bufsize = n + 1;
122 if ((buf = (wchar_t *)malloc(bufsize * sizeof (wchar_t))) ==
123 NULL)
124 bufsize = 0;
127 if (!code && !buf)
128 return (NULL);
130 bufp = code ? code : buf;
131 endbyte = byte + n;
133 while (byte < endbyte && *byte) {
134 int type, width;
135 wchar_t wchar;
137 type = TYPE(*byte & 0377);
138 width = cswidth[type];
139 if (type == 1 || type == 2)
140 width++;
142 if (byte + width <= endbyte) {
143 (void) _curs_mbtowc(&wchar, byte, width);
144 *bufp++ = wchar;
147 byte += width;
149 *bufp = 0;
151 return (code ? code : buf);