Annotation of coherent/d/bin/bc/getnum.xxx, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <ctype.h>
        !             3: #include "bc.h"
        !             4: 
        !             5: 
        !             6: /*
        !             7:  *     Getnum reads in a number from standard input.  It allows
        !             8:  *     decimal points and sets the scale field of the number accordingly.
        !             9:  *     In addition, although the default base is ibase, it recognizes
        !            10:  *     the C conventions of leading `0' for base 8 and `0x' for base
        !            11:  *     16.
        !            12:  */
        !            13: 
        !            14: rvalue *
        !            15: getnum(ch)
        !            16: register int   ch;
        !            17: {
        !            18:        register rvalue *result;
        !            19:        register FILE   *inf = infile;
        !            20:        mint    dig;
        !            21:        int     val,
        !            22:                dot,
        !            23:                base = ibase;
        !            24: 
        !            25:        result = (rvalue *)mpalc(sizeof *result);
        !            26:        newscalar(result);
        !            27:        minit(&dig);
        !            28:        if (ch == '0' && (ch = getc(inf)) != '.')
        !            29:                if (ch == 'x') {
        !            30:                        ch = getc(inf);
        !            31:                        base = 16;
        !            32:                } else
        !            33:                        base = 8;
        !            34:        for (dot = FALSE;; ch = getc(inf)) {
        !            35:                if (!isascii(ch))
        !            36:                        break;
        !            37:                else if (isdigit(ch))
        !            38:                        val = ch - '0';
        !            39:                else if ('A' <= ch && ch <= 'F')
        !            40:                        val = ch + 0xA - 'A';
        !            41:                else if ('a' <= ch && ch <= 'f')
        !            42:                        val = ch + 0xA - 'a';
        !            43:                else if (ch == '.' & !dot) {
        !            44:                        dot = TRUE;
        !            45:                        continue;
        !            46:                } else
        !            47:                        break;
        !            48:                mitom(val, &dig);
        !            49:                smult(&result->mantissa, base, &result->mantissa);
        !            50:                madd(&result->mantissa, &dig, &result->mantissa);
        !            51:                if (dot)
        !            52:                        ++result->scale;
        !            53:        }
        !            54:        ungetc(ch, inf);
        !            55:        mvfree(&dig);
        !            56:        return (result);
        !            57: }
        !            58: 
        !            59: 
        !            60: /*
        !            61:  *     Sibase takes the rvalue pointed to by lval and set ibase
        !            62:  *     to it if it is in range (ie. between 2 and 16).
        !            63:  */
        !            64: 
        !            65: sibase(lval)
        !            66: rvalue *lval;
        !            67: {
        !            68:        register int    base;
        !            69: 
        !            70:        base = rtoint(lval);
        !            71:        if (2 > base || base > 16)
        !            72:                bcmerr("Invalid input base");
        !            73:        ibase = base;
        !            74: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.