xref: /titanic_41/usr/src/cmd/sendmail/libsm/snprintf.c (revision 058561cbaa119a6f2659bc27ef343e1b47266bb2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993
57c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
87c478bd9Sstevel@tonic-gate  * Chris Torek.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
117c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
127c478bd9Sstevel@tonic-gate  * the sendmail distribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <sm/gen.h>
18*058561cbSjbeck SM_RCSID("@(#)$Id: snprintf.c,v 1.24 2006/10/12 21:50:10 ca Exp $")
197c478bd9Sstevel@tonic-gate #include <limits.h>
207c478bd9Sstevel@tonic-gate #include <sm/varargs.h>
217c478bd9Sstevel@tonic-gate #include <sm/io.h>
22*058561cbSjbeck #include <sm/string.h>
237c478bd9Sstevel@tonic-gate #include "local.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate **  SM_SNPRINTF -- format a string to a memory location of restricted size
277c478bd9Sstevel@tonic-gate **
287c478bd9Sstevel@tonic-gate **	Parameters:
297c478bd9Sstevel@tonic-gate **		str -- memory location to place formatted string
307c478bd9Sstevel@tonic-gate **		n -- size of buffer pointed to by str, capped to
317c478bd9Sstevel@tonic-gate **			a maximum of INT_MAX
327c478bd9Sstevel@tonic-gate **		fmt -- the formatting directives
337c478bd9Sstevel@tonic-gate **		... -- the data to satisfy the formatting
347c478bd9Sstevel@tonic-gate **
357c478bd9Sstevel@tonic-gate **	Returns:
367c478bd9Sstevel@tonic-gate **		Failure: -1
377c478bd9Sstevel@tonic-gate **		Success: number of bytes that would have been written
387c478bd9Sstevel@tonic-gate **			to str, not including the trailing '\0',
397c478bd9Sstevel@tonic-gate **			up to a maximum of INT_MAX, as if there was
407c478bd9Sstevel@tonic-gate **			no buffer size limitation.  If the result >= n
417c478bd9Sstevel@tonic-gate **			then the output was truncated.
427c478bd9Sstevel@tonic-gate **
437c478bd9Sstevel@tonic-gate **	Side Effects:
447c478bd9Sstevel@tonic-gate **		If n > 0, then between 0 and n-1 bytes of formatted output
457c478bd9Sstevel@tonic-gate **		are written into 'str', followed by a '\0'.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate int
497c478bd9Sstevel@tonic-gate #if SM_VA_STD
sm_snprintf(char * str,size_t n,char const * fmt,...)507c478bd9Sstevel@tonic-gate sm_snprintf(char *str, size_t n, char const *fmt, ...)
517c478bd9Sstevel@tonic-gate #else /* SM_VA_STD */
527c478bd9Sstevel@tonic-gate sm_snprintf(str, n, fmt, va_alist)
537c478bd9Sstevel@tonic-gate 	char *str;
547c478bd9Sstevel@tonic-gate 	size_t n;
557c478bd9Sstevel@tonic-gate 	char *fmt;
567c478bd9Sstevel@tonic-gate 	va_dcl
577c478bd9Sstevel@tonic-gate #endif /* SM_VA_STD */
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	int ret;
607c478bd9Sstevel@tonic-gate 	SM_VA_LOCAL_DECL
617c478bd9Sstevel@tonic-gate 	SM_FILE_T fake;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/* While snprintf(3) specifies size_t stdio uses an int internally */
647c478bd9Sstevel@tonic-gate 	if (n > INT_MAX)
657c478bd9Sstevel@tonic-gate 		n = INT_MAX;
667c478bd9Sstevel@tonic-gate 	SM_VA_START(ap, fmt);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* XXX put this into a static? */
697c478bd9Sstevel@tonic-gate 	fake.sm_magic = SmFileMagic;
707c478bd9Sstevel@tonic-gate 	fake.f_file = -1;
717c478bd9Sstevel@tonic-gate 	fake.f_flags = SMWR | SMSTR;
727c478bd9Sstevel@tonic-gate 	fake.f_cookie = &fake;
737c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_base = fake.f_p = (unsigned char *)str;
747c478bd9Sstevel@tonic-gate 	fake.f_bf.smb_size = fake.f_w = n ? n - 1 : 0;
757c478bd9Sstevel@tonic-gate 	fake.f_timeout = SM_TIME_FOREVER;
767c478bd9Sstevel@tonic-gate 	fake.f_timeoutstate = SM_TIME_BLOCK;
777c478bd9Sstevel@tonic-gate 	fake.f_close = NULL;
787c478bd9Sstevel@tonic-gate 	fake.f_open = NULL;
797c478bd9Sstevel@tonic-gate 	fake.f_read = NULL;
807c478bd9Sstevel@tonic-gate 	fake.f_write = NULL;
817c478bd9Sstevel@tonic-gate 	fake.f_seek = NULL;
827c478bd9Sstevel@tonic-gate 	fake.f_setinfo = fake.f_getinfo = NULL;
837c478bd9Sstevel@tonic-gate 	fake.f_type = "sm_snprintf:fake";
847c478bd9Sstevel@tonic-gate 	ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
857c478bd9Sstevel@tonic-gate 	if (n > 0)
867c478bd9Sstevel@tonic-gate 		*fake.f_p = '\0';
877c478bd9Sstevel@tonic-gate 	SM_VA_END(ap);
887c478bd9Sstevel@tonic-gate 	return ret;
897c478bd9Sstevel@tonic-gate }
90