xref: /inferno-os/lib9/sprint.c (revision 7ba4ff5becea2074955758024f3418d6ac79860f)
137da2899SCharles.Forsyth /*
237da2899SCharles.Forsyth  * The authors of this software are Rob Pike and Ken Thompson.
337da2899SCharles.Forsyth  *              Copyright (c) 2002 by Lucent Technologies.
437da2899SCharles.Forsyth  * Permission to use, copy, modify, and distribute this software for any
537da2899SCharles.Forsyth  * purpose without fee is hereby granted, provided that this entire notice
637da2899SCharles.Forsyth  * is included in all copies of any software which is or includes a copy
737da2899SCharles.Forsyth  * or modification of this software and in all copies of the supporting
837da2899SCharles.Forsyth  * documentation for such software.
937da2899SCharles.Forsyth  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
1037da2899SCharles.Forsyth  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
1137da2899SCharles.Forsyth  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
1237da2899SCharles.Forsyth  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
1337da2899SCharles.Forsyth  */
1437da2899SCharles.Forsyth #include "lib9.h"
1537da2899SCharles.Forsyth 
1637da2899SCharles.Forsyth int
sprint(char * buf,char * fmt,...)1737da2899SCharles.Forsyth sprint(char *buf, char *fmt, ...)
1837da2899SCharles.Forsyth {
1937da2899SCharles.Forsyth 	int n;
2037da2899SCharles.Forsyth 	uint len;
2137da2899SCharles.Forsyth 	va_list args;
2237da2899SCharles.Forsyth 
2337da2899SCharles.Forsyth 	len = 1<<30;  /* big number, but sprint is deprecated anyway */
2437da2899SCharles.Forsyth 	/*
2537da2899SCharles.Forsyth 	 * the stack might be near the top of memory, so
2637da2899SCharles.Forsyth 	 * we must be sure not to overflow a 32-bit pointer.
2737da2899SCharles.Forsyth 	 */
28*7ba4ff5bSCharles.Forsyth 	if((uintptr)buf+len < (uintptr)buf)
2937da2899SCharles.Forsyth 		len = -(uint)buf-1;
3037da2899SCharles.Forsyth 
3137da2899SCharles.Forsyth 	va_start(args, fmt);
3237da2899SCharles.Forsyth 	n = vsnprint(buf, len, fmt, args);
3337da2899SCharles.Forsyth 	va_end(args);
3437da2899SCharles.Forsyth 	return n;
3537da2899SCharles.Forsyth }
36