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.c106
1 files changed, 106 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..4562e309e8
--- /dev/null
+++ b/src/lib/libc/stdlib/ecvt.c
@@ -0,0 +1,106 @@
1/* $OpenBSD: ecvt.c,v 1.8 2013/11/01 19:05:11 guenther 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#include "gdtoa.h"
27
28static char *__cvt(double, int, int *, int *, int, int);
29
30static char *
31__cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad)
32{
33 static char *s;
34 char *p, *rve, c;
35 size_t siz;
36
37 if (ndigit == 0) {
38 *sign = value < 0.0;
39 *decpt = 0;
40 return ("");
41 }
42
43 if (s) {
44 free(s);
45 s = NULL;
46 }
47
48 if (ndigit < 0)
49 siz = -ndigit + 1;
50 else
51 siz = ndigit + 1;
52
53
54 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
55 if (value == 0.0) {
56 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
57 *sign = 0;
58 if ((rve = s = (char *)malloc(siz)) == NULL)
59 return(NULL);
60 *rve++ = '0';
61 *rve = '\0';
62 } else {
63 p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
64 if (p == NULL)
65 return (NULL);
66 if (*decpt == 9999) {
67 /* Infinity or Nan, convert to inf or nan like printf */
68 *decpt = 0;
69 c = *p;
70 __freedtoa(p);
71 return(c == 'I' ? "inf" : "nan");
72 }
73 /* Make a local copy and adjust rve to be in terms of s */
74 if (pad && fmode)
75 siz += *decpt;
76 if ((s = (char *)malloc(siz)) == NULL) {
77 __freedtoa(p);
78 return(NULL);
79 }
80 (void) strlcpy(s, p, siz);
81 rve = s + (rve - p);
82 __freedtoa(p);
83 }
84
85 /* Add trailing zeros */
86 if (pad) {
87 siz -= rve - s;
88 while (--siz)
89 *rve++ = '0';
90 *rve = '\0';
91 }
92
93 return(s);
94}
95
96char *
97ecvt(double value, int ndigit, int *decpt, int *sign)
98{
99 return(__cvt(value, ndigit, decpt, sign, 0, 1));
100}
101
102char *
103fcvt(double value, int ndigit, int *decpt, int *sign)
104{
105 return(__cvt(value, ndigit, decpt, sign, 1, 1));
106}