summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/ecvt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/ecvt.c')
-rw-r--r--src/lib/libc/stdlib/ecvt.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c
new file mode 100644
index 0000000000..719370a8f3
--- /dev/null
+++ b/src/lib/libc/stdlib/ecvt.c
@@ -0,0 +1,105 @@
1/* $OpenBSD: ecvt.c,v 1.6 2006/10/29 18:45:56 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2002, 2006 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27extern char *__dtoa(double, int, int, int *, int *, char **);
28extern void __freedtoa(char *);
29static char *__cvt(double, int, int *, int *, int, int);
30
31static char *
32__cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad)
33{
34 static char *s;
35 char *p, *rve, c;
36 size_t siz;
37
38 if (ndigit == 0) {
39 *sign = value < 0.0;
40 *decpt = 0;
41 return ("");
42 }
43
44 if (s) {
45 free(s);
46 s = NULL;
47 }
48
49 if (ndigit < 0)
50 siz = -ndigit + 1;
51 else
52 siz = ndigit + 1;
53
54
55 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
56 if (value == 0.0) {
57 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
58 *sign = 0;
59 if ((rve = s = (char *)malloc(siz)) == NULL)
60 return(NULL);
61 *rve++ = '0';
62 *rve = '\0';
63 } else {
64 p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
65 if (*decpt == 9999) {
66 /* Infinity or Nan, convert to inf or nan like printf */
67 *decpt = 0;
68 c = *p;
69 __freedtoa(p);
70 return(c == 'I' ? "inf" : "nan");
71 }
72 /* Make a local copy and adjust rve to be in terms of s */
73 if (pad && fmode)
74 siz += *decpt;
75 if ((s = (char *)malloc(siz)) == NULL) {
76 __freedtoa(p);
77 return(NULL);
78 }
79 (void) strlcpy(s, p, siz);
80 rve = s + (rve - p);
81 __freedtoa(p);
82 }
83
84 /* Add trailing zeros */
85 if (pad) {
86 siz -= rve - s;
87 while (--siz)
88 *rve++ = '0';
89 *rve = '\0';
90 }
91
92 return(s);
93}
94
95char *
96ecvt(double value, int ndigit, int *decpt, int *sign)
97{
98 return(__cvt(value, ndigit, decpt, sign, 0, 1));
99}
100
101char *
102fcvt(double value, int ndigit, int *decpt, int *sign)
103{
104 return(__cvt(value, ndigit, decpt, sign, 1, 1));
105}