Annotation of cci/usr/src/ucb/ex/x.c, revision 1.1.1.1

1.1       root        1: char   xstr[];
                      2: #line 1 "printf.c"
                      3: 
                      4: static char *printf_id = (&xstr[8210]);
                      5: 
                      6: static char *sccsid = (&xstr[8235]);
                      7: 
                      8: #line 1 "/DIST/usr/include/varargs.h"
                      9: 
                     10: 
                     11: typedef char *va_list;
                     12: 
                     13: 
                     14: 
                     15: 
                     16: #line 6 "printf.c"
                     17: 
                     18: 
                     19: 
                     20: 
                     21: 
                     22: 
                     23: 
                     24:  
                     25: 
                     26: 
                     27: 
                     28: 
                     29: 
                     30: 
                     31: static int width, sign, fill;
                     32: 
                     33: char *_p_dconv();
                     34: 
                     35: printf(va_alist)
                     36:        int va_alist;
                     37: {
                     38:        va_list ap;
                     39:        register char *fmt;
                     40:        char fcode;
                     41:        int prec;
                     42:        int length,mask1,nbits,n;
                     43:        long int mask2, num;
                     44:        register char *bptr;
                     45:        char *ptr;
                     46:        char buf[134];
                     47: 
                     48:         ap = (char *) &va_alist;
                     49:        fmt =  ((char * *)(ap += sizeof(char *)))[-1];
                     50:        for (;;) {
                     51:                
                     52:                while ((fcode = *fmt++)!='%') {
                     53:                        
                     54:                        if (fcode=='\0')
                     55:                                return;
                     56:                        putchar(fcode);
                     57:                }
                     58:                
                     59:                length = 0;
                     60:                
                     61:                sign = 0;
                     62:                if (*fmt == '-') {
                     63:                        sign++;
                     64:                        fmt++;
                     65:                }
                     66:                
                     67:                
                     68:                fill = 1;
                     69:                if (*fmt == '0') {
                     70:                        fill--;
                     71:                        fmt++;
                     72:                }
                     73:                
                     74:                
                     75:                if (*fmt == '*') {
                     76:                        width =  (( int *)(ap += sizeof( int)))[-1];
                     77:                        if (width < 0) {
                     78:                                width = -width;
                     79:                                sign = !sign;
                     80:                        }
                     81:                        fmt++;
                     82:                }
                     83:                else {
                     84:                        width = 0;
                     85:                        while (*fmt>='0' && *fmt<='9')
                     86:                                width = width * 10 + (*fmt++ - '0');
                     87:                }
                     88:                
                     89:                
                     90:                if (*fmt=='.') {
                     91:                        if (*++fmt == '*') {
                     92:                                prec =  (( int *)(ap += sizeof( int)))[-1];
                     93:                                fmt++;
                     94:                        }
                     95:                        else {
                     96:                                prec = 0;
                     97:                                while (*fmt>='0' && *fmt<='9')
                     98:                                        prec = prec * 10 + (*fmt++ - '0');
                     99:                        }
                    100:                }
                    101:                else
                    102:                        prec = -1;
                    103:                
                    104:                
                    105: 
                    106: 
                    107: 
                    108: 
                    109: 
                    110: 
                    111: 
                    112: 
                    113: 
                    114:                switch (*fmt) {
                    115:                        case 'L':
                    116:                        case 'l':
                    117:                                length = 2;
                    118:                                
                    119:                        case 'h':
                    120:                        case 'H':
                    121:                                length--;
                    122:                                fmt++;
                    123:                                break;
                    124:                }
                    125:                
                    126:                
                    127: 
                    128: 
                    129: 
                    130: 
                    131:                switch (fcode = *fmt++) {
                    132:                        
                    133:                        case 'c':
                    134:                                buf[0] =  (( int *)(ap += sizeof( int)))[-1];
                    135:                                ptr = bptr = &buf[0];
                    136:                                if (buf[0] != '\0')
                    137:                                        ptr++;
                    138:                                break;
                    139:                        case 's':
                    140:                                bptr =  ((char * *)(ap += sizeof(char *)))[-1];
                    141:                                if (bptr==0)
                    142:                                        bptr = (&xstr[8259]);
                    143:                                if (prec < 0)
                    144:                                        prec = 32767    ;
                    145:                                for (n=0; *bptr++ && n < prec; n++) ;
                    146:                                ptr = --bptr;
                    147:                                bptr -= n;
                    148:                                break;
                    149:                        case 'O':
                    150:                                length = 1;
                    151:                                fcode = 'o';
                    152:                                
                    153:                        case 'o':
                    154:                        case 'X':
                    155:                        case 'x':
                    156:                                if (length > 0)
                    157:                                        num =  ((long *)(ap += sizeof(long)))[-1];
                    158:                                else
                    159:                                        num = (unsigned) ((int *)(ap += sizeof(int)))[-1];
                    160:                                if (fcode=='o') {
                    161:                                        mask1 = 0x7;
                    162:                                        mask2 = 0x1fffffffL;
                    163:                                        nbits = 3;
                    164:                                }
                    165:                                else {
                    166:                                        mask1 = 0xf;
                    167:                                        mask2 = 0x0fffffffL;
                    168:                                        nbits = 4;
                    169:                                }
                    170:                                n = (num!=0);
                    171:                                bptr = buf + 11  + 3;
                    172:                                
                    173:                                do
                    174:                                    if (((int) num & mask1) < 10)
                    175:                                        *--bptr = ((int) num & mask1) + 060;
                    176:                                    else
                    177:                                        *--bptr = ((int) num & mask1) + 0127;
                    178:                                while (num = (num >> nbits) & mask2);
                    179:                                
                    180:                                if (fcode=='o') {
                    181:                                        if (n)
                    182:                                                *--bptr = '0';
                    183:                                }
                    184:                                else
                    185:                                        if (!sign && fill <= 0) {
                    186:                                                putchar('0');
                    187:                                                putchar(fcode);
                    188:                                                width -= 2;
                    189:                                        }
                    190:                                        else {
                    191:                                                *--bptr = fcode;
                    192:                                                *--bptr = '0';
                    193:                                        }
                    194:                                ptr = buf + 11   + 3;
                    195:                                break;
                    196:                        case 'D':
                    197:                        case 'U':
                    198:                        case 'I':
                    199:                                length = 1;
                    200:                                fcode = fcode + 'a' - 'A';
                    201:                                
                    202:                        case 'd':
                    203:                        case 'i':
                    204:                        case 'u':
                    205:                                if (length > 0)
                    206:                                        num =  ((long *)(ap += sizeof(long)))[-1];
                    207:                                else {
                    208:                                        n =  ((int *)(ap += sizeof(int)))[-1];
                    209:                                        if (fcode=='u')
                    210:                                                num = (unsigned) n;
                    211:                                        else
                    212:                                                num = (long) n;
                    213:                                }
                    214:                                if (n = (fcode != 'u' && num < 0))
                    215:                                        num = -num;
                    216:                                
                    217:                                bptr = _p_dconv(num, buf);
                    218:                                if (n)
                    219:                                        *--bptr = '-';
                    220:                                if (fill == 0)
                    221:                                        fill = -1;
                    222:                                ptr = buf + 10   + 1;
                    223:                                break;
                    224:                        default:
                    225:                                
                    226: 
                    227: 
                    228:                                ptr = bptr = &fcode;
                    229:                                ptr++;
                    230:                                break;
                    231:                        }
                    232:                        if (fcode != '\0')
                    233:                                _p_emit(bptr,ptr);
                    234:        }
                    235:        ;
                    236: }
                    237: 
                    238: 
                    239: 
                    240: 
                    241: 
                    242: 
                    243: 
                    244: 
                    245: 
                    246: 
                    247: 
                    248: char *
                    249: _p_dconv(value, buffer)
                    250:        long value;
                    251:        char *buffer;
                    252: {
                    253:        register char *bp;
                    254:        register int svalue;
                    255:        int n;
                    256:        long lval;
                    257:        
                    258:        bp = buffer;
                    259:        
                    260:        
                    261:        if (value == 0) {
                    262:                bp += 10        ;
                    263:                *bp = '0';
                    264:                return(bp);
                    265:        }
                    266:        
                    267:        
                    268:        n = 0;
                    269:        while (value < 0) {
                    270:                value -= 1000000000  ;  
                    271:                n++;
                    272:        }
                    273:        while ((lval = value - 1000000000  ) >= 0) {
                    274:                value = lval;
                    275:                n++;
                    276:        }
                    277:        
                    278:        
                    279:        bp[1] = n + '0';
                    280:        
                    281: 
                    282: 
                    283: 
                    284: 
                    285: 
                    286:        bp += 10         + 1;
                    287:        while (value > 32767    ) {
                    288:                *--bp = (int)(value % 10) + '0';
                    289:                value /= 10;
                    290:        }
                    291:        
                    292:        
                    293:        svalue = value;
                    294:        while (svalue > 0) {
                    295:                *--bp = (svalue % 10) + '0';
                    296:                svalue /= 10;
                    297:        }
                    298:        
                    299:        
                    300:        if (buffer[1] != '0') {
                    301:                while (bp > buffer + 2)
                    302:                        *--bp = '0';
                    303:                --bp;
                    304:        }
                    305:        return(bp);
                    306: }
                    307: 
                    308: 
                    309: 
                    310: 
                    311: 
                    312: 
                    313: 
                    314: 
                    315: 
                    316: 
                    317: 
                    318: 
                    319: 
                    320: _p_emit(s, send)
                    321:        register char *s;
                    322:        char *send;
                    323: {
                    324:        char cfill;
                    325:        register int alen;
                    326:        int npad;
                    327:        
                    328:        alen = send - s;
                    329:        if (alen > width)
                    330:                width = alen;
                    331:        cfill = fill>0? ' ': '0';
                    332:        
                    333:        
                    334:        if (*s == '-' && fill < 0) {
                    335:                putchar(*s++);
                    336:                alen--;
                    337:                width--;
                    338:        }
                    339:        npad = width - alen;
                    340:        
                    341:        
                    342:        if (!sign)
                    343:                while (--npad >= 0)
                    344:                        putchar(cfill);
                    345:                        
                    346:        
                    347:        while (--alen >= 0)
                    348:                putchar(*s++);
                    349:                
                    350:        
                    351:        if (sign)
                    352:                while (--npad >= 0)
                    353:                        putchar(cfill);
                    354: }

unix.superglobalmegacorp.com

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