vsprintf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. * %o: a 64-bit 8-based unsigned long number.
  44. *
  45. * OTHERS:
  46. * 8 : %b: a 16-based unsigned char number.
  47. * 16: %w: a 16-based unsigned short number.
  48. * 32: %d: (defined)
  49. * 64: %q: a 16-based unsigned long number.
  50. * */
  51. int vsprintf(char *buf, char *fmt, va_list args) {
  52. schk_init();
  53. /* ri: real index in buf */
  54. __uint64_t i, ri;
  55. char state, c;
  56. state = 0;
  57. for (i = 0, ri = 0; (c = fmt[i]); i++) {
  58. if (!state)
  59. if (c == '%')
  60. state = 1;
  61. else
  62. buf[ri++] = c;
  63. else {
  64. if (c == 'c')
  65. /* gcc compiler makes a char (that 1 byte) to
  66. * a int (4 bytes) in stack and registers because
  67. * many registers haven't got a byte-size piece.
  68. * and sp must be 8-bytes aligned in long-mode,
  69. * 4-bytes in protected-mode.
  70. * */
  71. buf[ri++] = va_arg(args, int);
  72. else if (c == 's') {
  73. /* it's not so difficult, I think */
  74. /* get pointer and save in s */
  75. char *s = va_arg(args, char *);
  76. while (*s) {
  77. buf[ri++] = *s;
  78. s++;
  79. }
  80. }
  81. else if (c == 'd')
  82. ri += itos(buf + ri, va_arg(args, int), 10, 1, 0);
  83. else if (c == 'l')
  84. ri += itos(buf + ri, va_arg(args, long), 10, 1, 0);
  85. else if (c == 'u')
  86. ri += itos(buf + ri, va_arg(args, unsigned long), 10, 1, 0);
  87. else if (c == 'x')
  88. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 0);
  89. else if (c == 'o')
  90. ri += itos(buf + ri, va_arg(args, unsigned long), 8, 0, 0);
  91. else if (c == 'b')
  92. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
  93. else if (c == 'w')
  94. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1);
  95. else if (c == 'q')
  96. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 1);
  97. else {
  98. /* unsupported format, we should put '%' on screen too */
  99. buf[ri++] = '%';
  100. buf[ri++] = c;
  101. }
  102. state = 0;
  103. }
  104. }
  105. schk_leave();
  106. return ri;
  107. }
  108. int itos(char *buffer, unsigned long n, int base, int sign, int withzero) {
  109. schk_init();
  110. static char digits[] = "0123456789abcdef";
  111. char tbuf[32];
  112. int i, neg, j;
  113. __uint64_t x;
  114. if (sign && n < 0) {
  115. neg = 1;
  116. x = -n;
  117. }
  118. else {
  119. neg = 0;
  120. x = n;
  121. }
  122. i = 0;
  123. do {
  124. tbuf[i++] = digits[x % base];
  125. } while (x /= base);
  126. if (withzero && (base == 8 || base == 16)) {
  127. /* fill zero */
  128. int i;
  129. for (i = 0; i < 32; i++)
  130. if (!tbuf[i])
  131. tbuf[i] = '0';
  132. }
  133. if (neg)
  134. tbuf[i++] = '-';
  135. j = i;
  136. while (--j >= 0)
  137. buffer[i - j - 1] = tbuf[j];
  138. schk_leave();
  139. return i;
  140. }