xref: /titanic_50/usr/src/lib/libc/port/i18n/wsprintf.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21e8031f0aSraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1986 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
32*7257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdarg.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <widec.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <limits.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * 	wsprintf -- this function will output a wchar_t string
427c478bd9Sstevel@tonic-gate  *		    according to the conversion format.
437c478bd9Sstevel@tonic-gate  *		    Note that the maximum length of the output
447c478bd9Sstevel@tonic-gate  *		    string is 1024 bytes.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*VARARGS2*/
487c478bd9Sstevel@tonic-gate int
wsprintf(wchar_t * wstring,const char * format,...)497c478bd9Sstevel@tonic-gate wsprintf(wchar_t *wstring, const char *format, ...)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	va_list	ap;
527c478bd9Sstevel@tonic-gate 	char	tempstring[1024];
537c478bd9Sstevel@tonic-gate 	char *p2;
547c478bd9Sstevel@tonic-gate 	size_t len;
557c478bd9Sstevel@tonic-gate 	int malloced = 0;
567c478bd9Sstevel@tonic-gate 	char *p1 = (char *)wstring;
577c478bd9Sstevel@tonic-gate 	int retcode;
587c478bd9Sstevel@tonic-gate 	int	i;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	va_start(ap, format);
617c478bd9Sstevel@tonic-gate 	if (vsprintf(p1, format, ap) == -1) {
627c478bd9Sstevel@tonic-gate 		va_end(ap);
637c478bd9Sstevel@tonic-gate 		return (-1);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 	va_end(ap);
667c478bd9Sstevel@tonic-gate 	len = strlen(p1) + 1;
677c478bd9Sstevel@tonic-gate 	if (len > 1024) {
687c478bd9Sstevel@tonic-gate 		p2 = malloc(len);
697c478bd9Sstevel@tonic-gate 		if (p2 == NULL)
707c478bd9Sstevel@tonic-gate 			return (-1);
717c478bd9Sstevel@tonic-gate 		malloced = 1;
727c478bd9Sstevel@tonic-gate 	} else
737c478bd9Sstevel@tonic-gate 		p2 = tempstring;
747c478bd9Sstevel@tonic-gate 	(void) strcpy(p2, p1);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (mbstowcs(wstring, p2, len) == (size_t)-1) {
777c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
787c478bd9Sstevel@tonic-gate 			if ((retcode = mbtowc(wstring, p2, MB_CUR_MAX)) == -1) {
797c478bd9Sstevel@tonic-gate 				*wstring = (wchar_t)*p2 & 0xff;
807c478bd9Sstevel@tonic-gate 				p2++;
817c478bd9Sstevel@tonic-gate 			} else {
827c478bd9Sstevel@tonic-gate 				p2 += retcode;
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 			if (*wstring++ == (wchar_t)0) {
857c478bd9Sstevel@tonic-gate 				break;
867c478bd9Sstevel@tonic-gate 			}
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (malloced == 1)
917c478bd9Sstevel@tonic-gate 		free(p2);
927c478bd9Sstevel@tonic-gate 	len = wcslen(wstring);
937c478bd9Sstevel@tonic-gate 	if (len <= INT_MAX)
947c478bd9Sstevel@tonic-gate 		return ((int)len);
957c478bd9Sstevel@tonic-gate 	else
967c478bd9Sstevel@tonic-gate 		return (EOF);
977c478bd9Sstevel@tonic-gate }
98