vsprintf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 nr, int base, int flags, int width) {
  53. int neg = 0, i = 0, j;
  54. char t[PTRSIZE * 2];
  55. memset(t, 0, PTRSIZE * 2);
  56. if (nr < 0) {
  57. nr = -nr;
  58. if (!(flags & UNSIGNED))
  59. neg = 1;
  60. }
  61. if (!nr)
  62. t[i++] = '0';
  63. else while (nr) {
  64. t[i++] = digits[nr % base];
  65. nr /= base;
  66. }
  67. if (neg)
  68. t[i++] = '-';
  69. if (flags & ZERO) {
  70. if (width < 0)
  71. width = PTRSIZE * 2;
  72. for (j = width - 1; j; j--) {
  73. if (t[j])
  74. break;
  75. t[j] = '0';
  76. }
  77. i = PTRSIZE * 2;
  78. }
  79. /* t[i] is always 0 */
  80. j = i;
  81. while (--j >= 0)
  82. buf[i - j - 1] = t[j];
  83. return i;
  84. }
  85. /* the function used to format a string, using stdargs */
  86. #define output(c) buf[bufindex++] = c
  87. int vsprintf(char *buf, const char *s, va_list args) {
  88. int i, bufindex, flag;
  89. char *t, *fmt;
  90. bufindex = 0;
  91. for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
  92. if (likely(*fmt != '%')) {
  93. output(*fmt);
  94. continue;
  95. }
  96. /* skip '%' */
  97. fmt++;
  98. if (*fmt == '0') {
  99. flag |= ZERO;
  100. fmt++;
  101. }
  102. switch (*fmt) {
  103. case 'c':
  104. output(va_arg(args, int));
  105. break;
  106. case 's':
  107. t = va_arg(args, char *);
  108. while (*t)
  109. output(*t++);
  110. break;
  111. case 'd':
  112. bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
  113. break;
  114. case 'x':
  115. case 'p':
  116. /* ignore upcase */
  117. bufindex += number(buf + bufindex, va_arg(args, int), 16,
  118. flag | UNSIGNED | ZERO, PTRSIZE * 2);
  119. break;
  120. default:
  121. output('%');
  122. output(*fmt);
  123. (void) va_arg(args, int);
  124. break;
  125. }
  126. }
  127. return i;
  128. }