190792Sgshapiro/*
2261194Sgshapiro * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *	All rights reserved.
490792Sgshapiro *
590792Sgshapiro * By using this file, you agree to the terms and conditions set
690792Sgshapiro * forth in the LICENSE file which can be found at the top level of
790792Sgshapiro * the sendmail distribution.
890792Sgshapiro */
990792Sgshapiro
1090792Sgshapiro#include <sm/gen.h>
11266527SgshapiroSM_RCSID("@(#)$Id: stringf.c,v 1.16 2013-11-22 20:51:43 ca Exp $")
1290792Sgshapiro#include <errno.h>
1390792Sgshapiro#include <stdio.h>
1490792Sgshapiro#include <sm/exc.h>
1590792Sgshapiro#include <sm/heap.h>
1690792Sgshapiro#include <sm/string.h>
1790792Sgshapiro#include <sm/varargs.h>
1890792Sgshapiro
1990792Sgshapiro/*
2090792Sgshapiro**  SM_STRINGF_X -- printf() to dynamically allocated string.
2190792Sgshapiro**
2290792Sgshapiro**	Takes the same arguments as printf.
2390792Sgshapiro**	It returns a pointer to a dynamically allocated string
2490792Sgshapiro**	containing the text that printf would print to standard output.
2590792Sgshapiro**	It raises an exception on error.
2690792Sgshapiro**	The name comes from a PWB Unix function called stringf.
2790792Sgshapiro**
2890792Sgshapiro**	Parameters:
2990792Sgshapiro**		fmt -- format string.
3090792Sgshapiro**		... -- arguments for format.
3190792Sgshapiro**
3290792Sgshapiro**	Returns:
3390792Sgshapiro**		Pointer to a dynamically allocated string.
3490792Sgshapiro**
3590792Sgshapiro**	Exceptions:
3690792Sgshapiro**		F:sm_heap -- out of memory (via sm_vstringf_x()).
3790792Sgshapiro*/
3890792Sgshapiro
3990792Sgshapirochar *
4090792Sgshapiro#if SM_VA_STD
4190792Sgshapirosm_stringf_x(const char *fmt, ...)
4290792Sgshapiro#else /* SM_VA_STD */
4390792Sgshapirosm_stringf_x(fmt, va_alist)
4490792Sgshapiro	const char *fmt;
4590792Sgshapiro	va_dcl
4690792Sgshapiro#endif /* SM_VA_STD */
4790792Sgshapiro{
4890792Sgshapiro	SM_VA_LOCAL_DECL
4990792Sgshapiro	char *s;
5090792Sgshapiro
5190792Sgshapiro	SM_VA_START(ap, fmt);
5290792Sgshapiro	s = sm_vstringf_x(fmt, ap);
5390792Sgshapiro	SM_VA_END(ap);
5490792Sgshapiro	return s;
5590792Sgshapiro}
5690792Sgshapiro
5790792Sgshapiro/*
5890792Sgshapiro**  SM_VSTRINGF_X -- printf() to dynamically allocated string.
5990792Sgshapiro**
6090792Sgshapiro**	Parameters:
6190792Sgshapiro**		fmt -- format string.
6290792Sgshapiro**		ap -- arguments for format.
6390792Sgshapiro**
6490792Sgshapiro**	Returns:
6590792Sgshapiro**		Pointer to a dynamically allocated string.
6690792Sgshapiro**
6790792Sgshapiro**	Exceptions:
6890792Sgshapiro**		F:sm_heap -- out of memory
6990792Sgshapiro*/
7090792Sgshapiro
7190792Sgshapirochar *
7290792Sgshapirosm_vstringf_x(fmt, ap)
7390792Sgshapiro	const char *fmt;
7490792Sgshapiro	SM_VA_LOCAL_DECL
7590792Sgshapiro{
7690792Sgshapiro	char *s;
7790792Sgshapiro
7890792Sgshapiro	sm_vasprintf(&s, fmt, ap);
7990792Sgshapiro	if (s == NULL)
8090792Sgshapiro	{
8190792Sgshapiro		if (errno == ENOMEM)
8290792Sgshapiro			sm_exc_raise_x(&SmHeapOutOfMemory);
8390792Sgshapiro		sm_exc_raisenew_x(&SmEtypeOs, errno, "sm_vasprintf", NULL);
8490792Sgshapiro	}
8590792Sgshapiro	return s;
8690792Sgshapiro}
87