]>
git.saurik.com Git - apple/libc.git/blob - stdlib/gcvt-obsd.c
1 /* $OpenBSD: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $ */
4 * Copyright (c) 2002, 2003 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: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $";
25 #endif /* LIBC_SCCS and not lint */
32 extern char *__dtoa(double, int, int, int *, int *, char **);
33 extern void __freedtoa(char *);
36 gcvt(double value
, int ndigit
, char *buf
)
38 char *digits
, *dst
, *src
;
40 char *decimal_point
= localeconv()->decimal_point
;
47 digits
= __dtoa(value
, 2, ndigit
, &decpt
, &sign
, NULL
);
49 /* Infinity or NaN, assume buffer is long enough. */
53 strcpy(dst
, (*digits
== 'N') ? "nan" : "inf");
62 if (decpt
< 0 || decpt
> ndigit
) {
63 /* exponential format */
71 dst
= stpcpy(dst
, decimal_point
);
85 for (sign
= decpt
, i
= 0; (sign
/= 10) != 0; i
++) {}
88 dst
[i
--] = '0' + decpt
% 10;
94 for (i
= 0, src
= digits
; i
< decpt
; i
++) {
101 if (src
== digits
) /* need leading zero */
103 dst
= stpcpy(dst
, decimal_point
);
104 for (i
= decpt
; digits
[i
] != '\0'; i
++) {