| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- /* kernel/vsprintf.c
- *
- * This file is part of evx.
- *
- * evx is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * evx is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with evx. If not, see <https://www.gnu.org/licenses/>
- */
- #include <kernel/init.h>
- #include <kernel/module.h>
- #include <kernel/kernel.h>
- #include <arch/string.h>
- #include <arch/archinfo.h>
- #include <sys/types.h>
- #include <stdarg.h>
- /* vsprintf() are almost copied from linux, but ... well, I have no
- * words to say.
- *
- * and you should know, we removed a lot of useless things (useful in
- * userspace) in kernel. */
- int cano_putc(int);
- int printk(char *s, ...) {
- /* 512 bytes are enough for kernel, because kernelers are
- * very very good, I think :-)
- *
- * but you must know this truth, because more long 's' can
- * make a stack-overflow */
- char buf[512] = { 0 };
- va_list args;
- int i;
- memset(buf, 0, 512);
- va_start(args, s);
- i = vsprintf(buf, s, args);
- va_end(args);
- for (i = 0; buf[i]; i++)
- /* will be changed by tty_write etc. in the future */
- cano_putc(buf[i]);
- return i;
- }
- static const char digits[] = "0123456789abcdef";
- #define ZERO 1
- #define UNSIGNED 2
- static int number(char *buf, long nr, int base, int flags, int width) {
- int neg = 0, i = 0, j;
- char t[PTRSIZE * 2];
- memset(t, 0, PTRSIZE * 2);
- if (nr < 0) {
- nr = -nr;
- if (!(flags & UNSIGNED))
- neg = 1;
- }
- if (!nr)
- t[i++] = '0';
- else while (nr) {
- t[i++] = digits[nr % base];
- nr /= base;
- }
- if (neg)
- t[i++] = '-';
-
- if (flags & ZERO) {
- if (width < 0)
- width = PTRSIZE * 2;
- for (j = width - 1; j; j--) {
- if (t[j])
- break;
-
- t[j] = '0';
- }
- i = PTRSIZE * 2;
- }
- /* t[i] is always 0 */
- j = i;
- while (--j >= 0)
- buf[i - j - 1] = t[j];
- return i;
- }
- /* the function used to format a string, using stdargs */
- #define output(c) buf[bufindex++] = c
- int vsprintf(char *buf, const char *s, va_list args) {
- int i, bufindex, flag;
- char *t, *fmt;
-
- bufindex = 0;
- for (i = 0, fmt = (char *) s; *fmt; fmt++, i++) {
- if (likely(*fmt != '%')) {
- output(*fmt);
- continue;
- }
- /* skip '%' */
- fmt++;
- if (*fmt == '0') {
- flag |= ZERO;
- fmt++;
- }
- switch (*fmt) {
- case 'c':
- output(va_arg(args, int));
- break;
- case 's':
- t = va_arg(args, char *);
-
- while (*t)
- output(*t++);
- break;
- case 'd':
- bufindex += number(buf + bufindex, va_arg(args, int), 10, flag, -1);
- break;
- case 'x':
- case 'p':
- /* ignore upcase */
- bufindex += number(buf + bufindex, va_arg(args, int), 16,
- flag | UNSIGNED | ZERO, PTRSIZE * 2);
- break;
- default:
- output('%');
- output(*fmt);
- (void) va_arg(args, int);
- break;
- }
- }
- return i;
- }
|