vsprintf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <sys/types.h>
  22. #include <stdarg.h>
  23. /* vsprintf() are almost copied from linux, but ... well, I have no
  24. * words to say.
  25. *
  26. * and you should know, we removed a lot of useless things (useful in
  27. * userspace) in kernel. */
  28. int cano_putc(int);
  29. int printk(char *s, ...) {
  30. /* 512 bytes are enough for kernel, because kernelers are
  31. * very very good, I think :-)
  32. *
  33. * but you must know this truth, because more long 's' can
  34. * make a stack-overflow */
  35. char buf[512];
  36. va_list args;
  37. int i;
  38. va_start(args, s);
  39. i = vsprintf(buf, s, args);
  40. va_end(args);
  41. for (i = 0; buf[i]; i++)
  42. /* will be changed by tty_write etc. in the future */
  43. cano_putc(buf[i]);
  44. return i;
  45. }
  46. static const char digits[] = "0123456789abcdef";
  47. #define ZERO 1
  48. #define UNSIGNED 2
  49. static int number(char *buf, long nr, int base, int flags, int width) {
  50. int neg = 0, i = 0, j;
  51. char t[16] = { 0 };
  52. if (nr < 0) {
  53. nr = -nr;
  54. if (!(flags & UNSIGNED))
  55. neg = 1;
  56. }
  57. do {
  58. t[i++] = digits[nr % base];
  59. } while (nr /= base);
  60. if (neg)
  61. t[i++] = '-';
  62. if (flags & ZERO) {
  63. if (width < 0)
  64. width = 16;
  65. for (i = 0; i <= width; i++)
  66. if (!t[i])
  67. t[i] = 0;
  68. }
  69. j = i;
  70. while (--j >= 0)
  71. buf[i - j - 1] = t[j];
  72. return i;
  73. }
  74. /* the function used to format a string, using stdargs */
  75. #define output(c) buf[bufindex++] = c
  76. int vsprintf(char *buf, const char *s, va_list args) {
  77. int i, bufindex, flag;
  78. char *t, *fmt;
  79. bufindex = 0;
  80. for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
  81. if (likely(*fmt != '%')) {
  82. output(*fmt);
  83. continue;
  84. }
  85. /* skip '%' */
  86. fmt++;
  87. if (*fmt == '0') {
  88. flag |= ZERO;
  89. fmt++;
  90. }
  91. switch (*fmt) {
  92. case 'c':
  93. output(va_arg(args, int));
  94. break;
  95. case 's':
  96. t = va_arg(args, char *);
  97. while (*t)
  98. output(*t++);
  99. break;
  100. case 'd':
  101. bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
  102. break;
  103. case 'x':
  104. case 'p':
  105. /* ignore upcase */
  106. bufindex += number(buf + bufindex, va_arg(args, int), 16, flag | UNSIGNED, 16);
  107. break;
  108. default:
  109. output('%');
  110. output(*fmt);
  111. (void) va_arg(args, int);
  112. break;
  113. }
  114. }
  115. return i;
  116. }