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 #include <sm/gen.h>
16*058561cbSjbeck SM_RCSID("@(#)$Id: snprintf.c,v 1.24 2006/10/12 21:50:10 ca Exp $")
177c478bd9Sstevel@tonic-gate #include <limits.h>
187c478bd9Sstevel@tonic-gate #include <sm/varargs.h>
197c478bd9Sstevel@tonic-gate #include <sm/io.h>
20*058561cbSjbeck #include <sm/string.h>
217c478bd9Sstevel@tonic-gate #include "local.h"
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate ** SM_SNPRINTF -- format a string to a memory location of restricted size
257c478bd9Sstevel@tonic-gate **
267c478bd9Sstevel@tonic-gate ** Parameters:
277c478bd9Sstevel@tonic-gate ** str -- memory location to place formatted string
287c478bd9Sstevel@tonic-gate ** n -- size of buffer pointed to by str, capped to
297c478bd9Sstevel@tonic-gate ** a maximum of INT_MAX
307c478bd9Sstevel@tonic-gate ** fmt -- the formatting directives
317c478bd9Sstevel@tonic-gate ** ... -- the data to satisfy the formatting
327c478bd9Sstevel@tonic-gate **
337c478bd9Sstevel@tonic-gate ** Returns:
347c478bd9Sstevel@tonic-gate ** Failure: -1
357c478bd9Sstevel@tonic-gate ** Success: number of bytes that would have been written
367c478bd9Sstevel@tonic-gate ** to str, not including the trailing '\0',
377c478bd9Sstevel@tonic-gate ** up to a maximum of INT_MAX, as if there was
387c478bd9Sstevel@tonic-gate ** no buffer size limitation. If the result >= n
397c478bd9Sstevel@tonic-gate ** then the output was truncated.
407c478bd9Sstevel@tonic-gate **
417c478bd9Sstevel@tonic-gate ** Side Effects:
427c478bd9Sstevel@tonic-gate ** If n > 0, then between 0 and n-1 bytes of formatted output
437c478bd9Sstevel@tonic-gate ** are written into 'str', followed by a '\0'.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate int
477c478bd9Sstevel@tonic-gate #if SM_VA_STD
sm_snprintf(char * str,size_t n,char const * fmt,...)487c478bd9Sstevel@tonic-gate sm_snprintf(char *str, size_t n, char const *fmt, ...)
497c478bd9Sstevel@tonic-gate #else /* SM_VA_STD */
507c478bd9Sstevel@tonic-gate sm_snprintf(str, n, fmt, va_alist)
517c478bd9Sstevel@tonic-gate char *str;
527c478bd9Sstevel@tonic-gate size_t n;
537c478bd9Sstevel@tonic-gate char *fmt;
547c478bd9Sstevel@tonic-gate va_dcl
557c478bd9Sstevel@tonic-gate #endif /* SM_VA_STD */
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate int ret;
587c478bd9Sstevel@tonic-gate SM_VA_LOCAL_DECL
597c478bd9Sstevel@tonic-gate SM_FILE_T fake;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /* While snprintf(3) specifies size_t stdio uses an int internally */
627c478bd9Sstevel@tonic-gate if (n > INT_MAX)
637c478bd9Sstevel@tonic-gate n = INT_MAX;
647c478bd9Sstevel@tonic-gate SM_VA_START(ap, fmt);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /* XXX put this into a static? */
677c478bd9Sstevel@tonic-gate fake.sm_magic = SmFileMagic;
687c478bd9Sstevel@tonic-gate fake.f_file = -1;
697c478bd9Sstevel@tonic-gate fake.f_flags = SMWR | SMSTR;
707c478bd9Sstevel@tonic-gate fake.f_cookie = &fake;
717c478bd9Sstevel@tonic-gate fake.f_bf.smb_base = fake.f_p = (unsigned char *)str;
727c478bd9Sstevel@tonic-gate fake.f_bf.smb_size = fake.f_w = n ? n - 1 : 0;
737c478bd9Sstevel@tonic-gate fake.f_timeout = SM_TIME_FOREVER;
747c478bd9Sstevel@tonic-gate fake.f_timeoutstate = SM_TIME_BLOCK;
757c478bd9Sstevel@tonic-gate fake.f_close = NULL;
767c478bd9Sstevel@tonic-gate fake.f_open = NULL;
777c478bd9Sstevel@tonic-gate fake.f_read = NULL;
787c478bd9Sstevel@tonic-gate fake.f_write = NULL;
797c478bd9Sstevel@tonic-gate fake.f_seek = NULL;
807c478bd9Sstevel@tonic-gate fake.f_setinfo = fake.f_getinfo = NULL;
817c478bd9Sstevel@tonic-gate fake.f_type = "sm_snprintf:fake";
827c478bd9Sstevel@tonic-gate ret = sm_io_vfprintf(&fake, SM_TIME_FOREVER, fmt, ap);
837c478bd9Sstevel@tonic-gate if (n > 0)
847c478bd9Sstevel@tonic-gate *fake.f_p = '\0';
857c478bd9Sstevel@tonic-gate SM_VA_END(ap);
867c478bd9Sstevel@tonic-gate return ret;
877c478bd9Sstevel@tonic-gate }
88