diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c index 849af603..87bd8433 100644 --- a/src/lib/libc/stdlib/gcvt.c +++ b/src/lib/libc/stdlib/gcvt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gcvt.c,v 1.6 2005/08/08 08:05:36 espie Exp $ */ +/* $OpenBSD: gcvt.c,v 1.7 2006/01/10 02:08:28 millert Exp $ */ /* * Copyright (c) 2002, 2003 Todd C. Miller @@ -40,7 +40,7 @@ gcvt(double value, int ndigit, char *buf) digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); if (decpt == 9999) { /* Infinity or NaN, assume buffer is at least ndigit long. */ - strlcpy(buf, digits, ndigit + 1); + snprintf(buf, ndigit + 1, "%s%s", sign ? "-" : "", digits); return (buf); } @@ -49,13 +49,16 @@ gcvt(double value, int ndigit, char *buf) *dst++ = '-'; if (decpt < 0 || decpt > ndigit) { - /* exponential format */ + /* exponential format (e.g. 1.2345e+13) */ if (--decpt < 0) { sign = 1; decpt = -decpt; } else sign = 0; - for (src = digits; *src != '\0'; ) + src = digits; + *dst++ = *src++; + *dst++ = '.'; /* XXX - locale-specific */ + while (*src != '\0') *dst++ = *src++; *dst++ = 'e'; if (sign) @@ -69,7 +72,8 @@ gcvt(double value, int ndigit, char *buf) } else { /* XXX - optimize */ for (sign = decpt, i = 0; (sign /= 10) != 0; i++) - sign /= 10; + continue; + dst[i + 1] = '\0'; while (decpt != 0) { dst[i--] = '0' + decpt % 10; decpt /= 10; @@ -84,7 +88,9 @@ gcvt(double value, int ndigit, char *buf) *dst++ = '0'; } if (*src != '\0') { - *dst++ = '.'; /* XXX - locale-specific (LC_NUMERIC) */ + if (src == digits) + *dst++ = '0'; /* zero before decimal point */ + *dst++ = '.'; /* XXX - locale-specific */ for (i = decpt; digits[i] != '\0'; i++) { *dst++ = digits[i]; }