10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate * Copyright (c) 1990, 1993
50Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by
80Sstevel@tonic-gate * Chris Torek.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
110Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
120Sstevel@tonic-gate * the sendmail distribution.
130Sstevel@tonic-gate */
140Sstevel@tonic-gate
150Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include <sm/gen.h>
18*3544Sjbeck SM_RCSID("@(#)$Id: snprintf.c,v 1.24 2006/10/12 21:50:10 ca Exp $")
190Sstevel@tonic-gate #include <limits.h>
200Sstevel@tonic-gate #include <sm/varargs.h>
210Sstevel@tonic-gate #include <sm/io.h>
22*3544Sjbeck #include <sm/string.h>
230Sstevel@tonic-gate #include "local.h"
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate ** SM_SNPRINTF -- format a string to a memory location of restricted size
270Sstevel@tonic-gate **
280Sstevel@tonic-gate ** Parameters:
290Sstevel@tonic-gate ** str -- memory location to place formatted string
300Sstevel@tonic-gate ** n -- size of buffer pointed to by str, capped to
310Sstevel@tonic-gate ** a maximum of INT_MAX
320Sstevel@tonic-gate ** fmt -- the formatting directives
330Sstevel@tonic-gate ** ... -- the data to satisfy the formatting
340Sstevel@tonic-gate **
350Sstevel@tonic-gate ** Returns:
360Sstevel@tonic-gate ** Failure: -1
370Sstevel@tonic-gate ** Success: number of bytes that would have been written
380Sstevel@tonic-gate ** to str, not including the trailing '\0',
390Sstevel@tonic-gate ** up to a maximum of INT_MAX, as if there was
400Sstevel@tonic-gate ** no buffer size limitation. If the result >= n
410Sstevel@tonic-gate ** then the output was truncated.
420Sstevel@tonic-gate **
430Sstevel@tonic-gate ** Side Effects:
440Sstevel@tonic-gate ** If n > 0, then between 0 and n-1 bytes of formatted output
450Sstevel@tonic-gate ** are written into 'str', followed by a '\0'.
460Sstevel@tonic-gate */
470Sstevel@tonic-gate
480Sstevel@tonic-gate int
490Sstevel@tonic-gate #if SM_VA_STD
sm_snprintf(char * str,size_t n,char const * fmt,...)500Sstevel@tonic-gate sm_snprintf(char *str, size_t n, char const *fmt, ...)
510Sstevel@tonic-gate #else /* SM_VA_STD */
520Sstevel@tonic-gate sm_snprintf(str, n, fmt, va_alist)
530Sstevel@tonic-gate char *str;
540Sstevel@tonic-gate size_t n;
550Sstevel@tonic-gate char *fmt;
560Sstevel@tonic-gate va_dcl
570Sstevel@tonic-gate #endif /* SM_VA_STD */
580Sstevel@tonic-gate {
590Sstevel@tonic-gate int ret;
600Sstevel@tonic-gate SM_VA_LOCAL_DECL
610Sstevel@tonic-gate SM_FILE_T fake;
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* While snprintf(3) specifies size_t stdio uses an int internally */
640Sstevel@tonic-gate if (n > INT_MAX)
650Sstevel@tonic-gate n = INT_MAX;
660Sstevel@tonic-gate SM_VA_START(ap, fmt);
670Sstevel@tonic-gate
680Sstevel@tonic-gate /* XXX put this into a static? */
690Sstevel@tonic-gate fake.sm_magic = SmFileMagic;
700Sstevel@tonic-gate fake.f_file = -1;
710Sstevel@tonic-gate fake.f_flags = SMWR | SMSTR;
720Sstevel@tonic-gate fake.f_cookie = &fake;
730Sstevel@tonic-gate fake.f_bf.smb_base = fake.f_p = (unsigned char *)str;
740Sstevel@tonic-gate fake.f_bf.smb_size = fake.f_w = n ? n - 1 : 0;
750Sstevel@tonic-gate fake.f_timeout = SM_TIME_FOREVER;
760Sstevel@tonic-gate fake.f_timeoutstate = SM_TIME_BLOCK;
770Sstevel@tonic-gate fake.f_close = NULL;
780Sstevel@tonic-gate fake.f_open = NULL;
790Sstevel@tonic-gate fake.f_read = NULL;
800Sstevel@tonic-gate fake.f_write = NULL;
810Sstevel@tonic-gate fake.f_seek = NULL;
820Sstevel@tonic-gate fake.f_setinfo = fake.f_getinfo = NULL;
830Sstevel@tonic-gate fake.f_type = "sm_snprintf:fake";
840Sstevel@tonic-gate ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
850Sstevel@tonic-gate if (n > 0)
860Sstevel@tonic-gate *fake.f_p = '\0';
870Sstevel@tonic-gate SM_VA_END(ap);
880Sstevel@tonic-gate return ret;
890Sstevel@tonic-gate }
90