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 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* Copyright (c) 1986 AT&T */ 30 /* All Rights Reserved */ 31 32 33 #include "lint.h" 34 #include <stdio.h> 35 #include <stdarg.h> 36 #include <stdlib.h> 37 #include <widec.h> 38 #include <string.h> 39 #include <limits.h> 40 41 /* 42 * wsprintf -- this function will output a wchar_t string 43 * according to the conversion format. 44 * Note that the maximum length of the output 45 * string is 1024 bytes. 46 */ 47 48 /*VARARGS2*/ 49 int 50 wsprintf(wchar_t *wstring, const char *format, ...) 51 { 52 va_list ap; 53 char tempstring[1024]; 54 char *p2; 55 size_t len; 56 int malloced = 0; 57 char *p1 = (char *)wstring; 58 int retcode; 59 int i; 60 61 va_start(ap, format); 62 if (vsprintf(p1, format, ap) == -1) { 63 va_end(ap); 64 return (-1); 65 } 66 va_end(ap); 67 len = strlen(p1) + 1; 68 if (len > 1024) { 69 p2 = malloc(len); 70 if (p2 == NULL) 71 return (-1); 72 malloced = 1; 73 } else 74 p2 = tempstring; 75 (void) strcpy(p2, p1); 76 77 if (mbstowcs(wstring, p2, len) == (size_t)-1) { 78 for (i = 0; i < len; i++) { 79 if ((retcode = mbtowc(wstring, p2, MB_CUR_MAX)) == -1) { 80 *wstring = (wchar_t)*p2 & 0xff; 81 p2++; 82 } else { 83 p2 += retcode; 84 } 85 if (*wstring++ == (wchar_t)0) { 86 break; 87 } 88 } 89 } 90 91 if (malloced == 1) 92 free(p2); 93 len = wcslen(wstring); 94 if (len <= INT_MAX) 95 return ((int)len); 96 else 97 return (EOF); 98 } 99