xref: /titanic_50/usr/src/lib/libc/port/print/vsprintf.c (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
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) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #include "synonyms.h"
34 #include <mtlib.h>
35 #include <stdarg.h>
36 #include <values.h>
37 #include <errno.h>
38 #include <synch.h>
39 #include <thread.h>
40 #include <sys/types.h>
41 #include "print.h"
42 #include "libc.h"
43 
44 /*
45  * 32-bit shadow function of vsprintf() is included here.
46  * When using the c89 compiler to build 32-bit applications, the size
47  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
48  * The shadow function uses 32-bit size of intmax_t for %j conversion.
49  * The #pragma redefine_extname in <stdio.h> selects the proper routine
50  * at compile time for the user application.
51  * NOTE: this function is only available in the 32-bit library.
52  */
53 
54 /*VARARGS2*/
55 int
56 #ifdef _C89_INTMAX32
57 _vsprintf_c89(char *string, const char *format, va_list ap)
58 #else
59 vsprintf(char *string, const char *format, va_list ap)
60 #endif
61 {
62 	ssize_t count;
63 	FILE siop;
64 
65 	siop._cnt = MAXINT;
66 	siop._base = siop._ptr = (unsigned char *)string;
67 	siop._flag = _IOREAD; /* distinguish dummy file descriptor */
68 #ifdef _C89_INTMAX32
69 	count = _ndoprnt(format, ap, &siop, _F_INTMAX32);
70 #else
71 	count = _ndoprnt(format, ap, &siop, 0);
72 #endif
73 	*siop._ptr = '\0'; /* plant terminating null character */
74 
75 	if (count == EOF) {
76 		return (EOF);
77 	}
78 	/* check for overflow */
79 	if ((size_t)count > MAXINT) {
80 		errno = EOVERFLOW;
81 		return (EOF);
82 	} else {
83 		return ((int)count);
84 	}
85 }
86