• 操作系统开发中printf函数的简单实现


    虽然没有一行注释,相信研究方向是操作系统的你也能够看懂吧,是啊,它实在是太傻太天真了,我真的希望自己的代码很慌很保利! 

     

    1. // printf.c
    2. #include "stdio.h"
    3. unsigned int strlen_(const char* s);
    4. unsigned int div_(unsigned int* i, unsigned int n);
    5. void i2a(char* buf, unsigned int a, unsigned int b);
    6. int vsprintf_(char* buf, const char* format_s, char* temp);
    7. int printf_(const char* format_s, ...);
    8. int main(void) {
    9. printf_(" %%% %%%% %c %s %d %% %x %b ",
    10. 'L', "Hello, World!...", 0xff, 256, 65535);
    11. }
    12. unsigned int strlen_(const char* s) {
    13. const char* t = s;
    14. while(*t) {
    15. t++;
    16. }
    17. return t - s;
    18. }
    19. unsigned int div_(unsigned int* i, unsigned int n) {
    20. unsigned int res = *i % n;
    21. *i /= n;
    22. return res;
    23. }
    24. void i2a(char* buf, unsigned int a, unsigned int b) {
    25. char* res = buf, * p;
    26. if(!a) {
    27. *res++ = '0';
    28. *res = '\0';
    29. return;
    30. }
    31. const char num[32] = "0123456789abcdef";
    32. char t[0x100];
    33. p = t;
    34. while(a) {
    35. *p++ = num[div_(&a, b)];
    36. }
    37. *p = '\0';
    38. while(p != t - 1) {
    39. *res++ = *--p;
    40. }
    41. *res = '\0';
    42. }
    43. int vsprintf_(char* buf, const char* format_s, char* temp) {
    44. const char* s = format_s;
    45. char t[0x100], * p, * res = buf;
    46. unsigned int i;
    47. while(*s) {
    48. if(*s == '%') {
    49. s++;
    50. switch(*s) {
    51. case '%':
    52. *buf++ = *s;
    53. break;
    54. case 'c':
    55. i = *((char*)(temp+=4));
    56. *buf++ = (char)i;
    57. break;
    58. case 's':
    59. p = *((char**)(temp+=4));
    60. while(*p) {
    61. *buf++ = *p++;
    62. }
    63. break;
    64. case 'd':
    65. i = *((int*)(temp+=4));
    66. i2a(t, i, 10);
    67. p = t;
    68. while(*p) {
    69. *buf++ = *p++;
    70. }
    71. break;
    72. case 'x':
    73. i = *((int*)(temp+=4));
    74. i2a(t, i, 16);
    75. *buf++ = '0';
    76. *buf++ = 'x';
    77. p = t;
    78. while(*p) {
    79. *buf++ = *p++;
    80. }
    81. break;
    82. case 'b':
    83. i = *((int*)(temp+=4));
    84. i2a(t, i, 2);
    85. p = t;
    86. while(*p) {
    87. *buf++ = *p++;
    88. }
    89. *buf++ = 'b';
    90. break;
    91. }
    92. s++;
    93. } else {
    94. *buf++ = *s++;
    95. }
    96. }
    97. *buf = '\0';
    98. return strlen_(res);
    99. }
    100. int printf_(const char* format_s, ...) {
    101. char t[0x400];
    102. char* temp = (char*)&format_s;
    103. vsprintf_(t, format_s, temp);
    104. temp = NULL;
    105. unsigned int i = 0;
    106. while(t[i]) {
    107. printf("%c", t[i++]);
    108. }
    109. return i;
    110. }

  • 相关阅读:
    centos7的忘记root管理员账号的登录密码
    Verilig语法之——Generate 结构
    011-盛最多水的容器-力扣
    PyQt5的安装和配置
    【JavaScript】制作一个抽奖转盘页面
    UTONMOS带您体验数字人 感受元宇宙
    Prometheus存储容量估算和Prometheus联邦机制
    百趣代谢组学文献分享:茶褐素可促进胆固醇降解
    linux挂载MTP
    spring事务
  • 原文地址:https://blog.csdn.net/weixin_39410618/article/details/125616437