summaryrefslogtreecommitdiffstats
path: root/sys
diff options
context:
space:
mode:
authorarchie <archie@FreeBSD.org>1998-12-03 04:45:57 +0000
committerarchie <archie@FreeBSD.org>1998-12-03 04:45:57 +0000
commit8ccd28a438a394a3aaf0e4c54acfa634195f6bc1 (patch)
treeac1acd8d8ecac5261326be10964b36121d814a41 /sys
parentdd093dc58135d9e5ecbcb9a6bcac72e6cbbb4022 (diff)
downloadFreeBSD-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')
-rw-r--r--sys/kern/subr_prf.c57
-rw-r--r--sys/sys/systm.h4
2 files changed, 58 insertions, 3 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.
diff --git a/sys/sys/systm.h b/sys/sys/systm.h
index 072dc35..09b9ce5 100644
--- a/sys/sys/systm.h
+++ b/sys/sys/systm.h
@@ -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);
OpenPOWER on IntegriCloud