]>
git.saurik.com Git - apple/libc.git/blob - stdlib/OpenBSD/ecvt.c
1 /* $OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $ */
4 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
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.
23 #if defined(LIBC_SCCS) && !defined(lint)
24 static char rcsid
[] = "$OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $";
25 #endif /* LIBC_SCCS and not lint */
31 extern char *__dtoa(double, int, int, int *, int *, char **);
32 static char *__cvt(double, int, int *, int *, int, int);
35 __cvt(double value
, int ndigit
, int *decpt
, int *sign
, int fmode
, int pad
)
58 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
60 *decpt
= 1 - fmode
; /* 1 for 'e', 0 for 'f' */
62 if ((rve
= s
= (char *)malloc(siz
)) == NULL
)
67 p
= __dtoa(value
, fmode
+ 2, ndigit
, decpt
, sign
, &rve
);
73 /* make a local copy and adjust rve to be in terms of s */
76 if ((s
= (char *)malloc(siz
)) == NULL
)
78 (void) strlcpy(s
, p
, siz
);
82 /* Add trailing zeros (unless we got NaN or Inf) */
83 if (pad
&& *decpt
!= 9999) {
94 ecvt(double value
, int ndigit
, int *decpt
, int *sign
)
96 return(__cvt(value
, ndigit
, decpt
, sign
, 0, 1));
100 fcvt(double value
, int ndigit
, int *decpt
, int *sign
)
102 return(__cvt(value
, ndigit
, decpt
, sign
, 1, 1));