vsprintf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * i don't know why but va_arg can only get first 6 arguments on x86_64
  31. * and 6 is also the number of arguments store in registers. I'm trying
  32. * i386 */
  33. static const char digits[] = "0123456789abcdef";
  34. #define ZERO 1
  35. #define UNSIGNED 2
  36. static int number(char *buf, long x, int base, int flags, int width) {
  37. int neg = 0, i = 0, j;
  38. char t[PTRSIZE * 2];
  39. unsigned long nr;
  40. memset(t, 0, PTRSIZE * 2);
  41. if (!(flags & UNSIGNED) && x < 0) {
  42. nr = -x;
  43. neg = 1;
  44. }
  45. else
  46. nr = x;
  47. if (!nr)
  48. t[i++] = '0';
  49. else while (nr) {
  50. t[i++] = digits[nr % base];
  51. nr /= base;
  52. }
  53. if (neg)
  54. t[i++] = '-';
  55. if (flags & ZERO) {
  56. if (width < 0)
  57. width = PTRSIZE * 2;
  58. for (j = width - 1; j; j--) {
  59. if (t[j])
  60. break;
  61. t[j] = '0';
  62. }
  63. i = PTRSIZE * 2;
  64. }
  65. /* t[i] is always 0 */
  66. j = i;
  67. while (--j >= 0)
  68. buf[i - j - 1] = t[j];
  69. return i;
  70. }
  71. /* the function used to format a string, using stdargs */
  72. #define output(c) buf[bufindex++] = c
  73. int vsprintf(char *buf, const char *s, va_list args) {
  74. int i, bufindex, flag;
  75. char *t, *fmt;
  76. bufindex = 0;
  77. for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
  78. if (likely(*fmt != '%')) {
  79. output(*fmt);
  80. continue;
  81. }
  82. /* skip '%' */
  83. fmt++;
  84. if (*fmt == '0') {
  85. flag |= ZERO;
  86. fmt++;
  87. }
  88. switch (*fmt) {
  89. case 'c':
  90. output(va_arg(args, int));
  91. break;
  92. case 's':
  93. t = va_arg(args, char *);
  94. while (*t)
  95. output(*t++);
  96. break;
  97. case 'd':
  98. bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
  99. break;
  100. case 'x':
  101. case 'p':
  102. /* ignore upcase */
  103. bufindex += number(buf + bufindex, va_arg(args, int), 16,
  104. flag | UNSIGNED | ZERO, PTRSIZE * 2);
  105. break;
  106. case '%':
  107. output('%');
  108. break;
  109. default:
  110. output('%');
  111. output(*fmt);
  112. (void) va_arg(args, int);
  113. break;
  114. }
  115. }
  116. return bufindex;
  117. }