|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1988 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that the above copyright notice and this paragraph are ! 7: * duplicated in all such forms and that any documentation, ! 8: * advertising materials, and other materials related to such ! 9: * distribution and use acknowledge that the software was developed ! 10: * by the University of California, Berkeley. The name of the ! 11: * University may not be used to endorse or promote products derived ! 12: * from this software without specific prior written permission. ! 13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 16: */ ! 17: ! 18: #ifndef lint ! 19: static char *sccsid = "@(#)doprnt.c 5.5 (Berkeley) 6/18/88"; ! 20: #endif /* not lint */ ! 21: ! 22: #ifdef notdef ! 23: static char sccsid[] = "@(#)doprnt.c 5.32 (Berkeley) 6/3/88"; ! 24: #endif ! 25: ! 26: #include <sys/types.h> ! 27: #include <varargs.h> ! 28: #include <stdio.h> ! 29: #include <ctype.h> ! 30: ! 31: /* 11-bit exponent (VAX G floating point) is 308 decimal digits */ ! 32: #define MAXEXP 308 ! 33: /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */ ! 34: #define MAXFRACT 39 ! 35: ! 36: #define DEFPREC 6 ! 37: ! 38: #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ ! 39: ! 40: #ifdef CSH_WHACKS ! 41: #define PUTC(ch) (void) putc(ch, fp) ! 42: #else ! 43: #include "sh.h" ! 44: #define PUTC(c) { ch = c; CSHPUTCHAR; } ! 45: #endif ! 46: ! 47: #define ARG() \ ! 48: _ulong = flags&LONGINT ? va_arg(argp, long) : \ ! 49: flags&SHORTINT ? va_arg(argp, short) : va_arg(argp, int); ! 50: ! 51: #define todigit(c) ((c) - '0') ! 52: #define tochar(n) ((n) + '0') ! 53: ! 54: /* have to deal with the negative buffer count kludge */ ! 55: #define NEGATIVE_COUNT_KLUDGE ! 56: ! 57: #define LONGINT 0x01 /* long integer */ ! 58: #define LONGDBL 0x02 /* long double; unimplemented */ ! 59: #define SHORTINT 0x04 /* short integer */ ! 60: #define ALT 0x08 /* alternate form */ ! 61: #define LADJUST 0x10 /* left adjustment */ ! 62: #define ZEROPAD 0x20 /* zero (as opposed to blank) pad */ ! 63: #define HEXPREFIX 0x40 /* add 0x or 0X prefix */ ! 64: ! 65: _doprnt(fmt0, argp, fp) ! 66: u_char *fmt0; ! 67: va_list argp; ! 68: register FILE *fp; ! 69: { ! 70: register u_char *fmt; /* format string */ ! 71: register int ch; /* character from fmt */ ! 72: register int cnt; /* return value accumulator */ ! 73: register int n; /* random handy integer */ ! 74: register char *t; /* buffer pointer */ ! 75: double _double; /* double precision arguments %[eEfgG] */ ! 76: u_long _ulong; /* integer arguments %[diouxX] */ ! 77: int base; /* base for [diouxX] conversion */ ! 78: int dprec; /* decimal precision in [diouxX] */ ! 79: int fieldsz; /* field size expanded by sign, etc */ ! 80: int flags; /* flags as above */ ! 81: int fpprec; /* `extra' floating precision in [eEfgG] */ ! 82: int prec; /* precision from format (%.3d), or -1 */ ! 83: int realsz; /* field size expanded by decimal precision */ ! 84: int size; /* size of converted field or string */ ! 85: int width; /* width from format (%8d), or 0 */ ! 86: char sign; /* sign prefix (+ - or \0) */ ! 87: char *digs; /* digits for [diouxX] conversion */ ! 88: char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ ! 89: ! 90: if (fp->_flag & _IORW) { ! 91: fp->_flag |= _IOWRT; ! 92: fp->_flag &= ~(_IOEOF|_IOREAD); ! 93: } ! 94: if ((fp->_flag & _IOWRT) == 0) ! 95: return (EOF); ! 96: ! 97: fmt = fmt0; ! 98: digs = "0123456789abcdef"; ! 99: for (cnt = 0;; ++fmt) { ! 100: #ifdef CSH_WHACKS ! 101: n = fp->_cnt; ! 102: for (t = (char *)fp->_ptr; (ch = *fmt) && ch != '%'; ! 103: ++cnt, ++fmt) ! 104: if (--n < 0 ! 105: #ifdef NEGATIVE_COUNT_KLUDGE ! 106: && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz) ! 107: #endif ! 108: || ch == '\n' && fp->_flag & _IOLBF) { ! 109: fp->_cnt = n; ! 110: fp->_ptr = t; ! 111: (void) _flsbuf((u_char)ch, fp); ! 112: n = fp->_cnt; ! 113: t = (char *)fp->_ptr; ! 114: } else ! 115: *t++ = ch; ! 116: fp->_cnt = n; ! 117: fp->_ptr = t; ! 118: #else ! 119: if ((ch = *fmt) && ch != '%') { ! 120: CSHPUTCHAR; ! 121: continue; ! 122: } ! 123: #endif ! 124: if (!ch) ! 125: return (cnt); ! 126: ! 127: flags = dprec = fpprec = width = 0; ! 128: prec = -1; ! 129: sign = '\0'; ! 130: ! 131: rflag: switch (*++fmt) { ! 132: case ' ': ! 133: sign = ' '; ! 134: goto rflag; ! 135: case '#': ! 136: flags |= ALT; ! 137: goto rflag; ! 138: case '*': ! 139: /* ! 140: * ``A negative field width argument is taken as a ! 141: * - flag followed by a positive field width.'' ! 142: * -- ANSI X3J11 ! 143: * They don't exclude field widths read from args. ! 144: */ ! 145: if ((width = va_arg(argp, int)) >= 0) ! 146: goto rflag; ! 147: width = -width; ! 148: /* FALLTHROUGH */ ! 149: case '-': ! 150: flags |= LADJUST; ! 151: goto rflag; ! 152: case '+': ! 153: sign = '+'; ! 154: goto rflag; ! 155: case '.': ! 156: if (*++fmt == '*') ! 157: n = va_arg(argp, int); ! 158: else { ! 159: n = 0; ! 160: while (isascii(*fmt) && isdigit(*fmt)) ! 161: n = 10 * n + todigit(*fmt++); ! 162: --fmt; ! 163: } ! 164: prec = n < 0 ? -1 : n; ! 165: goto rflag; ! 166: case '0': ! 167: /* ! 168: * ``Note that 0 is taken as a flag, not as the ! 169: * beginning of a field width.'' ! 170: * -- ANSI X3J11 ! 171: */ ! 172: flags |= ZEROPAD; ! 173: goto rflag; ! 174: case '1': case '2': case '3': case '4': ! 175: case '5': case '6': case '7': case '8': case '9': ! 176: n = 0; ! 177: do { ! 178: n = 10 * n + todigit(*fmt); ! 179: } while (isascii(*++fmt) && isdigit(*fmt)); ! 180: width = n; ! 181: --fmt; ! 182: goto rflag; ! 183: case 'L': ! 184: flags |= LONGDBL; ! 185: goto rflag; ! 186: case 'h': ! 187: flags |= SHORTINT; ! 188: goto rflag; ! 189: case 'l': ! 190: flags |= LONGINT; ! 191: goto rflag; ! 192: case 'c': ! 193: *(t = buf) = va_arg(argp, int); ! 194: size = 1; ! 195: sign = '\0'; ! 196: goto pforw; ! 197: case 'D': ! 198: flags |= LONGINT; ! 199: /*FALLTHROUGH*/ ! 200: case 'd': ! 201: case 'i': ! 202: ARG(); ! 203: if ((long)_ulong < 0) { ! 204: _ulong = -_ulong; ! 205: sign = '-'; ! 206: } ! 207: base = 10; ! 208: goto number; ! 209: case 'e': ! 210: case 'E': ! 211: case 'f': ! 212: case 'g': ! 213: case 'G': ! 214: _double = va_arg(argp, double); ! 215: /* ! 216: * don't bother to do unrealistic precision; just ! 217: * pad it with zeroes later. This keeps buffer size ! 218: * rational. ! 219: */ ! 220: if (prec > MAXFRACT) { ! 221: if (*fmt != 'g' && *fmt != 'G' || (flags&ALT)) ! 222: fpprec = prec - MAXFRACT; ! 223: prec = MAXFRACT; ! 224: } ! 225: else if (prec == -1) ! 226: prec = DEFPREC; ! 227: if (_double < 0) { ! 228: sign = '-'; ! 229: _double = -_double; ! 230: } ! 231: /* ! 232: * _cvt may have to round up past the "start" of the ! 233: * buffer, i.e. ``intf("%.2f", (double)9.999);''; ! 234: * if the first char isn't NULL, it did. ! 235: */ ! 236: *buf = NULL; ! 237: size = _cvt(_double, prec, flags, *fmt, buf, ! 238: buf + sizeof(buf)); ! 239: t = *buf ? buf : buf + 1; ! 240: goto pforw; ! 241: case 'n': ! 242: if (flags & LONGINT) ! 243: *va_arg(argp, long *) = cnt; ! 244: else if (flags & SHORTINT) ! 245: *va_arg(argp, short *) = cnt; ! 246: else ! 247: *va_arg(argp, int *) = cnt; ! 248: break; ! 249: case 'O': ! 250: flags |= LONGINT; ! 251: /*FALLTHROUGH*/ ! 252: case 'o': ! 253: ARG(); ! 254: base = 8; ! 255: goto nosign; ! 256: case 'p': ! 257: /* ! 258: * ``The argument shall be a pointer to void. The ! 259: * value of the pointer is converted to a sequence ! 260: * of printable characters, in an implementation- ! 261: * defined manner.'' ! 262: * -- ANSI X3J11 ! 263: */ ! 264: /* NOSTRICT */ ! 265: _ulong = (u_long)va_arg(argp, void *); ! 266: base = 16; ! 267: goto nosign; ! 268: case 's': ! 269: if (!(t = va_arg(argp, char *))) ! 270: t = "(null)"; ! 271: if (prec >= 0) { ! 272: /* ! 273: * can't use strlen; can only look for the ! 274: * NUL in the first `prec' characters, and ! 275: * strlen() will go further. ! 276: */ ! 277: char *p, *memchr(); ! 278: ! 279: if (p = memchr(t, 0, prec)) { ! 280: size = p - t; ! 281: if (size > prec) ! 282: size = prec; ! 283: } else ! 284: size = prec; ! 285: } else ! 286: size = strlen(t); ! 287: sign = '\0'; ! 288: goto pforw; ! 289: case 'U': ! 290: flags |= LONGINT; ! 291: /*FALLTHROUGH*/ ! 292: case 'u': ! 293: ARG(); ! 294: base = 10; ! 295: goto nosign; ! 296: case 'X': ! 297: digs = "0123456789ABCDEF"; ! 298: /* FALLTHROUGH */ ! 299: case 'x': ! 300: ARG(); ! 301: base = 16; ! 302: /* leading 0x/X only if non-zero */ ! 303: if (flags & ALT && _ulong != 0) ! 304: flags |= HEXPREFIX; ! 305: ! 306: /* unsigned conversions */ ! 307: nosign: sign = '\0'; ! 308: /* ! 309: * ``... diouXx conversions ... if a precision is ! 310: * specified, the 0 flag will be ignored.'' ! 311: * -- ANSI X3J11 ! 312: */ ! 313: number: if ((dprec = prec) >= 0) ! 314: flags &= ~ZEROPAD; ! 315: ! 316: /* ! 317: * ``The result of converting a zero value with an ! 318: * explicit precision of zero is no characters.'' ! 319: * -- ANSI X3J11 ! 320: */ ! 321: t = buf + BUF; ! 322: if (_ulong != 0 || prec != 0) { ! 323: do { ! 324: *--t = digs[_ulong % base]; ! 325: _ulong /= base; ! 326: } while (_ulong); ! 327: digs = "0123456789abcdef"; ! 328: if (flags & ALT && base == 8 && *t != '0') ! 329: *--t = '0'; /* octal leading 0 */ ! 330: } ! 331: size = buf + BUF - t; ! 332: ! 333: pforw: ! 334: /* ! 335: * All reasonable formats wind up here. At this point, ! 336: * `t' points to a string which (if not flags&LADJUST) ! 337: * should be padded out to `width' places. If ! 338: * flags&ZEROPAD, it should first be prefixed by any ! 339: * sign or other prefix; otherwise, it should be blank ! 340: * padded before the prefix is emitted. After any ! 341: * left-hand padding and prefixing, emit zeroes ! 342: * required by a decimal [diouxX] precision, then print ! 343: * the string proper, then emit zeroes required by any ! 344: * leftover floating precision; finally, if LADJUST, ! 345: * pad with blanks. ! 346: */ ! 347: ! 348: /* ! 349: * compute actual size, so we know how much to pad ! 350: * fieldsz excludes decimal prec; realsz includes it ! 351: */ ! 352: fieldsz = size + fpprec; ! 353: if (sign) ! 354: fieldsz++; ! 355: if (flags & HEXPREFIX) ! 356: fieldsz += 2; ! 357: realsz = dprec > fieldsz ? dprec : fieldsz; ! 358: ! 359: /* right-adjusting blank padding */ ! 360: if ((flags & (LADJUST|ZEROPAD)) == 0 && width) ! 361: for (n = realsz; n < width; n++) ! 362: PUTC(' '); ! 363: /* prefix */ ! 364: if (sign) ! 365: PUTC(sign); ! 366: if (flags & HEXPREFIX) { ! 367: PUTC('0'); ! 368: PUTC((char)*fmt); ! 369: } ! 370: /* right-adjusting zero padding */ ! 371: if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) ! 372: for (n = realsz; n < width; n++) ! 373: PUTC('0'); ! 374: /* leading zeroes from decimal precision */ ! 375: for (n = fieldsz; n < dprec; n++) ! 376: PUTC('0'); ! 377: ! 378: /* the string or number proper */ ! 379: #ifdef CSH_WHACKS ! 380: if (fp->_cnt - (n = size) >= 0 && ! 381: (fp->_flag & _IOLBF) == 0) { ! 382: fp->_cnt -= n; ! 383: bcopy(t, (char *)fp->_ptr, n); ! 384: fp->_ptr += n; ! 385: } else ! 386: while (--n >= 0) ! 387: #else ! 388: for (n = size; --n >= 0;) ! 389: #endif ! 390: PUTC(*t++); ! 391: /* trailing f.p. zeroes */ ! 392: while (--fpprec >= 0) ! 393: PUTC('0'); ! 394: /* left-adjusting padding (always blank) */ ! 395: if (flags & LADJUST) ! 396: for (n = realsz; n < width; n++) ! 397: PUTC(' '); ! 398: /* finally, adjust cnt */ ! 399: cnt += width > realsz ? width : realsz; ! 400: break; ! 401: case '\0': /* "%?" prints ?, unless ? is NULL */ ! 402: return (cnt); ! 403: default: ! 404: PUTC((char)*fmt); ! 405: cnt++; ! 406: } ! 407: } ! 408: /* NOTREACHED */ ! 409: } ! 410: ! 411: static ! 412: _cvt(number, prec, flags, fmtch, startp, endp) ! 413: double number; ! 414: register int prec; ! 415: int flags; ! 416: u_char fmtch; ! 417: char *startp, *endp; ! 418: { ! 419: register char *p, *t; ! 420: double fract, integer, tmp, modf(); ! 421: int dotrim, expcnt, gformat; ! 422: char *exponent(), *eround(), *fround(); ! 423: ! 424: dotrim = expcnt = gformat = 0; ! 425: fract = modf(number, &integer); ! 426: ! 427: /* get an extra slot for rounding. */ ! 428: t = ++startp; ! 429: ! 430: /* ! 431: * get integer portion of number; put into the end of the buffer; the ! 432: * .01 is added for modf(356.0 / 10, &integer) returning .59999999... ! 433: */ ! 434: for (p = endp - 1; integer; ++expcnt) { ! 435: tmp = modf(integer / 10, &integer); ! 436: *p-- = tochar((int)((tmp + .01) * 10)); ! 437: } ! 438: switch(fmtch) { ! 439: case 'f': ! 440: /* reverse integer into beginning of buffer */ ! 441: if (expcnt) ! 442: for (; ++p < endp; *t++ = *p); ! 443: else ! 444: *t++ = '0'; ! 445: /* ! 446: * if precision required or alternate flag set, add in a ! 447: * decimal point. ! 448: */ ! 449: if (prec || flags&ALT) ! 450: *t++ = '.'; ! 451: /* if requires more precision and some fraction left */ ! 452: if (fract) { ! 453: if (prec) ! 454: do { ! 455: fract = modf(fract * 10, &tmp); ! 456: *t++ = tochar((int)tmp); ! 457: } while (--prec && fract); ! 458: if (fract) ! 459: startp = fround(startp, t - 1, fract); ! 460: } ! 461: for (; prec--; *t++ = '0'); ! 462: break; ! 463: case 'e': ! 464: case 'E': ! 465: eformat: if (expcnt) { ! 466: *t++ = *++p; ! 467: if (prec || flags&ALT) ! 468: *t++ = '.'; ! 469: /* if requires more precision and some integer left */ ! 470: for (; prec && ++p < endp; --prec) ! 471: *t++ = *p; ! 472: /* ! 473: * if done precision and more of the integer component, ! 474: * round using it; adjust fract so we don't re-round ! 475: * later. ! 476: */ ! 477: if (!prec && ++p < endp) { ! 478: startp = eround(startp, t - 1, (double)0, ! 479: *p, &expcnt); ! 480: fract = 0; ! 481: } ! 482: /* adjust expcnt for digit in front of decimal */ ! 483: --expcnt; ! 484: } ! 485: /* until first fractional digit, decrement exponent */ ! 486: else if (fract) { ! 487: /* adjust expcnt for digit in front of decimal */ ! 488: for (expcnt = -1;; --expcnt) { ! 489: fract = modf(fract * 10, &tmp); ! 490: if (tmp) ! 491: break; ! 492: } ! 493: *t++ = tochar((int)tmp); ! 494: if (prec || flags&ALT) ! 495: *t++ = '.'; ! 496: } ! 497: else { ! 498: *t++ = '0'; ! 499: if (prec || flags&ALT) ! 500: *t++ = '.'; ! 501: } ! 502: /* if requires more precision and some fraction left */ ! 503: if (fract) { ! 504: if (prec) ! 505: do { ! 506: fract = modf(fract * 10, &tmp); ! 507: *t++ = tochar((int)tmp); ! 508: } while (--prec && fract); ! 509: if (fract) ! 510: startp = eround(startp, t - 1, fract, ! 511: (char)0, &expcnt); ! 512: } ! 513: /* if requires more precision */ ! 514: for (; prec--; *t++ = '0'); ! 515: ! 516: /* unless alternate flag, trim any g/G format trailing 0's */ ! 517: if (gformat && !(flags&ALT)) { ! 518: while (t > startp && *--t == '0'); ! 519: if (*t == '.') ! 520: --t; ! 521: ++t; ! 522: } ! 523: t = exponent(t, expcnt, fmtch); ! 524: break; ! 525: case 'g': ! 526: case 'G': ! 527: /* a precision of 0 is treated as a precision of 1. */ ! 528: if (!prec) ! 529: ++prec; ! 530: /* ! 531: * ``The style used depends on the value converted; style e ! 532: * will be used only if the exponent resulting from the ! 533: * conversion is less than -4 or greater than the precision.'' ! 534: * -- ANSI X3J11 ! 535: */ ! 536: if (expcnt > prec || !expcnt && fract && fract < .0001) { ! 537: /* ! 538: * g/G format counts "significant digits, not digits of ! 539: * precision; for the e/E format, this just causes an ! 540: * off-by-one problem, i.e. g/G considers the digit ! 541: * before the decimal point significant and e/E doesn't ! 542: * count it as precision. ! 543: */ ! 544: --prec; ! 545: fmtch -= 2; /* G->E, g->e */ ! 546: gformat = 1; ! 547: goto eformat; ! 548: } ! 549: /* ! 550: * reverse integer into beginning of buffer, ! 551: * note, decrement precision ! 552: */ ! 553: if (expcnt) ! 554: for (; ++p < endp; *t++ = *p, --prec); ! 555: else ! 556: *t++ = '0'; ! 557: /* ! 558: * if precision required or alternate flag set, add in a ! 559: * decimal point. If no digits yet, add in leading 0. ! 560: */ ! 561: if (prec || flags&ALT) { ! 562: dotrim = 1; ! 563: *t++ = '.'; ! 564: } ! 565: else ! 566: dotrim = 0; ! 567: /* if requires more precision and some fraction left */ ! 568: if (fract) { ! 569: if (prec) { ! 570: do { ! 571: fract = modf(fract * 10, &tmp); ! 572: *t++ = tochar((int)tmp); ! 573: } while(!tmp); ! 574: while (--prec && fract) { ! 575: fract = modf(fract * 10, &tmp); ! 576: *t++ = tochar((int)tmp); ! 577: } ! 578: } ! 579: if (fract) ! 580: startp = fround(startp, t - 1, fract); ! 581: } ! 582: /* alternate format, adds 0's for precision, else trim 0's */ ! 583: if (flags&ALT) ! 584: for (; prec--; *t++ = '0'); ! 585: else if (dotrim) { ! 586: while (t > startp && *--t == '0'); ! 587: if (*t != '.') ! 588: ++t; ! 589: } ! 590: } ! 591: return(t - startp); ! 592: } ! 593: ! 594: static char * ! 595: fround(start, end, fract) ! 596: register char *start, *end; ! 597: double fract; ! 598: { ! 599: double tmp; ! 600: ! 601: (void)modf(fract * 10, &tmp); ! 602: if (tmp > 4) ! 603: for (;; --end) { ! 604: if (*end == '.') ! 605: --end; ! 606: if (++*end <= '9') ! 607: break; ! 608: *end = '0'; ! 609: /* add extra digit to round past buffer beginning */ ! 610: if (end == start) { ! 611: *--end = '1'; ! 612: --start; ! 613: break; ! 614: } ! 615: } ! 616: return(start); ! 617: } ! 618: ! 619: static char * ! 620: eround(start, end, fract, ch, exp) ! 621: register char *start, *end; ! 622: double fract; ! 623: char ch; ! 624: int *exp; ! 625: { ! 626: double tmp; ! 627: ! 628: if (fract) ! 629: (void)modf(fract * 10, &tmp); ! 630: else ! 631: tmp = todigit(ch); ! 632: if (tmp > 4) ! 633: for (;; --end) { ! 634: if (*end == '.') ! 635: --end; ! 636: if (++*end <= '9') ! 637: break; ! 638: *end = '0'; ! 639: /* increment exponent to round past buffer beginning */ ! 640: if (end == start) { ! 641: *end = '1'; ! 642: ++*exp; ! 643: break; ! 644: } ! 645: } ! 646: return(start); ! 647: } ! 648: ! 649: static char * ! 650: exponent(p, exp, fmtch) ! 651: register char *p; ! 652: register int exp; ! 653: u_char fmtch; ! 654: { ! 655: register char *t; ! 656: char expbuf[MAXEXP]; ! 657: ! 658: *p++ = fmtch; ! 659: if (exp < 0) { ! 660: exp = -exp; ! 661: *p++ = '-'; ! 662: } ! 663: else ! 664: *p++ = '+'; ! 665: t = expbuf + MAXEXP; ! 666: if (exp > 9) { ! 667: do { ! 668: *--t = tochar(exp % 10); ! 669: } while ((exp /= 10) > 9); ! 670: *--t = tochar(exp); ! 671: for (; t < expbuf + MAXEXP; *p++ = *t++); ! 672: } ! 673: else { ! 674: *p++ = '0'; ! 675: *p++ = tochar(exp); ! 676: } ! 677: return(p); ! 678: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.