Add snprintf(3) and vsnprintf(3) capability to the kernel.

Reviewed by:	bde
This commit is contained in:
archie 1998-12-03 04:45:57 +00:00
parent dd093dc581
commit 8ccd28a438
2 changed files with 58 additions and 3 deletions

View File

@ -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 */
@ -328,6 +338,49 @@ vsprintf(char *buf, const char *cfmt, va_list ap)
return retval;
}
/*
* 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?)

View File

@ -36,7 +36,7 @@
* SUCH DAMAGE.
*
* @(#)systm.h 8.7 (Berkeley) 3/29/95
* $Id: systm.h,v 1.77 1998/10/09 01:44:09 msmith Exp $
* $Id: systm.h,v 1.78 1998/10/30 05:41:15 msmith Exp $
*/
#ifndef _SYS_SYSTM_H_
@ -107,9 +107,11 @@ int kvprintf __P((char const *, void (*)(int, void*), void *, int,
void log __P((int, const char *, ...)) __printflike(2, 3);
void logwakeup __P((void));
int printf __P((const char *, ...)) __printflike(1, 2);
int snprintf __P((char *, size_t, const char *, ...)) __printflike(3, 4);
int sprintf __P((char *buf, const char *, ...)) __printflike(2, 3);
void uprintf __P((const char *, ...)) __printflike(1, 2);
void vprintf __P((const char *, _BSD_VA_LIST_)) __printflike(1, 0);
int vsnprintf __P((char *, size_t, const char *, _BSD_VA_LIST_)) __printflike(3, 0);
int vsprintf __P((char *buf, const char *, _BSD_VA_LIST_)) __printflike(2, 0);
void ttyprintf __P((struct tty *, const char *, ...)) __printflike(2, 3);