xref: /titanic_50/usr/src/lib/libc/port/print/sprintf.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
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 <thread.h>
39 #include <synch.h>
40 #include <sys/types.h>
41 #include "print.h"
42 #include "libc.h"
43 
44 /*VARARGS2*/
45 int
46 sprintf(char *string, const char *format, ...)
47 {
48 	ssize_t count;
49 	FILE siop;
50 	va_list ap;
51 
52 	siop._cnt = MAXINT;
53 	siop._base = siop._ptr = (unsigned char *)string;
54 	siop._flag = _IOREAD; /* distinguish dummy file descriptor */
55 	va_start(ap, format);
56 
57 	count = _ndoprnt(format, ap, &siop, 0);
58 	va_end(ap);
59 	*siop._ptr = '\0'; /* plant terminating null character */
60 	if (count == EOF) {
61 		return (EOF);
62 	}
63 
64 	/* check for overflow */
65 	if ((size_t)count > MAXINT) {
66 		errno = EOVERFLOW;
67 		return (EOF);
68 	} else {
69 		return ((int)count);
70 	}
71 }
72 
73 #ifndef _LP64
74 
75 /*
76  * 32-bit shadow function of sprintf().
77  * When using the c89 compiler to build 32-bit applications, the size
78  * of intmax_t is 32-bits, otherwise the size of intmax_t is 64-bits.
79  * The shadow function uses 32-bit size of intmax_t for %j conversion.
80  * The #pragma redefine_extname in <stdio.h> selects the proper routine
81  * at compile time for the user application.
82  * NOTE: this function is only available in the 32-bit library.
83  */
84 
85 int
86 _sprintf_c89(char *string, const char *format, ...)
87 {
88 	ssize_t count;
89 	va_list ap;
90 
91 	va_start(ap, format);
92 	count = _vsprintf_c89(string, format, ap);
93 	va_end(ap);
94 	return ((int)count);
95 }
96 
97 #endif	/* _LP64 */
98