|
|
1.1 ! root 1: #include <stdio.h> ! 2: ! 3: #define BSIZE 4096 ! 4: #define STRSIZE 1024 ! 5: #define BTOTAL (BSIZE+STRSIZE) ! 6: ! 7: int nosharp, syncstdio; ! 8: ! 9: static char buffer[BTOTAL]; ! 10: static char *bufpt = buffer; ! 11: ! 12: char *sprintxl(), *sprintd(), *sprinto(), *sprintX(); ! 13: ! 14: /* ! 15: main() ! 16: { ! 17: printx("string: %s, decimals: %d, %d, %d, %d\npercent: %%, sharp: %#\n", ! 18: "this is it", 123456, 2147483647, -2147483647, -2147483648); ! 19: printx("octal: %o, hex: %x, char: %c\n", ! 20: 0123456, 0x123456, 'z'); ! 21: exit(0); ! 22: } ! 23: */ ! 24: ! 25: printx(fmt, list) ! 26: char *fmt; long list; ! 27: { ! 28: if (bufpt >= &buffer[BSIZE]) ! 29: flushx(); ! 30: bufpt = sprintxl(bufpt, fmt, &list); ! 31: if (syncstdio) ! 32: flushx(); ! 33: } ! 34: ! 35: printbuf(p, n) ! 36: register char *p; register int n; ! 37: { ! 38: register int ncp; ! 39: while (n > 0) { ! 40: if (bufpt >= &buffer[BTOTAL]) ! 41: flushx(); ! 42: ncp = &buffer[BTOTAL] - bufpt; ! 43: if (n < ncp) ! 44: ncp = n; ! 45: memcpy(bufpt, p, ncp); ! 46: bufpt += ncp; ! 47: p += ncp; ! 48: n -= ncp; ! 49: } ! 50: if (syncstdio) ! 51: flushx(); ! 52: } ! 53: ! 54: flushx() ! 55: { ! 56: register int ncp; ! 57: if (ncp = bufpt - buffer) ! 58: if (syncstdio) ! 59: fwrite(bufpt = buffer, sizeof(char), ncp, stdout); ! 60: else ! 61: write(fileno(stdout), bufpt=buffer, ncp); ! 62: } ! 63: ! 64: char * ! 65: sprintx(str, fmt, list) ! 66: char *str, *fmt; long list; ! 67: { ! 68: return sprintxl(str, fmt, &list); ! 69: } ! 70: ! 71: char * ! 72: sprintxl(str, fmt, lp) ! 73: register char *str, *fmt; register long *lp; ! 74: { ! 75: register c; register char *p; char *sprintd(); ! 76: while (c = *fmt++) switch (c) { ! 77: case '%': casePCT: ! 78: switch (c = *fmt++) { ! 79: case 'd': ! 80: str = sprintd(str, *lp++); break; ! 81: case 's': ! 82: for (p = (char *)(*lp++); *p;) ! 83: *str++ = *p++; ! 84: break; ! 85: case 'o': ! 86: str = sprinto(str, *lp++); break; ! 87: case 'x': ! 88: str = sprintX(str, *lp++); break; ! 89: case 'c': ! 90: *str++ = *lp++; break; ! 91: case '%': ! 92: case '#': ! 93: *str++ = c; break; ! 94: case 'l': ! 95: goto casePCT; ! 96: default: ! 97: *str++ = '%'; *str++ = c; ! 98: if (c == 0) return --str; ! 99: break; ! 100: } ! 101: break; ! 102: case '#': ! 103: if (nosharp == 0) *str++ = c; break; ! 104: default: ! 105: *str++ = c; break; ! 106: } ! 107: *str = 0; ! 108: return str; ! 109: } ! 110: ! 111: static int pwrd[] = { ! 112: 0, 10, 100, 1000, 10000, 100000, 1000000, ! 113: 10000000, 100000000, 1000000000, 0xffffffff, ! 114: }; ! 115: ! 116: static char mostneg[] = "-2147483648"; ! 117: ! 118: char * ! 119: sprintd(str, x) ! 120: register char *str; register int x; ! 121: { ! 122: register int *p = &pwrd[1], d; ! 123: ! 124: if (x < 0) { ! 125: x = -x; ! 126: if (x < 0) { ! 127: memcpy(str, mostneg, sizeof mostneg); ! 128: return str + (sizeof mostneg) - 1; ! 129: } ! 130: *str++ = '-'; ! 131: } ! 132: while ((unsigned)(*p++) <= (unsigned)x) ! 133: /* void */; ! 134: --p; ! 135: while (*--p) { ! 136: d = x / *p; ! 137: x -= d * *p; ! 138: *str++ = d + '0'; ! 139: } ! 140: *str++ = x + '0'; ! 141: *str = 0; ! 142: return str; ! 143: } ! 144: ! 145: static unsigned int pwro[] = { ! 146: 010, 0100, 01000, 010000, 0100000, 01000000, ! 147: 010000000, 0100000000, 01000000000, 010000000000, 0, ! 148: }; ! 149: ! 150: char * ! 151: sprinto(str, x) ! 152: register char *str; register unsigned int x; ! 153: { ! 154: register int i = 0; ! 155: while (pwro[i] && (pwro[i] <= x)) ! 156: ++i; ! 157: i *= 3; ! 158: do ! 159: *str++ = ((x>>i)&07) + '0'; ! 160: while ((i -= 3) >= 0); ! 161: *str = 0; ! 162: return str; ! 163: } ! 164: ! 165: static unsigned int pwrx[] = { ! 166: 0x10, 0x100, 0x1000, 0x10000, 0x100000, 0x1000000, ! 167: 0x10000000, 0, ! 168: }; ! 169: ! 170: static char hexit[] = { ! 171: '0', '1', '2', '3', '4', '5', '6', '7', ! 172: '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', ! 173: }; ! 174: ! 175: char * ! 176: sprintX(str, x) ! 177: register char *str; register unsigned int x; ! 178: { ! 179: register int i = 0; ! 180: while (pwrx[i] && (pwrx[i] <= x)) ! 181: ++i; ! 182: i *= 4; ! 183: do ! 184: *str++ = hexit[(x>>i)&0xf]; ! 185: while ((i -= 4) >= 0); ! 186: *str = 0; ! 187: return str; ! 188: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.