vsprintf.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* kernel/vsprintf.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx 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. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <kernel/init.h>
  19. #include <kernel/module.h>
  20. #include <kernel/kernel.h>
  21. #include <arch/string.h>
  22. #include <arch/archinfo.h>
  23. #include <sys/types.h>
  24. #include <stdarg.h>
  25. /* vsprintf() are almost copied from linux, but ... well, I have no
  26. * words to say.
  27. *
  28. * and you should know, we removed a lot of useless things (useful in
  29. * userspace) in kernel. */
  30. int cano_putc(int);
  31. int printk(char *s, ...) {
  32. /* 512 bytes are enough for kernel, because kernelers are
  33. * very very good, I think :-)
  34. *
  35. * but you must know this truth, because more long 's' can
  36. * make a stack-overflow */
  37. char buf[512] = { 0 };
  38. va_list args;
  39. int i;
  40. memset(buf, 0, 512);
  41. va_start(args, s);
  42. i = vsprintf(buf, s, args);
  43. va_end(args);
  44. for (i = 0; buf[i]; i++)
  45. /* will be changed by tty_write etc. in the future */
  46. cano_putc(buf[i]);
  47. return i;
  48. }
  49. static const char digits[] = "0123456789abcdef";
  50. #define ZERO 1
  51. #define UNSIGNED 2
  52. static int number(char *buf, long x, int base, int flags, int width) {
  53. int neg = 0, i = 0, j;
  54. char t[PTRSIZE * 2];
  55. unsigned long nr;
  56. memset(t, 0, PTRSIZE * 2);
  57. if (!(flags & UNSIGNED) && nr < 0) {
  58. nr = -x;
  59. neg = 1;
  60. }
  61. else
  62. nr = x;
  63. if (!nr)
  64. t[i++] = '0';
  65. else while (nr) {
  66. t[i++] = digits[nr % base];
  67. nr /= base;
  68. }
  69. if (neg)
  70. t[i++] = '-';
  71. if (flags & ZERO) {
  72. if (width < 0)
  73. width = PTRSIZE * 2;
  74. for (j = width - 1; j; j--) {
  75. if (t[j])
  76. break;
  77. t[j] = '0';
  78. }
  79. i = PTRSIZE * 2;
  80. }
  81. /* t[i] is always 0 */
  82. j = i;
  83. while (--j >= 0)
  84. buf[i - j - 1] = t[j];
  85. return i;
  86. }
  87. /* the function used to format a string, using stdargs */
  88. #define output(c) buf[bufindex++] = c
  89. int vsprintf(char *buf, const char *s, va_list args) {
  90. int i, bufindex, flag;
  91. char *t, *fmt;
  92. bufindex = 0;
  93. for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
  94. if (likely(*fmt != '%')) {
  95. output(*fmt);
  96. continue;
  97. }
  98. /* skip '%' */
  99. fmt++;
  100. if (*fmt == '0') {
  101. flag |= ZERO;
  102. fmt++;
  103. }
  104. switch (*fmt) {
  105. case 'c':
  106. output(va_arg(args, int));
  107. break;
  108. case 's':
  109. t = va_arg(args, char *);
  110. while (*t)
  111. output(*t++);
  112. break;
  113. case 'd':
  114. bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
  115. break;
  116. case 'x':
  117. case 'p':
  118. /* ignore upcase */
  119. bufindex += number(buf + bufindex, va_arg(args, int), 16,
  120. flag | UNSIGNED | ZERO, PTRSIZE * 2);
  121. break;
  122. case '%':
  123. output('%');
  124. break;
  125. default:
  126. output('%');
  127. output(*fmt);
  128. (void) va_arg(args, int);
  129. break;
  130. }
  131. }
  132. return i;
  133. }