printf-pos.c (72291) | printf-pos.c (72523) |
---|---|
1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 40#endif 41static const char rcsid[] = | 1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 40#endif 41static const char rcsid[] = |
42 "$FreeBSD: head/lib/libc/stdio/vfprintf.c 72291 2001-02-10 06:25:33Z ache $"; | 42 "$FreeBSD: head/lib/libc/stdio/vfprintf.c 72523 2001-02-15 22:12:50Z tegge $"; |
43#endif /* LIBC_SCCS and not lint */ 44 45/* 46 * Actual printf innards. 47 * 48 * This code is large and complicated... 49 */ 50 --- 212 unchanged lines hidden (view full) --- 263#ifdef FLOATING_POINT 264#include <locale.h> 265#include <math.h> 266#include "floatio.h" 267 268#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 269#define DEFPREC 6 270 | 43#endif /* LIBC_SCCS and not lint */ 44 45/* 46 * Actual printf innards. 47 * 48 * This code is large and complicated... 49 */ 50 --- 212 unchanged lines hidden (view full) --- 263#ifdef FLOATING_POINT 264#include <locale.h> 265#include <math.h> 266#include "floatio.h" 267 268#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 269#define DEFPREC 6 270 |
271static char *cvt __P((double, int, int, char *, int *, int, int *)); | 271static char *cvt __P((double, int, int, char *, int *, int, int *, char **)); |
272static int exponent __P((char *, int, int)); 273 274#else /* no FLOATING_POINT */ 275 276#define BUF 68 277 278#endif /* FLOATING_POINT */ 279 --- 30 unchanged lines hidden (view full) --- 310#ifdef FLOATING_POINT 311 char *decimal_point = localeconv()->decimal_point; 312 char softsign; /* temporary negative sign for floats */ 313 double _double; /* double precision arguments %[eEfgG] */ 314 int expt; /* integer value of exponent */ 315 int expsize; /* character count for expstr */ 316 int ndig; /* actual number of digits returned by cvt */ 317 char expstr[7]; /* buffer for exponent string */ | 272static int exponent __P((char *, int, int)); 273 274#else /* no FLOATING_POINT */ 275 276#define BUF 68 277 278#endif /* FLOATING_POINT */ 279 --- 30 unchanged lines hidden (view full) --- 310#ifdef FLOATING_POINT 311 char *decimal_point = localeconv()->decimal_point; 312 char softsign; /* temporary negative sign for floats */ 313 double _double; /* double precision arguments %[eEfgG] */ 314 int expt; /* integer value of exponent */ 315 int expsize; /* character count for expstr */ 316 int ndig; /* actual number of digits returned by cvt */ 317 char expstr[7]; /* buffer for exponent string */ |
318 char *dtoaresult; /* buffer allocated by dtoa */ |
|
318#endif 319 u_long ulval; /* integer arguments %[diouxX] */ 320 u_quad_t uqval; /* %q integers */ 321 int base; /* base for [diouxX] conversion */ 322 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 323 int realsz; /* field size expanded by dprec, sign, etc */ 324 int size; /* size of converted field or string */ 325 int prsize; /* max size of printed field */ --- 92 unchanged lines hidden (view full) --- 418 val = GETARG (int); \ 419 nextarg = hold; \ 420 fmt = ++cp; \ 421 } else { \ 422 val = GETARG (int); \ 423 } 424 425 | 319#endif 320 u_long ulval; /* integer arguments %[diouxX] */ 321 u_quad_t uqval; /* %q integers */ 322 int base; /* base for [diouxX] conversion */ 323 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 324 int realsz; /* field size expanded by dprec, sign, etc */ 325 int size; /* size of converted field or string */ 326 int prsize; /* max size of printed field */ --- 92 unchanged lines hidden (view full) --- 419 val = GETARG (int); \ 420 nextarg = hold; \ 421 fmt = ++cp; \ 422 } else { \ 423 val = GETARG (int); \ 424 } 425 426 |
427#ifdef FLOATING_POINT 428 dtoaresult = NULL; 429#endif |
|
426 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 427 if (cantwrite(fp)) 428 return (EOF); 429 430 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 431 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 432 fp->_file >= 0) 433 return (__sbprintf(fp, fmt0, ap)); --- 169 unchanged lines hidden (view full) --- 603 break; 604 } 605 if (isnan(_double)) { 606 cp = "NaN"; 607 size = 3; 608 break; 609 } 610 flags |= FPT; | 430 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 431 if (cantwrite(fp)) 432 return (EOF); 433 434 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 435 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 436 fp->_file >= 0) 437 return (__sbprintf(fp, fmt0, ap)); --- 169 unchanged lines hidden (view full) --- 607 break; 608 } 609 if (isnan(_double)) { 610 cp = "NaN"; 611 size = 3; 612 break; 613 } 614 flags |= FPT; |
615 if (dtoaresult != NULL) { 616 free(dtoaresult); 617 dtoaresult = NULL; 618 } |
|
611 cp = cvt(_double, prec, flags, &softsign, | 619 cp = cvt(_double, prec, flags, &softsign, |
612 &expt, ch, &ndig); | 620 &expt, ch, &ndig, &dtoaresult); |
613 if (ch == 'g' || ch == 'G') { 614 if (expt <= -4 || expt > prec) 615 ch = (ch == 'g') ? 'e' : 'E'; 616 else 617 ch = 'g'; 618 } 619 if (ch <= 'e') { /* 'e' or 'E' fmt */ 620 --expt; --- 238 unchanged lines hidden (view full) --- 859 /* finally, adjust ret */ 860 ret += prsize; 861 862 FLUSH(); /* copy out the I/O vectors */ 863 } 864done: 865 FLUSH(); 866error: | 621 if (ch == 'g' || ch == 'G') { 622 if (expt <= -4 || expt > prec) 623 ch = (ch == 'g') ? 'e' : 'E'; 624 else 625 ch = 'g'; 626 } 627 if (ch <= 'e') { /* 'e' or 'E' fmt */ 628 --expt; --- 238 unchanged lines hidden (view full) --- 867 /* finally, adjust ret */ 868 ret += prsize; 869 870 FLUSH(); /* copy out the I/O vectors */ 871 } 872done: 873 FLUSH(); 874error: |
875#ifdef FLOATING_POINT 876 if (dtoaresult != NULL) 877 free(dtoaresult); 878#endif |
|
867 if (__sferror(fp)) 868 ret = EOF; 869 if ((argtable != NULL) && (argtable != statargtable)) 870 free (argtable); 871 return (ret); 872 /* NOTREACHED */ 873} 874 --- 315 unchanged lines hidden (view full) --- 1190 1191 *typetable = newtable; 1192 *tablesize = newsize; 1193} 1194 1195 1196#ifdef FLOATING_POINT 1197 | 879 if (__sferror(fp)) 880 ret = EOF; 881 if ((argtable != NULL) && (argtable != statargtable)) 882 free (argtable); 883 return (ret); 884 /* NOTREACHED */ 885} 886 --- 315 unchanged lines hidden (view full) --- 1202 1203 *typetable = newtable; 1204 *tablesize = newsize; 1205} 1206 1207 1208#ifdef FLOATING_POINT 1209 |
1198extern char *__dtoa __P((double, int, int, int *, int *, char **)); | 1210extern char *__dtoa __P((double, int, int, int *, int *, char **, char **)); |
1199 1200static char * 1201cvt(double value, int ndigits, int flags, char *sign, int *decpt, | 1211 1212static char * 1213cvt(double value, int ndigits, int flags, char *sign, int *decpt, |
1202 int ch, int *length) | 1214 int ch, int *length, char **dtoaresultp) |
1203{ 1204 int mode, dsgn; 1205 char *digits, *bp, *rve; 1206 1207 if (ch == 'f') 1208 mode = 3; /* ndigits after the decimal point */ 1209 else { 1210 /* --- 5 unchanged lines hidden (view full) --- 1216 ndigits++; 1217 mode = 2; /* ndigits significant digits */ 1218 } 1219 if (value < 0) { 1220 value = -value; 1221 *sign = '-'; 1222 } else 1223 *sign = '\000'; | 1215{ 1216 int mode, dsgn; 1217 char *digits, *bp, *rve; 1218 1219 if (ch == 'f') 1220 mode = 3; /* ndigits after the decimal point */ 1221 else { 1222 /* --- 5 unchanged lines hidden (view full) --- 1228 ndigits++; 1229 mode = 2; /* ndigits significant digits */ 1230 } 1231 if (value < 0) { 1232 value = -value; 1233 *sign = '-'; 1234 } else 1235 *sign = '\000'; |
1224 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); | 1236 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, 1237 dtoaresultp); |
1225 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1226 /* print trailing zeros */ 1227 bp = digits + ndigits; 1228 if (ch == 'f') { 1229 if (*digits == '0' && value) 1230 *decpt = -ndigits + 1; 1231 bp += *decpt; 1232 } --- 38 unchanged lines hidden --- | 1238 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1239 /* print trailing zeros */ 1240 bp = digits + ndigits; 1241 if (ch == 'f') { 1242 if (*digits == '0' && value) 1243 *decpt = -ndigits + 1; 1244 bp += *decpt; 1245 } --- 38 unchanged lines hidden --- |