xref: /illumos-gate/usr/src/lib/libc/port/print/vsprintf.c (revision 608eb926e14f4ba4736b2d59e891335f1cba9e1e)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2025 Hans Rosenfeld
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved	*/
30 
31 #include "lint.h"
32 #include <mtlib.h>
33 #include <stdarg.h>
34 #include <values.h>
35 #include <errno.h>
36 #include <synch.h>
37 #include <thread.h>
38 #include <sys/types.h>
39 #include "print.h"
40 #include "libc.h"
41 #include <stdio_ext.h>
42 #include <upanic.h>
43 
44 #ifdef _C89_INTMAX32
45 /*
46  * 32-bit shadow function of vsprintf() and sprintf() are built here.
47  * When using the c89 compiler to build 32-bit applications, the size
48  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
49  * The shadow function uses 32-bit size of intmax_t for %j conversion.
50  * The #pragma redefine_extname in <stdio.h> selects the proper routine
51  * at compile time for the user application.
52  * NOTE: these functions are only available in the 32-bit library.
53  */
54 #pragma redefine_extname vsprintf _vsprintf_c89
55 #pragma redefine_extname sprintf _sprintf_c89
56 #endif
57 
58 int
59 vsprintf(char *string, const char *format, va_list ap)
60 {
61 	ssize_t count;
62 	FILE siop;
63 
64 	/*
65 	 * The dummy FILE * created for vsprintf has the _IOREAD
66 	 * flag set to distinguish it from printf and fprintf
67 	 * invocations. It also has the _IOWRT flag set to indicate
68 	 * it is writable, which is checked later by vfprintf().
69 	 */
70 	siop._flag = _IOWRT | _IOREAD;
71 	siop._cnt = MAXINT;
72 	siop._base = siop._ptr = (unsigned char *)string;
73 
74 	/*
75 	 * Mark the dummy FILE so that no locking is ever done.
76 	 */
77 	if (__fsetlocking(&siop, FSETLOCKING_BYCALLER) == -1)
78 		upanic(NULL, 0);	/* this should never happen */
79 
80 	count = vfprintf(&siop, format, ap);
81 
82 	*siop._ptr = '\0'; /* plant terminating null character */
83 
84 	return ((int)count);
85 }
86 
87 int
88 sprintf(char *string, const char *format, ...)
89 {
90 	int count;
91 	va_list ap;
92 
93 	va_start(ap, format);
94 	count = vsprintf(string, format, ap);
95 	va_end(ap);
96 
97 	return (count);
98 }
99