Skip Navigation Links | |
Exit Print View | |
man pages section 3: Basic Library Functions Oracle Solaris 11.1 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- code conversion function
#include <iconv.h> size_t iconv(iconv_t cd, char **restrict inbuf, size_t *restrict inbytesleft, char **restrict outbuf, size_t *restrict outbytesleft);
The iconv() function converts the sequence of characters from one code set, in the array specified by inbuf, into a sequence of corresponding characters in another code set, in the array specified by outbuf. The code sets are those specified in the iconv_open( ) call that returned the conversion descriptor, cd. The inbuf argument points to a variable that points to the first character in the input buffer and inbytesleft indicates the number of bytes to the end of the buffer to be converted. The outbuf argument points to a variable that points to the first available byte in the output buffer and outbytesleft indicates the number of the available bytes to the end of the buffer.
For state-dependent encodings, the conversion descriptor cd is placed into its initial shift state by a call for which inbuf is a null pointer, or for which inbuf points to a null pointer. When iconv() is called in this way, and if outbuf is not a null pointer or a pointer to a null pointer, and outbytesleft points to a positive value, iconv() will place, into the output buffer, the byte sequence to change the output buffer to its initial shift state. If the output buffer is not large enough to hold the entire reset sequence, iconv() will fail and set errno to E2BIG. Subsequent calls with inbuf as other than a null pointer or a pointer to a null pointer cause the conversion to take place from the current state of the conversion descriptor.
If a sequence of input bytes does not form a valid character in the specified code set, conversion stops after the previous successfully converted character. If the input buffer ends with an incomplete character or shift sequence, conversion stops after the previous successfully converted bytes. If the output buffer is not large enough to hold the entire converted input, conversion stops just prior to the input bytes that would cause the output buffer to overflow. The variable pointed to by inbuf is updated to point to the byte following the last byte successfully used in the conversion. The value pointed to by inbytesleft is decremented to reflect the number of bytes still not converted in the input buffer. The variable pointed to by outbuf is updated to point to the byte following the last byte of converted output data. The value pointed to by outbytesleft is decremented to reflect the number of bytes still available in the output buffer. For state-dependent encodings, the conversion descriptor is updated to reflect the shift state in effect at the end of the last successfully converted byte sequence.
If iconv() encounters a character in the input buffer that is legal, but for which an identical character does not exist in the target code set, iconv() performs an implementation-defined conversion on this character.
The default conversion behavior mentioned above can be modified if one or more of the conversion behavior modification indicators is specified and such conversion behavior modifications are supported by the implementation of the corresponding iconv code conversion. See iconv_open(3C) and iconvctl(C).
The iconv() function updates the variables pointed to by the arguments to reflect the extent of the conversion and returns the number of non-identical conversions performed. If the entire string in the input buffer is converted, the value pointed to by inbytesleft will be 0. If the input conversion is stopped due to any conditions mentioned above, the value pointed to by inbytesleft will be non-zero and errno is set to indicate the condition. If an error occurs iconv() returns (size_t) -1 and sets errno to indicate the error.
The iconv() function will fail if:
Input conversion stopped due to an input byte that does not belong to the input code set.
Input conversion stopped due to lack of space in the output buffer.
Input conversion stopped due to an incomplete character or shift sequence at the end of the input buffer.
The iconv() function may fail if:
The cd argument is not a valid open conversion descriptor.
Example 1 Using the iconv() Functions
The following example uses the iconv() functions:
#include <stdio.h> #include <errno.h> #include <string.h> #include <iconv.h> #include <stdlib.h> /* * For state-dependent encodings, changes the state of the * conversion descriptor to initial shift state. Also, outputs * the byte sequence to change the state to initial state. * This code is assuming the iconv call for initializing the * state will not fail due to lack of space in the output buffer. */ #define INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft) \ { \ fptr = NULL; \ ileft = 0; \ tptr = to; \ oleft = BUFSIZ; \ (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); \ (void) fwrite(to, 1, BUFSIZ - oleft, stdout); \ } int main(int argc, char **argv) { iconv_t cd; char from[BUFSIZ], to[BUFSIZ]; char *from_code, *to_code; char *tptr; char *fptr; size_t ileft, oleft, num, ret; if (argc != 3) { (void) fprintf(stderr, "Usage: %s from_codeset to_codeset\\n", argv[0]); return (1); } from_code = argv[1]; to_code = argv[2]; cd = iconv_open((const char *)to_code, (const char *)from_code); if (cd == (iconv_t)-1) { /* * iconv_open failed */ (void) fprintf(stderr, "iconv_open(%s, %s) failed\\n", to_code, from_code); return (1); } ileft = 0; while ((ileft += (num = fread(from + ileft, 1, BUFSIZ - ileft, stdin))) > 0) { if (num == 0) { /* * Input buffer still contains incomplete character * or sequence. However, no more input character. */ /* * Initializes the conversion descriptor and outputs * the sequence to change the state to initial state. */ INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft); (void) iconv_close(cd); (void) fprintf(stderr, "Conversion error\\n"); return (1); } fptr = from; for (;;) { tptr = to; oleft = BUFSIZ; ret = iconv(cd, &fptr, &ileft, &tptr, &oleft); if (ret != (size_t)-1) { /* * iconv succeeded */ /* * Outputs converted characters */ (void) fwrite(to, 1, BUFSIZ - oleft, stdout); break; } /* * iconv failed */ if (errno == EINVAL) { /* * Incomplete character or shift sequence */ /* * Outputs converted characters */ (void) fwrite(to, 1, BUFSIZ - oleft, stdout); /* * Copies remaining characters in input buffer * to the top of the input buffer. */ (void) memmove(from, fptr, ileft); /* * Tries to fill input buffer from stdin */ break; } else if (errno == E2BIG) { /* * Lack of space in output buffer */ /* * Outputs converted characters */ (void) fwrite(to, 1, BUFSIZ - oleft, stdout); /* * Tries to convert remaining characters in * input buffer with emptied output buffer */ continue; } else if (errno == EILSEQ) { /* * Illegal character or shift sequence */ /* * Outputs converted characters */ (void) fwrite(to, 1, BUFSIZ - oleft, stdout); /* * Initializes the conversion descriptor and * outputs the sequence to change the state to * initial state. */ INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft); (void) iconv_close(cd); (void) fprintf(stderr, "Illegal character or sequence\\n"); return (1); } else if (errno == EBADF) { /* * Invalid conversion descriptor. * Actually, this shouldn't happen here. */ (void) fprintf(stderr, "Conversion error\\n"); return (1); } else { /* * This errno is not defined */ (void) fprintf(stderr, "iconv error\\n"); return (1); } } } /* * Initializes the conversion descriptor and outputs * the sequence to change the state to initial state. */ INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft); (void) iconv_close(cd); return (0); }
conversion modules for 32-bit
conversion modules for 64-bit sparc
conversion modules for 64-bit amd64
conversion binary tables
See attributes(5) for descriptions of the following attributes:
|
geniconvtbl(1), iconv(1), iconv_close(3C), iconv_open(3C), iconvctl(C), iconvstr(3C), geniconvtbl(4), attributes(5), iconv(5), iconv_unicode(5), standards(5)