@ -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 < Todd . Miller @ courtesan . com >
* Copyright ( c ) 2002 , 2003 Todd C . Miller < Todd . Miller @ courtesan . com >
@ -40,7 +40,7 @@ gcvt(double value, int ndigit, char *buf)
digits = __dtoa ( value , 2 , ndigit , & decpt , & sign , NULL ) ;
digits = __dtoa ( value , 2 , ndigit , & decpt , & sign , NULL ) ;
if ( decpt = = 9999 ) {
if ( decpt = = 9999 ) {
/* Infinity or NaN, assume buffer is at least ndigit long. */
/* 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 ) ;
return ( buf ) ;
}
}
@ -49,13 +49,16 @@ gcvt(double value, int ndigit, char *buf)
* dst + + = ' - ' ;
* dst + + = ' - ' ;
if ( decpt < 0 | | decpt > ndigit ) {
if ( decpt < 0 | | decpt > ndigit ) {
/* exponential format */
/* exponential format (e.g. 1.2345e+13) */
if ( - - decpt < 0 ) {
if ( - - decpt < 0 ) {
sign = 1 ;
sign = 1 ;
decpt = - decpt ;
decpt = - decpt ;
} else
} else
sign = 0 ;
sign = 0 ;
for ( src = digits ; * src ! = ' \0 ' ; )
src = digits ;
* dst + + = * src + + ;
* dst + + = ' . ' ; /* XXX - locale-specific */
while ( * src ! = ' \0 ' )
* dst + + = * src + + ;
* dst + + = * src + + ;
* dst + + = ' e ' ;
* dst + + = ' e ' ;
if ( sign )
if ( sign )
@ -69,7 +72,8 @@ gcvt(double value, int ndigit, char *buf)
} else {
} else {
/* XXX - optimize */
/* XXX - optimize */
for ( sign = decpt , i = 0 ; ( sign / = 10 ) ! = 0 ; i + + )
for ( sign = decpt , i = 0 ; ( sign / = 10 ) ! = 0 ; i + + )
sign / = 10 ;
continue ;
dst [ i + 1 ] = ' \0 ' ;
while ( decpt ! = 0 ) {
while ( decpt ! = 0 ) {
dst [ i - - ] = ' 0 ' + decpt % 10 ;
dst [ i - - ] = ' 0 ' + decpt % 10 ;
decpt / = 10 ;
decpt / = 10 ;
@ -84,7 +88,9 @@ gcvt(double value, int ndigit, char *buf)
* dst + + = ' 0 ' ;
* dst + + = ' 0 ' ;
}
}
if ( * src ! = ' \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 + + ) {
for ( i = decpt ; digits [ i ] ! = ' \0 ' ; i + + ) {
* dst + + = digits [ i ] ;
* dst + + = digits [ i ] ;
}
}