text: /*-
   * SPDX-License-Identifier: BSD-3-Clause
   *
   * Copyright (c) 1986, 1988, 1991, 1993
   *	The Regents of the University of California.  All rights reserved.
   * (c) UNIX System Laboratories, Inc.
   * All or some portions of this file are derived from material licensed
   * to the University of California by American Telephone and Telegraph
   * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   * the permission of UNIX System Laboratories, Inc.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in the
   *    documentation and/or other materials provided with the distribution.
   * 3. Neither the name of the University nor the names of its contributors
   *    may be used to endorse or promote products derived from this software
   *    without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   *
   *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
   */
  
  #include <sys/cdefs.h>
  #include <sys/stdarg.h>
  #include <sys/console.h>
  
  struct snprintf_arg {
  	char	*str;
  	size_t	remain;
  };
  
  static void  snprintf_func(int ch, void *arg);
  
  extern struct kernel_console kern_console;
  
  int
  vprintf(const char *fmt, va_list ap)
  {
  
  	return (kvprintf(fmt, kern_console.console_putchar,
  	    kern_console.console_putchar_arg, 10, ap));
  }
  
  int
  printf(const char *fmt, ...)
  {
  	va_list ap;
  	int retval;
  
  	va_start(ap, fmt);
  	retval = vprintf(fmt, ap);
  	va_end(ap);
  
  	return (retval);
  }
  
  /*
   * Scaled down version of sprintf(3).
   */
  int
  sprintf(char *buf, const char *cfmt, ...)
  {
  	int retval;
  	va_list ap;
  
  	va_start(ap, cfmt);
  	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
  	buf[retval] = '\0';
  	va_end(ap);
  	return (retval);
  }
  
  /*
   * Scaled down version of vsprintf(3).
   */
  int
  vsprintf(char *buf, const char *cfmt, va_list ap)
  {
  	int retval;
  
  	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
  	buf[retval] = '\0';
  	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--;
  	}
  }
  