| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /* 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 <sys/types.h>
- #include <stdarg.h>
- 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];
- va_list args;
- int i;
- 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;
- }
- /* 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, c, state = 0, bufindex;
- char *t;
-
- bufindex = 0;
- for (i = 0; (c = s[i]); i++) {
- if (c == '%') {
- if (unlikely(!state))
- state = 1;
- else
- state = 0;
- continue;
- }
- else if (likely(!state)) {
- output(c);
- continue;
- }
- switch (c) {
- case 'c':
- output(va_arg(args, int));
- break;
- case 's':
- t = va_arg(args, char *);
- while (*t)
- output(*t++);
- break;
- default:
- output('E');
- (void) va_arg(args, int);
- break;
- }
- state = 0;
- }
- return i;
- }
|