doprnt.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 792 行 · 第 1/2 页
C
792 行
#ifndef lintstatic char *sccsid = "@(#)doprnt.c 4.2 (ULTRIX) 8/13/90";#endif/************************************************************************ * * * Copyright (c) 1988 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//* ------------------------------------------------------------------ *//* | Copyright Unpublished, MIPS Computer Systems, Inc. All Rights | *//* | Reserved. This software contains proprietary and confidential | *//* | information of MIPS and its suppliers. Use, disclosure or | *//* | reproduction is prohibited without the prior express written | *//* | consent of MIPS. | *//* ------------------------------------------------------------------ *//* doprnt.c 2.2 *//* * _doprnt: common code for printf, fprintf, sprintf * * Modification History * 002 - Bob Fontaine - Thu Jun 21 10:22:20 EDT 1990 * Changed name of _doprnt routine to csh_doprnt. This allows * the Cshell to work with library routines that use the stdio * library printf functions. Fixes QAR #4449. * * 001 - Gary A. Gaudet - Thu Dec 28 17:20:42 EST 1989 * Fixed MAX and MIN floating point numbers. * Initialized variables. */#include <ctype.h>#include <varargs.h>#include <math.h> /* 001 - GAG */#include <limits.h> /* 001 - GAG */#define NULL 0/* #include "values.h" */#ifndef BITSPERBYTE/* These values work with any binary representation of integers * where the high-order bit contains the sign. *//* a number used normally for size of a shift */#if gcos#define BITSPERBYTE 9#else#define BITSPERBYTE 8#endif#define BITS(type) (BITSPERBYTE * (int)sizeof(type))/* short, regular and long ints with only the high-order bit turned on */#define HIBITS ((short)(1 << BITS(short) - 1))#define HIBITI (1 << BITS(int) - 1)#define HIBITL (1L << BITS(long) - 1)/* largest short, regular and long int */#define MAXSHORT ((short)~HIBITS)#define MAXINT (~HIBITI)#define MAXLONG (~HIBITL)/* various values that describe the binary floating-point representation * _EXPBASE - the exponent base * DMAXEXP - the maximum exponent of a double (as returned by frexp()) * FMAXEXP - the maximum exponent of a float (as returned by frexp()) * DMINEXP - the minimum exponent of a double (as returned by frexp()) * FMINEXP - the minimum exponent of a float (as returned by frexp()) * MAXDOUBLE - the largest double ((_EXPBASE ** DMAXEXP) * (1 - (_EXPBASE ** -DSIGNIF))) * MAXFLOAT - the largest float ((_EXPBASE ** FMAXEXP) * (1 - (_EXPBASE ** -FSIGNIF))) * MINDOUBLE - the smallest double (_EXPBASE ** (DMINEXP - 1)) * MINFLOAT - the smallest float (_EXPBASE ** (FMINEXP - 1)) * DSIGNIF - the number of significant bits in a double * FSIGNIF - the number of significant bits in a float * DMAXPOWTWO - the largest power of two exactly representable as a double * FMAXPOWTWO - the largest power of two exactly representable as a float * _IEEE - 1 if IEEE standard representation is used * _DEXPLEN - the number of bits for the exponent of a double * _FEXPLEN - the number of bits for the exponent of a float * _HIDDENBIT - 1 if high-significance bit of mantissa is implicit * LN_MAXDOUBLE - the natural log of the largest double -- log(MAXDOUBLE) * LN_MINDOUBLE - the natural log of the smallest double -- log(MINDOUBLE) */#if u3b || u3b5#define MAXDOUBLE 1.79769313486231470e+308#define MAXFLOAT ((float)3.40282346638528860e+38)#define MINDOUBLE 4.94065645841246544e-324#define MINFLOAT ((float)1.40129846432481707e-45)#define _IEEE 1#define _DEXPLEN 11#define _HIDDENBIT 1#define DMINEXP (-(DMAXEXP + DSIGNIF - _HIDDENBIT - 3))#define FMINEXP (-(FMAXEXP + FSIGNIF - _HIDDENBIT - 3))#endif#if pdp11 || vax || mips#define MAXDOUBLE DBL_MAX /* 001 - GAG *//* The following is kludged because the PDP-11 compilers botch the simple form. The kludge causes the constant to be computed at run-time on the PDP-11, even though it is still "folded" at compile-time on the VAX. */#define MINDOUBLE DBL_MIN /* 001 - GAG */#define MINFLOAT -MAXFLOAT /* 001 - GAG */#define _IEEE 0#define _DEXPLEN 8#define _HIDDENBIT 1#define DMINEXP (-DMAXEXP)#define FMINEXP (-FMAXEXP)#endif#if gcos#define MAXDOUBLE 1.7014118346046923171e+38#define MAXFLOAT ((float)1.7014118219281863150e+38)#define MINDOUBLE 2.9387358770557187699e-39#define MINFLOAT ((float)MINDOUBLE)#define _IEEE 0#define _DEXPLEN 8#define _HIDDENBIT 0#define DMINEXP (-(DMAXEXP + 1))#define FMINEXP (-(FMAXEXP + 1))#endif#if u370 || gould#define MAXDOUBLE 0.7237005577332262e+76#define MAXFLOAT ((float)0.7237005145e+76)#define MINDOUBLE 5.3976053469342702e-79#define MINFLOAT ((float)MINDOUBLE)#define _IEEE 0#define _DEXPLEN 8 /* DAG -- Gould said 7, but I think this is right */#define _HIDDENBIT 0#define DMINEXP (-(DMAXEXP + 1))#define FMINEXP (-(FMAXEXP + 1))#endif#if u370 || gould#define _LENBASE 4#else#define _LENBASE 1#endif#define _EXPBASE (1 << _LENBASE)#define _FEXPLEN 8#define DSIGNIF (BITS(double) - _DEXPLEN + _HIDDENBIT - 1)#define FSIGNIF (BITS(float) - _FEXPLEN + _HIDDENBIT - 1)#define DMAXPOWTWO ((double)(1L << BITS(long) - 2) * \ (1L << DSIGNIF - BITS(long) + 1))#define FMAXPOWTWO ((float)(1L << FSIGNIF - 1))#define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)#define FMAXEXP ((1 << _FEXPLEN - 1) - 1 + _IEEE)#define LN_MAXDOUBLE (M_LN2 * DMAXEXP)#define LN_MINDOUBLE (M_LN2 * (DMINEXP - 1))#define H_PREC (DSIGNIF % 2 ? (1L << DSIGNIF/2) * M_SQRT2 : 1L << DSIGNIF/2)#define X_EPS (1.0/H_PREC)#define X_PLOSS ((double)(long)(M_PI * H_PREC))#define X_TLOSS (M_PI * DMAXPOWTWO)#define M_LN2 0.69314718055994530942#define M_PI 3.14159265358979323846#define M_SQRT2 1.41421356237309504880#define MAXBEXP DMAXEXP /* for backward compatibility */#define MINBEXP DMINEXP /* for backward compatibility */#define MAXPOWTWO DMAXPOWTWO /* for backward compatibility */#endif/* #include "print.h" *//* Maximum number of digits in any integer representation */#define MAXDIGS 11/* Maximum total number of digits in E format */#if u3b || u3b5#define MAXECVT 17#else#define MAXECVT 18#endif/* Maximum number of digits after decimal point in F format */#define MAXFCVT 60/* Maximum significant figures in a floating-point number */#define MAXFSIG MAXECVT/* Maximum number of characters in an exponent */#if u3b || u3b5#define MAXESIZ 5#else#define MAXESIZ 4#endif/* Maximum (positive) exponent */#if u3b || u3b5#define MAXEXP 325 /* DAG -- bug fix (was 310) */#else#if gould#define MAXEXP 80#else#define MAXEXP 40#endif#endif/* Data type for flags */typedef char bool;/* Convert a digit character to the corresponding number */#define tonumber(x) ((x)-'0')/* Convert a number between 0 and 9 to the corresponding digit */#define todigit(x) ((x)+'0')/* Max and Min macros */#define max(a,b) ((a) > (b)? (a): (b))#define min(a,b) ((a) < (b)? (a): (b))/*#include "nan.h" *//* Handling of Not_a_Number's (only in IEEE floating-point standard) */#define KILLFPE() (void) kill(getpid(), 8)#if u3b || u3b5#define NaN(X) (((union { double d; struct { unsigned :1, e:11; } s; } \ *)&X)->s.e == 0x7ff)#define KILLNaN(X) if (NaN(X)) KILLFPE()#else#define Nan(X) 0#define KILLNaN(X)#endif/* * Returns the number of * non-NULL bytes in string argument. */static intstrlen(s)register char *s;{ register char *s0 = s + 1; while (*s++ != '\0') ; return (s - s0);}/* * ecvt converts to decimal * the number of digits is specified by ndigit * decpt is set to the position of the decimal point * sign is set to 0 for positive, 1 for negative * */#define NMAX ((DSIGNIF * 3 + 19)/10) /* restrict max precision */#define NDIG 80#define PUT(p, n) { register int nn; \ if (n > 0) { \ nn = 0; \ do { \ putchar((int)*(p+nn)); \ nn++; \ } while (nn != n); \ } \ }/* bit positions for flags used in doprnt */#define LENGTH 1 /* l */#define FPLUS 2 /* + */#define FMINUS 4 /* - */#define FBLANK 8 /* blank */#define FSHARP 16 /* # */#define PADZERO 32 /* padding zeroes requested via '0' */#define DOTSEEN 64 /* dot appeared in format specification */#define SUFFIX 128 /* a suffix is to appear in the output */#define RZERO 256 /* there will be trailing zeros in output */#define LZERO 512 /* there will be leading zeroes in output */static int_lowdigit(valptr)long *valptr;{ /* This function computes the decimal low-order digit of the number */ /* pointed to by valptr, and returns this digit after dividing */ /* *valptr by ten. This function is called ONLY to compute the */ /* low-order digit of a long whose high-order bit is set. */ int lowbit = *valptr & 1; long value = (*valptr >> 1) & ~HIBITL; *valptr = value / 5; return(value % 5 * 2 + lowbit + '0');}intcsh_doprnt(format, args) /* 002 RNF */register char *format;va_list args;{ /* This variable counts output characters. */ int count = 0; /* Starting and ending points for value to be printed */ register char *bp; char *p; /* Field width and precision */ int width, prec; /* Format code */ register int fcode; /* Number of padding zeroes required on the left and right */ int lzero = 0, /* 001 - GAG */ rzero = 0; /* 001 - GAG */ /* Flags - bit positions defined by LENGTH, FPLUS, FMINUS, FBLANK, */ /* and FSHARP are set if corresponding character is in format */ /* Bit position defined by PADZERO means extra space in the field */ /* should be padded with leading zeroes rather than with blanks */ register int flagword; /* Values are developed in this buffer */ char buf[max(MAXDIGS, 1+max(MAXFCVT+MAXEXP, MAXECVT))]; /* Pointer to sign, "0x", "0X", or empty */ char *prefix; /* Exponent or empty */ char *suffix = 0; /* 001 - GAG */ /* Length of prefix and of suffix */ int prefixlength = 0, /* 001 - GAG */ suffixlength = 0; /* 001 - GAG */ /* combined length of leading zeroes, trailing zeroes, and suffix */ int otherlength; /* The value being converted, if integer */ long val; /* Pointer to a translate table for digits of whatever radix */ char *tab; /* Work variables */ int k, lradix, mradix; /* * The main loop -- this loop goes through one iteration * for each string of ordinary characters or format specification. */ for ( ; ; ) { register int n; while ((fcode = *format) != '\0' && fcode != '%') { putchar(fcode); format++; } if (fcode == '\0') { /* end of format; return */ return; } /* * % has been found. * The following switch is used to parse the format * specification and to perform the operation specified * by the format letter. The program repeatedly goes * back to this switch until the format letter is * encountered. */ width = prefixlength = otherlength = flagword = 0; format++; charswitch: switch (fcode = *format++) { case '+': flagword |= FPLUS; goto charswitch; case '-': flagword |= FMINUS; goto charswitch; case ' ': flagword |= FBLANK; goto charswitch; case '#': flagword |= FSHARP; goto charswitch;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?