stringf.c revision 261194
11592Srgrimes/*
21592Srgrimes * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
31592Srgrimes *	All rights reserved.
41592Srgrimes *
51592Srgrimes * By using this file, you agree to the terms and conditions set
61592Srgrimes * forth in the LICENSE file which can be found at the top level of
71592Srgrimes * the sendmail distribution.
81592Srgrimes */
91592Srgrimes
101592Srgrimes#include <sm/gen.h>
111592SrgrimesSM_RCSID("@(#)$Id: stringf.c,v 1.16 2013/11/22 20:51:43 ca Exp $")
121592Srgrimes#include <errno.h>
131592Srgrimes#include <stdio.h>
141592Srgrimes#include <sm/exc.h>
151592Srgrimes#include <sm/heap.h>
161592Srgrimes#include <sm/string.h>
171592Srgrimes#include <sm/varargs.h>
181592Srgrimes
191592Srgrimes/*
201592Srgrimes**  SM_STRINGF_X -- printf() to dynamically allocated string.
21262435Sbrueffer**
221592Srgrimes**	Takes the same arguments as printf.
231592Srgrimes**	It returns a pointer to a dynamically allocated string
241592Srgrimes**	containing the text that printf would print to standard output.
251592Srgrimes**	It raises an exception on error.
261592Srgrimes**	The name comes from a PWB Unix function called stringf.
271592Srgrimes**
281592Srgrimes**	Parameters:
291592Srgrimes**		fmt -- format string.
301592Srgrimes**		... -- arguments for format.
311592Srgrimes**
321592Srgrimes**	Returns:
331592Srgrimes**		Pointer to a dynamically allocated string.
341592Srgrimes**
351592Srgrimes**	Exceptions:
361592Srgrimes**		F:sm_heap -- out of memory (via sm_vstringf_x()).
3727074Ssteve*/
381592Srgrimes
3927074Sstevechar *
401592Srgrimes#if SM_VA_STD
41262434Sbrueffersm_stringf_x(const char *fmt, ...)
42262434Sbrueffer#else /* SM_VA_STD */
431592Srgrimessm_stringf_x(fmt, va_alist)
441592Srgrimes	const char *fmt;
451592Srgrimes	va_dcl
461592Srgrimes#endif /* SM_VA_STD */
471592Srgrimes{
481592Srgrimes	SM_VA_LOCAL_DECL
491592Srgrimes	char *s;
501592Srgrimes
511592Srgrimes	SM_VA_START(ap, fmt);
521592Srgrimes	s = sm_vstringf_x(fmt, ap);
531592Srgrimes	SM_VA_END(ap);
541592Srgrimes	return s;
551592Srgrimes}
561592Srgrimes
571592Srgrimes/*
581592Srgrimes**  SM_VSTRINGF_X -- printf() to dynamically allocated string.
591592Srgrimes**
601592Srgrimes**	Parameters:
611592Srgrimes**		fmt -- format string.
621592Srgrimes**		ap -- arguments for format.
631592Srgrimes**
641592Srgrimes**	Returns:
651592Srgrimes**		Pointer to a dynamically allocated string.
661592Srgrimes**
671592Srgrimes**	Exceptions:
681592Srgrimes**		F:sm_heap -- out of memory
691592Srgrimes*/
701592Srgrimes
711592Srgrimeschar *
721592Srgrimessm_vstringf_x(fmt, ap)
731592Srgrimes	const char *fmt;
741592Srgrimes	SM_VA_LOCAL_DECL
751592Srgrimes{
7627074Ssteve	char *s;
7727074Ssteve
7827074Ssteve	sm_vasprintf(&s, fmt, ap);
791592Srgrimes	if (s == NULL)
801592Srgrimes	{
811592Srgrimes		if (errno == ENOMEM)
821592Srgrimes			sm_exc_raise_x(&SmHeapOutOfMemory);
8327074Ssteve		sm_exc_raisenew_x(&SmEtypeOs, errno, "sm_vasprintf", NULL);
841592Srgrimes	}
851592Srgrimes	return s;
861592Srgrimes}
8727074Ssteve