Annotation of cci/usr/src/lib/libc/tahoe/stdio/doprnt.c, revision 1.1

1.1     ! root        1: /* doprnt.c    Tahoe version 1/20/83 */
        !             2: #include <stdio.h>
        !             3: 
        !             4: #define        STRMAX  (BUFSIZ>>2)
        !             5: #define        RMAX    (STRMAX-50)
        !             6: 
        !             7: char *_punsign(), *_pfloat(), *_pscien(), *gcvt();
        !             8: _doprnt(formp, argp, file)
        !             9:        register char *formp;
        !            10:        int *argp;
        !            11:        FILE *file;
        !            12: {
        !            13:        register char *p, *s;
        !            14:        register count, width, ndigit, decpt;
        !            15:        int n, base, max;
        !            16:        char c, rjust, zfill, ndfnd;
        !            17:        char string[STRMAX];
        !            18: 
        !            19: loop:
        !            20:        max = 0;
        !            21:        s = p = string;
        !            22:        while((c = *formp++) && c!='%') {
        !            23:                *p++ = c;
        !            24:                if (++max >= STRMAX-1) {
        !            25:                        _strout(STRMAX, s, 0, file, 0);
        !            26:                        p = string;
        !            27:                        max = 0;
        !            28:                }
        !            29:        }
        !            30:        if((count = p-s) > 0)
        !            31:                _strout(count, s, 0, file, 0);
        !            32:        if(c == '\0')
        !            33:                return;
        !            34:        width = ndigit = rjust = ndfnd = 0;
        !            35:        zfill = ' ';
        !            36:        if(*formp == '-') {
        !            37:                rjust++;
        !            38:                formp++;
        !            39:        }
        !            40:        if(*formp == '0')
        !            41:                zfill = '0';
        !            42:        if(*formp == '*') {
        !            43:                formp++;
        !            44:                width = *argp++;
        !            45:        }
        !            46:        else while('0'<=*formp && *formp<='9')
        !            47:                width = width*10+(*formp++)-'0';
        !            48:        if(*formp == '.') {
        !            49:                if(*++formp == '*') {
        !            50:                        formp++;
        !            51:                        ndigit = *argp++;
        !            52:                        ndfnd++;
        !            53:                }
        !            54:                else while('0'<=*formp && *formp<='9') {
        !            55:                        ndigit = ndigit*10+(*formp++)-'0';
        !            56:                        ndfnd++;
        !            57:                }
        !            58:        }
        !            59:        p = string;
        !            60:        switch(c = *formp++) {
        !            61: 
        !            62:        case 'c':
        !            63:                zfill = ' ';
        !            64:                *p++ = (*argp++)&0377;
        !            65:                break;
        !            66: 
        !            67:        case 's':
        !            68:                s = (char *)*argp++;
        !            69:                if(s == NULL)
        !            70:                        s = "(null)";
        !            71:                p = s;
        !            72:                while(*p != '\0') {
        !            73:                        p++;
        !            74:                        if(--ndigit == 0)
        !            75:                                break;
        !            76:                }
        !            77:                break;
        !            78: 
        !            79:        case 'l':
        !            80:        case 'L':
        !            81:                switch(*formp++) {
        !            82:                case 'd':
        !            83:                        goto case_d;
        !            84:                case 'o':
        !            85:                        goto case_o;
        !            86:                case 'x':
        !            87:                        goto case_x;
        !            88:                case 'u':
        !            89:                        goto case_u;
        !            90:                case 'f':
        !            91:                        goto case_f;
        !            92:                default:
        !            93:                        formp--;
        !            94:                        goto xdef;
        !            95:                }
        !            96:        case_d:
        !            97:        case 'd':
        !            98:        case 'D':
        !            99:                n = *argp++;
        !           100:                if(n < 0) {
        !           101:                        n = -n;
        !           102:                        *p++ = '-';
        !           103:                }
        !           104:                base = 10;
        !           105:                goto pun;
        !           106: 
        !           107:        case_u:
        !           108:        case 'u':
        !           109:        case 'U':
        !           110:                n = *argp++;
        !           111:                base = 10;
        !           112:                goto pun;
        !           113: 
        !           114:        case_o:
        !           115:        case 'o':
        !           116:        case 'O':
        !           117:                n = *argp++;
        !           118:                base = 8;
        !           119:                goto pun;
        !           120: 
        !           121:        case_x:
        !           122:        case 'x':
        !           123:        case 'X':
        !           124:                n = *argp++;
        !           125:                base = 16;
        !           126: 
        !           127:        pun:
        !           128:                if(n == 0)
        !           129:                        *p++ = '0';
        !           130:                else
        !           131:                        p = _punsign(n, base, p);
        !           132:                break;
        !           133: 
        !           134:        case 'g':
        !           135:                p = gcvt(*((double *)argp), ndfnd?(ndigit>RMAX?RMAX:ndigit):6, p);
        !           136:                argp++;
        !           137:                argp++;
        !           138:                while(*p)
        !           139:                        p++;
        !           140:                break;
        !           141: 
        !           142:        case_f:
        !           143:        case 'f':
        !           144:                p = _pfloat(*((double *)argp), ndfnd?(ndigit>RMAX?RMAX:ndigit):6, p);
        !           145:                argp++;
        !           146:                argp++;
        !           147:                break;
        !           148: 
        !           149:        case 'e':
        !           150:                p = _pscien(*((double *)argp), (ndfnd?(ndigit>RMAX?RMAX:ndigit):6)+1, p);
        !           151:                argp++;
        !           152:                argp++;
        !           153:                break;
        !           154: 
        !           155:        xdef:
        !           156:        default:
        !           157:                *p++ = c;
        !           158:        }
        !           159:        count = p-s;
        !           160:        if((width -= count) < 0)
        !           161:                width = 0;
        !           162:        if(rjust == 0)
        !           163:                width = -width;
        !           164:        _strout(count, s, width, file, zfill);
        !           165:        goto loop;
        !           166: }
        !           167: 
        !           168: char *
        !           169: _punsign(n, base, p)
        !           170:        register unsigned n;
        !           171:        register base;
        !           172:        register char *p;
        !           173: {
        !           174: 
        !           175:        if(n==0)
        !           176:                return(p);
        !           177:        p = _punsign(n/base, base, p);
        !           178:        *p = n%base+'0';
        !           179:        if(*p > '9')
        !           180:                *p += 'A'-'0'-10;
        !           181:        return(++p);
        !           182: }

unix.superglobalmegacorp.com

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