]> git.saurik.com Git - apple/libc.git/blame - stdlib/gcvt-obsd.c
Libc-583.tar.gz
[apple/libc.git] / stdlib / gcvt-obsd.c
CommitLineData
224c7076
A
1/* $OpenBSD: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $ */
2
3/*
4 * Copyright (c) 2002, 2003 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#if defined(LIBC_SCCS) && !defined(lint)
24static char rcsid[] = "$OpenBSD: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $";
25#endif /* LIBC_SCCS and not lint */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <locale.h>
31
32extern char *__dtoa(double, int, int, int *, int *, char **);
33extern void __freedtoa(char *);
34
35char *
36gcvt(double value, int ndigit, char *buf)
37{
38 char *digits, *dst, *src;
39 int i, decpt, sign;
40 char *decimal_point = localeconv()->decimal_point;
41
42 if (ndigit == 0) {
43 buf[0] = '\0';
44 return (buf);
45 }
46
47 digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL);
48 if (decpt == 9999) {
49 /* Infinity or NaN, assume buffer is long enough. */
50 dst = buf;
51 if (sign)
52 *dst++ = '-';
53 strcpy(dst, (*digits == 'N') ? "nan" : "inf");
54 __freedtoa(digits);
55 return (buf);
56 }
57
58 dst = buf;
59 if (sign)
60 *dst++ = '-';
61
62 if (decpt < 0 || decpt > ndigit) {
63 /* exponential format */
64 if (--decpt < 0) {
65 sign = 1;
66 decpt = -decpt;
67 } else
68 sign = 0;
69 src = digits;
70 *dst++ = *src++;
71 dst = stpcpy(dst, decimal_point);
72 while (*src != '\0')
73 *dst++ = *src++;
74 *dst++ = 'e';
75 if (sign)
76 *dst++ = '-';
77 else
78 *dst++ = '+';
79 if (decpt < 10) {
80 *dst++ = '0';
81 *dst++ = '0' + decpt;
82 *dst = '\0';
83 } else {
84 /* XXX - optimize */
85 for (sign = decpt, i = 0; (sign /= 10) != 0; i++) {}
86 dst[i + 1] = '\0';
87 while (decpt != 0) {
88 dst[i--] = '0' + decpt % 10;
89 decpt /= 10;
90 }
91 }
92 } else {
93 /* standard format */
94 for (i = 0, src = digits; i < decpt; i++) {
95 if (*src != '\0')
96 *dst++ = *src++;
97 else
98 *dst++ = '0';
99 }
100 if (*src != '\0') {
101 if (src == digits) /* need leading zero */
102 *dst++ = '0';
103 dst = stpcpy(dst, decimal_point);
104 for (i = decpt; digits[i] != '\0'; i++) {
105 *dst++ = digits[i];
106 }
107 }
108 *dst = '\0';
109 }
110 __freedtoa(digits);
111 return (buf);
112}