vsprintf.c 3.6 KB

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