diff options
author | archie <archie@FreeBSD.org> | 1998-12-03 04:45:57 +0000 |
---|---|---|
committer | archie <archie@FreeBSD.org> | 1998-12-03 04:45:57 +0000 |
commit | 8ccd28a438a394a3aaf0e4c54acfa634195f6bc1 (patch) | |
tree | ac1acd8d8ecac5261326be10964b36121d814a41 /sys/kern/subr_prf.c | |
parent | dd093dc58135d9e5ecbcb9a6bcac72e6cbbb4022 (diff) | |
download | FreeBSD-src-8ccd28a438a394a3aaf0e4c54acfa634195f6bc1.zip FreeBSD-src-8ccd28a438a394a3aaf0e4c54acfa634195f6bc1.tar.gz |
Add snprintf(3) and vsnprintf(3) capability to the kernel.
Reviewed by: bde
Diffstat (limited to 'sys/kern/subr_prf.c')
-rw-r--r-- | sys/kern/subr_prf.c | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index f18a71a..424ac9f 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -36,7 +36,7 @@ * SUCH DAMAGE. * * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 - * $Id: subr_prf.c,v 1.49 1998/08/10 14:27:34 bde Exp $ + * $Id: subr_prf.c,v 1.50 1998/09/06 06:25:04 ache Exp $ */ #include <sys/param.h> @@ -62,12 +62,22 @@ struct tty *constty; /* pointer to console "window" tty */ +struct putchar_arg { + int flags; + struct tty *tty; +}; + +struct snprintf_arg { + char *str; + size_t remain; +}; + static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */ static void logpri __P((int level)); static void msglogchar(int c, void *dummyarg); -struct putchar_arg {int flags; struct tty *tty; }; static void putchar __P((int ch, void *arg)); static char *ksprintn __P((u_long num, int base, int *len)); +static void snprintf_func __P((int ch, void *arg)); static int consintr = 1; /* Ok to handle console interrupts? */ static int msgbufmapped; /* Set when safe to use msgbuf */ @@ -329,6 +339,49 @@ vsprintf(char *buf, const char *cfmt, va_list ap) } /* + * Scaled down version of snprintf(3). + */ +int +snprintf(char *str, size_t size, const char *format, ...) +{ + int retval; + va_list ap; + + va_start(ap, format); + retval = vsnprintf(str, size, format, ap); + va_end(ap); + return(retval); +} + +/* + * Scaled down version of vsnprintf(3). + */ +int +vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + struct snprintf_arg info; + int retval; + + info.str = str; + info.remain = size; + retval = kvprintf(format, snprintf_func, &info, 10, ap); + if (info.remain >= 1) + *info.str++ = '\0'; + return retval; +} + +static void +snprintf_func(int ch, void *arg) +{ + struct snprintf_arg *const info = arg; + + if (info->remain >= 2) { + *info->str++ = ch; + info->remain--; + } +} + +/* * Put a number (base <= 16) in a buffer in reverse order; return an * optional length and a pointer to the NULL terminated (preceded?) * buffer. |