vsprintf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/errno.h>
  21. #include <kernel/types.h>
  22. #include <kernel/graphic.h>
  23. #include <kernel/vsprintf.h>
  24. #include <kernel/schk.h>
  25. #include <arch/x86/int.h>
  26. #include <arch/io.h>
  27. #include <arch/sys.h>
  28. #include <stdarg.h>
  29. /*
  30. * vsprintf: a function that uses to format strings.
  31. *
  32. * buf: (write only) buffer for output.
  33. * fmt: formater (read only)
  34. * args: arguments
  35. *
  36. * SUPPORTS:
  37. * STANDARD FORMATERS:
  38. * %c: a character
  39. * %d: a 32-bit 10-based signed int number.
  40. * %l: a 64-bit 10-based signed long number.
  41. * %u: a 64-bit 10-based unsigned long number.
  42. * %x: a 64-bit 16-based unsigned long number.
  43. *
  44. * OTHERS:
  45. * 8 : %b: a 16-based unsigned char number.
  46. * 16: %w: a 16-based unsigned short number.
  47. * 32: %d: (defined)
  48. * 64: %q: a 16-based unsigned long number.
  49. * */
  50. int vsprintf(char *buf, char *fmt, va_list args) {
  51. schk_init();
  52. /* ri: real index in buf */
  53. __uint64_t i, ri;
  54. char state, c;
  55. state = 0;
  56. for (i = 0, ri = 0; (c = fmt[i]); i++) {
  57. if (!state)
  58. if (c == '%')
  59. state = 1;
  60. else
  61. buf[ri++] = c;
  62. else {
  63. if (c == 'c')
  64. /* gcc compiler makes a char (that 1 byte) to
  65. * a int (4 bytes) in stack and registers because
  66. * many registers haven't got a byte-size piece.
  67. * and sp must be 8-bytes aligned in long-mode,
  68. * 4-bytes in protected-mode.
  69. * */
  70. buf[ri++] = va_arg(args, int);
  71. else if (c == 's') {
  72. /* it's not so difficult, I think */
  73. /* get pointer and save in s */
  74. char *s = va_arg(args, char *);
  75. while (*s) {
  76. buf[ri++] = *s;
  77. s++;
  78. }
  79. }
  80. else if (c == 'd')
  81. ri += itos(buf + ri, va_arg(args, int), 10, 1, 0);
  82. else if (c == 'l')
  83. ri += itos(buf + ri, va_arg(args, long), 10, 1, 0);
  84. else if (c == 'u')
  85. ri += itos(buf + ri, va_arg(args, unsigned long), 10, 1, 0);
  86. else if (c == 'x')
  87. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 0);
  88. else if (c == 'b')
  89. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
  90. else if (c == 'w')
  91. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
  92. else if (c == 'q')
  93. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 1);
  94. else {
  95. /* unsupported format, we should put '%' on screen too */
  96. buf[ri++] = '%';
  97. buf[ri++] = c;
  98. }
  99. state = 0;
  100. }
  101. }
  102. schk_leave();
  103. return ri;
  104. }
  105. int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
  106. schk_init();
  107. static char digits[] = "0123456789abcdef";
  108. char tbuf[16] = {0};
  109. int i, neg, j;
  110. __uint64_t x;
  111. if (sign && n < 0) {
  112. neg = 1;
  113. x = -n;
  114. }
  115. else {
  116. neg = 0;
  117. x = n;
  118. }
  119. i = 0;
  120. do {
  121. tbuf[i++] = digits[x % base];
  122. } while (x /= base);
  123. if (withzero && base == 16) {
  124. for (i = 0; i < 16; i++) {
  125. if (!tbuf[i])
  126. tbuf[i] = '0';
  127. }
  128. }
  129. if (neg)
  130. tbuf[i++] = '-';
  131. j = i;
  132. while (--j >= 0)
  133. buffer[i - j - 1] = tbuf[j];
  134. schk_leave();
  135. return i;
  136. }