2 .\" Portions Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved .
4 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
7 .\" This notice shall appear on any product containing this material.
8 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
9 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
10 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
11 .TH ICONV 3C "Oct 6, 2004"
13 iconv \- code conversion function
20 \fBextern size_t\fR \fBiconv\fR(\fBiconv_t\fR \fIcd\fR, \fBconst char **restrict\fR \fIinbuf\fR,
21 \fBsize_t *restrict\fR \fIinbytesleft\fR, \fBchar **restrict\fR \fIoutbuf\fR,
22 \fBsize_t *restrict\fR \fIoutbytesleft\fR);
30 \fBsize_t\fR \fBiconv\fR(\fBiconv_t\fR \fIcd\fR, \fBchar **restrict\fR \fIinbuf\fR,
31 \fBsize_t *restrict\fR \fIinbytesleft\fR, \fBchar **restrict\fR \fIoutbuf\fR,
32 \fBsize_t *restrict\fR \fIoutbytesleft\fR);
38 The \fBiconv()\fR function converts the sequence of characters from one code
39 set, in the array specified by \fIinbuf\fR, into a sequence of corresponding
40 characters in another code set, in the array specified by \fIoutbuf\fR. The
41 code sets are those specified in the \fIiconv_open\fR(\|) call that returned
42 the conversion descriptor, \fIcd\fR. The \fIinbuf\fR argument points to a
43 variable that points to the first character in the input buffer and
44 \fIinbytesleft\fR indicates the number of bytes to the end of the buffer to be
45 converted. The \fIoutbuf\fR argument points to a variable that points to the
46 first available byte in the output buffer and \fIoutbytesleft\fR indicates the
47 number of the available bytes to the end of the buffer.
50 For state-dependent encodings, the conversion descriptor \fIcd\fR is placed
51 into its initial shift state by a call for which \fIinbuf\fR is a null pointer,
52 or for which \fIinbuf\fR points to a null pointer. When \fBiconv()\fR is called
53 in this way, and if \fIoutbuf\fR is not a null pointer or a pointer to a null
54 pointer, and \fIoutbytesleft\fR points to a positive value, \fBiconv()\fR will
55 place, into the output buffer, the byte sequence to change the output buffer
56 to its initial shift state. If the output buffer is not large enough to hold
57 the entire reset sequence, \fBiconv()\fR will fail and set \fIerrno\fR to
58 \fBE2BIG\fR. Subsequent calls with \fIinbuf\fR as other than a null pointer or
59 a pointer to a null pointer cause the conversion to take place from the
60 current state of the conversion descriptor.
63 If a sequence of input bytes does not form a valid character in the specified
64 code set, conversion stops after the previous successfully converted
65 character. If the input buffer ends with an incomplete character or shift
66 sequence, conversion stops after the previous successfully converted bytes. If
67 the output buffer is not large enough to hold the entire converted input,
68 conversion stops just prior to the input bytes that would cause the output
69 buffer to overflow. The variable pointed to by \fIinbuf\fR is updated to point
70 to the byte following the last byte successfully used in the conversion. The
71 value pointed to by \fIinbytesleft\fR is decremented to reflect the number of
72 bytes still not converted in the input buffer. The variable pointed to by
73 \fIoutbuf\fR is updated to point to the byte following the last byte of
74 converted output data. The value pointed to by \fIoutbytesleft\fR is
75 decremented to reflect the number of bytes still available in the output
76 buffer. For state-dependent encodings, the conversion descriptor is updated to
77 reflect the shift state in effect at the end of the last successfully
78 converted byte sequence.
81 If \fBiconv()\fR encounters a character in the input buffer that is legal, but
82 for which an identical character does not exist in the target code set,
83 \fBiconv()\fR performs an implementation-defined conversion on this character.
87 The \fBiconv()\fR function updates the variables pointed to by the arguments
88 to reflect the extent of the conversion and returns the number of non-identical
89 conversions performed. If the entire string in the input buffer is converted,
90 the value pointed to by \fIinbytesleft\fR will be \fB0\fR. If the input
91 conversion is stopped due to any conditions mentioned above, the value pointed
92 to by \fIinbytesleft\fR will be non-zero and \fBerrno\fR is set to indicate
93 the condition. If an error occurs \fBiconv()\fR returns \fB(size_t)\fR
94 \fB\(mi1\fR and sets \fIerrno\fR to indicate the error.
98 The \fBiconv()\fR function will fail if:
105 Input conversion stopped due to an input byte that does not belong to the input
115 Input conversion stopped due to lack of space in the output buffer.
124 Input conversion stopped due to an incomplete character or shift sequence at
125 the end of the input buffer.
130 The \fBiconv()\fR function may fail if:
137 The \fIcd\fR argument is not a valid open conversion descriptor.
142 \fBExample 1 \fRUsing the \fBiconv()\fR Functions
145 The following example uses the \fBiconv()\fR functions:
157 * For state-dependent encodings, changes the state of the
158 * conversion descriptor to initial shift state. Also, outputs
159 * the byte sequence to change the state to initial state.
160 * This code is assuming the iconv call for initializing the
161 * state will not fail due to lack of space in the output buffer.
163 #define INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft) \e
169 (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); \e
170 (void) fwrite(to, 1, BUFSIZ - oleft, stdout); \e
174 main(int argc, char **argv)
177 char from[BUFSIZ], to[BUFSIZ];
178 char *from_code, *to_code;
181 size_t ileft, oleft, num, ret;
185 (void) fprintf(stderr,
186 "Usage: %s from_codeset to_codeset\e\en", argv[0]);
193 cd = iconv_open((const char *)to_code, (const char *)from_code);
194 if (cd == (iconv_t)-1) {
198 (void) fprintf(stderr,
199 "iconv_open(%s, %s) failed\e\en", to_code, from_code);
205 (num = fread(from + ileft, 1, BUFSIZ - ileft, stdin))) > 0) {
208 * Input buffer still contains incomplete character
209 * or sequence. However, no more input character.
213 * Initializes the conversion descriptor and outputs
214 * the sequence to change the state to initial state.
216 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
217 (void) iconv_close(cd);
219 (void) fprintf(stderr, "Conversion error\e\en");
228 ret = iconv(cd, &fptr, &ileft, &tptr, &oleft);
229 if (ret != (size_t)-1) {
235 * Outputs converted characters
237 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
244 if (errno == EINVAL) {
246 * Incomplete character or shift sequence
250 * Outputs converted characters
252 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
254 * Copies remaining characters in input buffer
255 * to the top of the input buffer.
257 (void) memmove(from, fptr, ileft);
259 * Tries to fill input buffer from stdin
262 } else if (errno == E2BIG) {
264 * Lack of space in output buffer
268 * Outputs converted characters
270 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
272 * Tries to convert remaining characters in
273 * input buffer with emptied output buffer
276 } else if (errno == EILSEQ) {
278 * Illegal character or shift sequence
282 * Outputs converted characters
284 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
286 * Initializes the conversion descriptor and
287 * outputs the sequence to change the state to
290 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
291 (void) iconv_close(cd);
293 (void) fprintf(stderr,
294 "Illegal character or sequence\e\en");
296 } else if (errno == EBADF) {
298 * Invalid conversion descriptor.
299 * Actually, this shouldn't happen here.
301 (void) fprintf(stderr, "Conversion error\e\en");
305 * This errno is not defined
307 (void) fprintf(stderr, "iconv error\e\en");
314 * Initializes the conversion descriptor and outputs
315 * the sequence to change the state to initial state.
317 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
319 (void) iconv_close(cd);
329 \fB\fB/usr/lib/iconv/*.so\fR\fR
333 conversion modules for 32-bit
339 \fB\fB/usr/lib/iconv/sparcv9/*.so\fR\fR
343 conversion modules for 64-bit sparc
349 \fB\fB/usr/lib/iconv/amd64/*.so\fR\fR
353 conversion modules for 64-bit amd64
359 \fB\fB/usr/lib/iconv/geniconvtbl/binarytables/*.bt\fR\fR
363 conversion binary tables
369 See \fBattributes\fR(5) for descriptions of the following attributes:
377 ATTRIBUTE TYPE ATTRIBUTE VALUE
379 Interface Stability Standard
387 \fBgeniconvtbl\fR(1), \fBiconv\fR(1), \fBiconv_close\fR(3C),
388 \fBiconv_open\fR(3C), \fBgeniconvtbl\fR(4), \fBattributes\fR(5),
389 \fBiconv\fR(5), \fBiconv_unicode\fR(5), \fBstandards\fR(5)