vsprintf.c 3.5 KB

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