]> git.saurik.com Git - apple/libc.git/blob - stdlib/OpenBSD/gcvt.c.patch
Libc-594.9.1.tar.gz
[apple/libc.git] / stdlib / OpenBSD / gcvt.c.patch
1 --- gcvt.c.orig Thu Jul 8 16:06:45 2004
2 +++ gcvt.c Fri Jul 9 12:16:25 2004
3 @@ -27,14 +27,17 @@
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 +#include <locale.h>
8
9 extern char *__dtoa(double, int, int, int *, int *, char **);
10 +extern void __freedtoa(char *);
11
12 char *
13 gcvt(double value, int ndigit, char *buf)
14 {
15 char *digits, *dst, *src;
16 int i, decpt, sign;
17 + char *decimal_point = localeconv()->decimal_point;
18
19 if (ndigit == 0) {
20 buf[0] = '\0';
21 @@ -43,8 +46,12 @@
22
23 digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL);
24 if (decpt == 9999) {
25 - /* Infinity or NaN, assume buffer is at least ndigit long. */
26 - strlcpy(buf, digits, ndigit + 1);
27 + /* Infinity or NaN, assume buffer is long enough. */
28 + dst = buf;
29 + if (sign)
30 + *dst++ = '-';
31 + strcpy(dst, (*digits == 'N') ? "nan" : "inf");
32 + __freedtoa(digits);
33 return (buf);
34 }
35
36 @@ -59,7 +66,10 @@
37 decpt = -decpt;
38 } else
39 sign = 0;
40 - for (src = digits; *src != '\0'; )
41 + src = digits;
42 + *dst++ = *src++;
43 + dst = stpcpy(dst, decimal_point);
44 + while (*src != '\0')
45 *dst++ = *src++;
46 *dst++ = 'e';
47 if (sign)
48 @@ -72,8 +82,8 @@
49 *dst = '\0';
50 } else {
51 /* XXX - optimize */
52 - for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
53 - sign /= 10;
54 + for (sign = decpt, i = 0; (sign /= 10) != 0; i++) {}
55 + dst[i + 1] = '\0';
56 while (decpt != 0) {
57 dst[i--] = '0' + decpt % 10;
58 decpt /= 10;
59 @@ -88,12 +98,15 @@
60 *dst++ = '0';
61 }
62 if (*src != '\0') {
63 - *dst++ = '.'; /* XXX - locale-specific (LC_NUMERIC) */
64 + if (src == digits) /* need leading zero */
65 + *dst++ = '0';
66 + dst = stpcpy(dst, decimal_point);
67 for (i = decpt; digits[i] != '\0'; i++) {
68 *dst++ = digits[i];
69 }
70 }
71 *dst = '\0';
72 }
73 + __freedtoa(digits);
74 return (buf);
75 }