vsprintf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/vsprintf.h>
  23. #include <kernel/schk.h>
  24. #include <arch/x86/int.h>
  25. #include <arch/io.h>
  26. #include <arch/sys.h>
  27. #include <stdarg.h>
  28. /*
  29. * vsprintf: a function that uses to format strings.
  30. *
  31. * buf: (write only) buffer for output.
  32. * fmt: formater (read only)
  33. * args: arguments
  34. *
  35. * SUPPORTS:
  36. * STANDARD FORMATERS:
  37. * %c: a character
  38. * %d: a 32-bit 10-based signed int number.
  39. * %l: a 64-bit 10-based signed long number.
  40. * %u: a 64-bit 10-based unsigned long number.
  41. * %x: a 64-bit 16-based unsigned long number.
  42. *
  43. * OTHERS:
  44. * 8 : %b: a 16-based unsigned char number.
  45. * 16: %w: a 16-based unsigned short number.
  46. * 32: %d: (defined)
  47. * 64: %q: a 16-based unsigned long number.
  48. * */
  49. int vsprintf(char *buf, const char *fmt, va_list args) {
  50. /* ri: real index in buf */
  51. __uint64_t i, ri;
  52. char state, c;
  53. state = 0;
  54. for (i = 0, ri = 0; (c = fmt[i]); i++) {
  55. if (!state)
  56. if (c == '%')
  57. state = 1;
  58. else
  59. buf[ri++] = c;
  60. else {
  61. if (c == 'c')
  62. /* gcc compiler makes a char (that 1 byte) to
  63. * a int (4 bytes) in stack and registers because
  64. * many registers haven't got a byte-size piece.
  65. * and sp must be 8-bytes aligned in long-mode,
  66. * 4-bytes in protected-mode.
  67. * */
  68. buf[ri++] = va_arg(args, int);
  69. else if (c == 's') {
  70. /* it's not so difficult, I think */
  71. /* get pointer and save in s */
  72. const char *s = va_arg(args, char *);
  73. while (*s) {
  74. buf[ri++] = *s;
  75. s++;
  76. }
  77. }
  78. else if (c == 'd')
  79. ri += itos(buf + ri, va_arg(args, int), 10, 1, 0, 0);
  80. else if (c == 'l')
  81. ri += itos(buf + ri, va_arg(args, long), 10, 1, 0, 0);
  82. else if (c == 'u')
  83. ri += itos(buf + ri, va_arg(args, unsigned long), 10, 1, 0, 0);
  84. else if (c == 'x')
  85. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 0, 0);
  86. else if (c == 'b')
  87. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1, 2);
  88. else if (c == 'w')
  89. ri += itos(buf + ri, va_arg(args, unsigned int), 16, 0, 1, 4);
  90. else if (c == 'q')
  91. ri += itos(buf + ri, va_arg(args, unsigned long), 16, 0, 1, 16);
  92. else {
  93. /* unsupported format, we should put '%' on screen too */
  94. buf[ri++] = '%';
  95. buf[ri++] = c;
  96. }
  97. state = 0;
  98. }
  99. }
  100. return ri;
  101. }
  102. int itos(char *buffer, unsigned long n, int base, int sign, int withzero, int width) {
  103. static char digits[] = "0123456789abcdef";
  104. char tbuf[16] = {0};
  105. int i, neg, j;
  106. __uint64_t x;
  107. if (sign && n < 0) {
  108. neg = 1;
  109. x = -n;
  110. }
  111. else {
  112. neg = 0;
  113. x = n;
  114. }
  115. i = 0;
  116. do {
  117. tbuf[i++] = digits[x % base];
  118. } while (x /= base);
  119. if (withzero && base == 16) {
  120. for (i = 0; i < width; i++) {
  121. if (!tbuf[i])
  122. tbuf[i] = '0';
  123. }
  124. }
  125. if (neg)
  126. tbuf[i++] = '-';
  127. j = i;
  128. while (--j >= 0)
  129. buffer[i - j - 1] = tbuf[j];
  130. return i;
  131. }