vsprintf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* kernel/vsprintf.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <kernel/init.h>
  21. #include <kernel/module.h>
  22. #include <kernel/kernel.h>
  23. #include <arch/string.h>
  24. #include <arch/archinfo.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. /* vsprintf() are almost copied from linux, but ... well, I have no
  28. * words to say.
  29. *
  30. * and you should know, we removed a lot of useless things (useful in
  31. * userspace) in kernel.
  32. * i don't know why but va_arg can only get first 6 arguments on x86_64
  33. * and 6 is also the number of arguments store in registers. I'm trying
  34. * i386 */
  35. static const char digits[] = "0123456789abcdef";
  36. #define ZERO 1
  37. #define UNSIGNED 2
  38. static int number(char *buf, long x, int base, int flags, int width) {
  39. int neg = 0, i = 0, j;
  40. char t[PTRSIZE * 2];
  41. unsigned long nr;
  42. memset(t, 0, PTRSIZE * 2);
  43. if (!(flags & UNSIGNED) && x < 0) {
  44. nr = -x;
  45. neg = 1;
  46. }
  47. else
  48. nr = x;
  49. if (!nr)
  50. t[i++] = '0';
  51. else while (nr) {
  52. t[i++] = digits[nr % base];
  53. nr /= base;
  54. }
  55. if (neg)
  56. t[i++] = '-';
  57. if (flags & ZERO) {
  58. if (width < 0)
  59. width = PTRSIZE * 2;
  60. for (j = width - 1; j; j--) {
  61. if (t[j])
  62. break;
  63. t[j] = '0';
  64. }
  65. i = PTRSIZE * 2;
  66. }
  67. /* t[i] is always 0 */
  68. j = i;
  69. while (--j >= 0)
  70. buf[i - j - 1] = t[j];
  71. return i;
  72. }
  73. /* the function used to format a string, using stdargs */
  74. #define output(c) buf[bufindex++] = c
  75. int vsprintf(char *buf, const char *s, va_list args) {
  76. int i, bufindex, flag;
  77. char *t, *fmt;
  78. bufindex = 0;
  79. for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
  80. if (likely(*fmt != '%')) {
  81. output(*fmt);
  82. continue;
  83. }
  84. /* skip '%' */
  85. fmt++;
  86. if (*fmt == '0') {
  87. flag |= ZERO;
  88. fmt++;
  89. }
  90. switch (*fmt) {
  91. case 'c':
  92. output(va_arg(args, int));
  93. break;
  94. case 's':
  95. t = va_arg(args, char *);
  96. while (*t)
  97. output(*t++);
  98. break;
  99. case 'd':
  100. bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
  101. break;
  102. case 'x':
  103. case 'p':
  104. /* ignore upcase */
  105. bufindex += number(buf + bufindex, va_arg(args, int), 16,
  106. flag | UNSIGNED | ZERO, PTRSIZE * 2);
  107. break;
  108. case '%':
  109. output('%');
  110. break;
  111. default:
  112. output('%');
  113. output(*fmt);
  114. (void) va_arg(args, int);
  115. break;
  116. }
  117. }
  118. return bufindex;
  119. }