]> git.saurik.com Git - apple/libc.git/blob - stdlib/ecvt-obsd.c
0a1e095c32ee5bf90a9f98933fb7180bf2d05115
[apple/libc.git] / stdlib / ecvt-obsd.c
1 /* $OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $ */
2
3 /*
4 * Copyright (c) 2002 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 <sys/cdefs.h>
24 #if defined(LIBC_SCCS) && !defined(lint)
25 static char rcsid[] = "$OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $";
26 #endif /* LIBC_SCCS and not lint */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 extern char *__dtoa(double, int, int, int *, int *, char **);
33 extern void __freedtoa(char *); /* special gdtoa free function */
34 static char *__cvt(double, int, int *, int *, int, int);
35
36 static char *
37 __cvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign, int fmode, int pad)
38 {
39 static char *s;
40 char *p, *rve;
41 size_t siz;
42
43 if (ndigit == 0) {
44 *sign = value < 0.0;
45 *decpt = 0;
46 return ("");
47 }
48
49 if (s) {
50 free(s);
51 s = NULL;
52 }
53
54 if (ndigit < 0)
55 siz = -ndigit + 1;
56 else
57 siz = ndigit + 1;
58
59
60 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
61 if (value == 0.0) {
62 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
63 *sign = 0;
64 if ((rve = s = (char *)malloc(siz)) == NULL)
65 return(NULL);
66 *rve++ = '0';
67 *rve = '\0';
68 } else {
69 p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
70 if (*decpt == 9999) {
71 /* Nan or Infinity */
72 *decpt = 0;
73 rve = (*p == 'N') ? "nan" : "inf";
74 __freedtoa(p);
75 return(rve);
76 }
77 /* make a local copy and adjust rve to be in terms of s */
78 if (pad && fmode)
79 siz += *decpt;
80 if ((s = (char *)malloc(siz)) == NULL) {
81 __freedtoa(p);
82 return(NULL);
83 }
84 (void) strlcpy(s, p, siz);
85 rve = s + (rve - p);
86 __freedtoa(p);
87 }
88
89 /* Add trailing zeros (unless we got NaN or Inf) */
90 if (pad && *decpt != 9999) {
91 siz -= rve - s;
92 while (--siz)
93 *rve++ = '0';
94 *rve = '\0';
95 }
96
97 return(s);
98 }
99
100 char *
101 ecvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign)
102 {
103 return(__cvt(value, ndigit, decpt, sign, 0, 1));
104 }
105
106 char *
107 fcvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign)
108 {
109 return(__cvt(value, ndigit, decpt, sign, 1, 1));
110 }