]>
Commit | Line | Data |
---|---|---|
1f2f436a | 1 | /* $OpenBSD: ecvt.c,v 1.7 2009/10/16 12:15:03 martynas Exp $ */ |
3d9156a7 A |
2 | |
3 | /* | |
1f2f436a | 4 | * Copyright (c) 2002, 2006 Todd C. Miller <Todd.Miller@courtesan.com> |
3d9156a7 A |
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 | ||
ad3c9f2a | 23 | #include <sys/cdefs.h> |
3d9156a7 A |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
28 | extern char *__dtoa(double, int, int, int *, int *, char **); | |
1f2f436a | 29 | extern void __freedtoa(char *); |
3d9156a7 A |
30 | static char *__cvt(double, int, int *, int *, int, int); |
31 | ||
32 | static char * | |
ad3c9f2a | 33 | __cvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign, int fmode, int pad) |
3d9156a7 A |
34 | { |
35 | static char *s; | |
1f2f436a | 36 | char *p, *rve, c; |
3d9156a7 A |
37 | size_t siz; |
38 | ||
39 | if (ndigit == 0) { | |
40 | *sign = value < 0.0; | |
41 | *decpt = 0; | |
42 | return (""); | |
43 | } | |
44 | ||
45 | if (s) { | |
46 | free(s); | |
47 | s = NULL; | |
48 | } | |
49 | ||
50 | if (ndigit < 0) | |
51 | siz = -ndigit + 1; | |
52 | else | |
53 | siz = ndigit + 1; | |
54 | ||
55 | ||
56 | /* __dtoa() doesn't allocate space for 0 so we do it by hand */ | |
57 | if (value == 0.0) { | |
58 | *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */ | |
59 | *sign = 0; | |
60 | if ((rve = s = (char *)malloc(siz)) == NULL) | |
61 | return(NULL); | |
62 | *rve++ = '0'; | |
63 | *rve = '\0'; | |
64 | } else { | |
65 | p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve); | |
1f2f436a A |
66 | if (p == NULL) |
67 | return (NULL); | |
3d9156a7 | 68 | if (*decpt == 9999) { |
1f2f436a | 69 | /* Infinity or Nan, convert to inf or nan like printf */ |
3d9156a7 | 70 | *decpt = 0; |
1f2f436a A |
71 | c = *p; |
72 | __freedtoa(p); | |
73 | return(c == 'I' ? "inf" : "nan"); | |
3d9156a7 | 74 | } |
1f2f436a | 75 | /* Make a local copy and adjust rve to be in terms of s */ |
3d9156a7 A |
76 | if (pad && fmode) |
77 | siz += *decpt; | |
1f2f436a A |
78 | if ((s = (char *)malloc(siz)) == NULL) { |
79 | __freedtoa(p); | |
3d9156a7 | 80 | return(NULL); |
1f2f436a | 81 | } |
3d9156a7 A |
82 | (void) strlcpy(s, p, siz); |
83 | rve = s + (rve - p); | |
1f2f436a | 84 | __freedtoa(p); |
3d9156a7 A |
85 | } |
86 | ||
1f2f436a A |
87 | /* Add trailing zeros */ |
88 | if (pad) { | |
3d9156a7 A |
89 | siz -= rve - s; |
90 | while (--siz) | |
91 | *rve++ = '0'; | |
92 | *rve = '\0'; | |
93 | } | |
94 | ||
95 | return(s); | |
96 | } | |
97 | ||
98 | char * | |
ad3c9f2a | 99 | ecvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign) |
3d9156a7 A |
100 | { |
101 | return(__cvt(value, ndigit, decpt, sign, 0, 1)); | |
102 | } | |
103 | ||
104 | char * | |
ad3c9f2a | 105 | fcvt(double value, int ndigit, int * __restrict decpt, int * __restrict sign) |
3d9156a7 A |
106 | { |
107 | return(__cvt(value, ndigit, decpt, sign, 1, 1)); | |
108 | } |