xref: /titanic_50/usr/src/cmd/mdb/common/mdb/mdb_stdlib.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 #include <mdb/mdb_stdlib.h>
30 #include <mdb/mdb_string.h>
31 #include <mdb/mdb_modapi.h>
32 #include <mdb/mdb_debug.h>
33 
34 #include <sys/types.h>
35 #include <floatingpoint.h>
36 #include <poll.h>
37 
38 /*
39  * Post-processing routine for econvert and qeconvert.  This function is
40  * called by both doubletos() and longdoubletos() below.
41  */
42 static const char *
43 fptos(const char *p, char *buf, size_t buflen, int decpt, int sign, char expchr)
44 {
45 	char *q = buf;
46 
47 	*q++ = sign ? '-' : '+';
48 
49 	/*
50 	 * If the initial character is not a digit, the result is a special
51 	 * identifier such as "NaN" or "Inf"; just copy it verbatim.
52 	 */
53 	if (*p < '0' || *p > '9') {
54 		(void) strncpy(q, p, buflen);
55 		buf[buflen - 1] = '\0';
56 		return (buf);
57 	}
58 
59 	*q++ = *p++;
60 	*q++ = '.';
61 
62 	(void) strcpy(q, p);
63 	q += strlen(q);
64 	*q++ = expchr;
65 
66 	if (--decpt < 0) {
67 		decpt = -decpt;
68 		*q++ = '-';
69 	} else
70 		*q++ = '+';
71 
72 	if (decpt < 10)
73 		*q++ = '0';
74 
75 	(void) strcpy(q, numtostr((uint_t)decpt, 10, 0));
76 	return (buf);
77 }
78 
79 /*
80  * Convert the specified double to a string, and return a pointer to a static
81  * buffer containing the string value.  The double is converted using the
82  * same formatting conventions as sprintf(buf, "%+.*e", precision, d).  The
83  * expchr parameter specifies the character used to denote the exponent,
84  * and is usually 'e' or 'E'.
85  */
86 const char *
87 doubletos(double d, int precision, char expchr)
88 {
89 	static char buf[DECIMAL_STRING_LENGTH];
90 	char digits[DECIMAL_STRING_LENGTH];
91 	int decpt, sign;
92 	char *p;
93 
94 	p = econvert(d, precision + 1, &decpt, &sign, digits);
95 	return (fptos(p, buf, sizeof (buf), decpt, sign, expchr));
96 }
97 
98 /*
99  * Same as doubletos(), but for long doubles (quad precision floating point).
100  */
101 const char *
102 longdoubletos(long double *ldp, int precision, char expchr)
103 {
104 	static char buf[DECIMAL_STRING_LENGTH];
105 	char digits[DECIMAL_STRING_LENGTH];
106 	int decpt, sign;
107 	char *p;
108 
109 	p = qeconvert(ldp, precision + 1, &decpt, &sign, digits);
110 	return (fptos(p, buf, sizeof (buf), decpt, sign, expchr));
111 }
112