xref: /openbsd-src/sys/lib/libsa/snprintf.c (revision 48789c6b3cc6d5509061289b43f25a60f25906ef)
1*48789c6bSderaadt /*	$OpenBSD: snprintf.c,v 1.7 2019/05/11 16:56:47 deraadt Exp $	*/
21ee9984cSderaadt /*	$NetBSD: printf.c,v 1.10 1996/11/30 04:19:21 gwr Exp $	*/
31ee9984cSderaadt 
41ee9984cSderaadt /*-
51ee9984cSderaadt  * Copyright (c) 1993
61ee9984cSderaadt  *	The Regents of the University of California.  All rights reserved.
71ee9984cSderaadt  *
81ee9984cSderaadt  * Redistribution and use in source and binary forms, with or without
91ee9984cSderaadt  * modification, are permitted provided that the following conditions
101ee9984cSderaadt  * are met:
111ee9984cSderaadt  * 1. Redistributions of source code must retain the above copyright
121ee9984cSderaadt  *    notice, this list of conditions and the following disclaimer.
131ee9984cSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
141ee9984cSderaadt  *    notice, this list of conditions and the following disclaimer in the
151ee9984cSderaadt  *    documentation and/or other materials provided with the distribution.
161fd2a284Smillert  * 3. Neither the name of the University nor the names of its contributors
171ee9984cSderaadt  *    may be used to endorse or promote products derived from this software
181ee9984cSderaadt  *    without specific prior written permission.
191ee9984cSderaadt  *
201ee9984cSderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211ee9984cSderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221ee9984cSderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231ee9984cSderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241ee9984cSderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251ee9984cSderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261ee9984cSderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271ee9984cSderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281ee9984cSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291ee9984cSderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301ee9984cSderaadt  * SUCH DAMAGE.
311ee9984cSderaadt  *
321ee9984cSderaadt  *	@(#)printf.c	8.1 (Berkeley) 6/11/93
331ee9984cSderaadt  */
341ee9984cSderaadt 
351ee9984cSderaadt #include <sys/types.h>
367034f964Sespie #include <sys/stdarg.h>
371ee9984cSderaadt 
381ee9984cSderaadt #include "stand.h"
391ee9984cSderaadt 
401ee9984cSderaadt extern void kprintn(void (*)(int), u_long, int);
411ee9984cSderaadt extern void kdoprnt(void (*)(int), const char *, va_list);
421ee9984cSderaadt 
431ee9984cSderaadt static void sputchar(int);
441ee9984cSderaadt 
451ee9984cSderaadt static char *sbuf, *sbuf_end;
461ee9984cSderaadt 
471ee9984cSderaadt void
sputchar(int c)48599546b3Sderaadt sputchar(int c)
491ee9984cSderaadt {
501ee9984cSderaadt 	if (sbuf < sbuf_end)
511ee9984cSderaadt 		*sbuf = c;
521ee9984cSderaadt 	sbuf++;
531ee9984cSderaadt }
541ee9984cSderaadt 
551ee9984cSderaadt int
snprintf(char * buf,size_t len,const char * fmt,...)561ee9984cSderaadt snprintf(char *buf, size_t len, const char *fmt, ...)
571ee9984cSderaadt {
581ee9984cSderaadt 	va_list ap;
591ee9984cSderaadt 
601ee9984cSderaadt 	sbuf = buf;
611ee9984cSderaadt 	sbuf_end = sbuf + len;
621ee9984cSderaadt 	va_start(ap, fmt);
631ee9984cSderaadt 	kdoprnt(sputchar, fmt, ap);
641ee9984cSderaadt 	va_end(ap);
651ee9984cSderaadt 
661ee9984cSderaadt 	if (sbuf < sbuf_end)
671ee9984cSderaadt 		*sbuf = '\0';
681ee9984cSderaadt 	else if (len > 0)
691ee9984cSderaadt 		*(sbuf_end - 1) = '\0';
701ee9984cSderaadt 
711ee9984cSderaadt 	return sbuf - buf;
721ee9984cSderaadt }
73