虽然没有一行注释,相信研究方向是操作系统的你也能够看懂吧,是啊,它实在是太傻太天真了,我真的希望自己的代码很慌很保利!
- // printf.c
- #include "stdio.h"
-
- unsigned int strlen_(const char* s);
- unsigned int div_(unsigned int* i, unsigned int n);
- void i2a(char* buf, unsigned int a, unsigned int b);
- int vsprintf_(char* buf, const char* format_s, char* temp);
- int printf_(const char* format_s, ...);
-
-
- int main(void) {
- printf_(" %%% %%%% %c %s %d %% %x %b ",
- 'L', "Hello, World!...", 0xff, 256, 65535);
- }
-
- unsigned int strlen_(const char* s) {
- const char* t = s;
- while(*t) {
- t++;
- }
- return t - s;
- }
-
- unsigned int div_(unsigned int* i, unsigned int n) {
- unsigned int res = *i % n;
- *i /= n;
- return res;
- }
-
- void i2a(char* buf, unsigned int a, unsigned int b) {
- char* res = buf, * p;
- if(!a) {
- *res++ = '0';
- *res = '\0';
- return;
- }
- const char num[32] = "0123456789abcdef";
- char t[0x100];
- p = t;
- while(a) {
- *p++ = num[div_(&a, b)];
- }
- *p = '\0';
- while(p != t - 1) {
- *res++ = *--p;
- }
- *res = '\0';
- }
-
- int vsprintf_(char* buf, const char* format_s, char* temp) {
- const char* s = format_s;
- char t[0x100], * p, * res = buf;
- unsigned int i;
- while(*s) {
- if(*s == '%') {
- s++;
- switch(*s) {
- case '%':
- *buf++ = *s;
- break;
- case 'c':
- i = *((char*)(temp+=4));
- *buf++ = (char)i;
- break;
- case 's':
- p = *((char**)(temp+=4));
- while(*p) {
- *buf++ = *p++;
- }
- break;
- case 'd':
- i = *((int*)(temp+=4));
- i2a(t, i, 10);
- p = t;
- while(*p) {
- *buf++ = *p++;
- }
- break;
- case 'x':
- i = *((int*)(temp+=4));
- i2a(t, i, 16);
- *buf++ = '0';
- *buf++ = 'x';
- p = t;
- while(*p) {
- *buf++ = *p++;
- }
- break;
- case 'b':
- i = *((int*)(temp+=4));
- i2a(t, i, 2);
- p = t;
- while(*p) {
- *buf++ = *p++;
- }
- *buf++ = 'b';
- break;
- }
- s++;
- } else {
- *buf++ = *s++;
- }
- }
- *buf = '\0';
- return strlen_(res);
- }
-
- int printf_(const char* format_s, ...) {
- char t[0x400];
- char* temp = (char*)&format_s;
- vsprintf_(t, format_s, temp);
- temp = NULL;
- unsigned int i = 0;
- while(t[i]) {
- printf("%c", t[i++]);
- }
- return i;
- }
-
-