1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*0Sstevel@tonic-gate * All rights reserved.
4*0Sstevel@tonic-gate *
5*0Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set
6*0Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of
7*0Sstevel@tonic-gate * the sendmail distribution.
8*0Sstevel@tonic-gate */
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate #include <sm/gen.h>
13*0Sstevel@tonic-gate SM_RCSID("@(#)$Id: stringf.c,v 1.13 2001/03/03 03:40:43 ca Exp $")
14*0Sstevel@tonic-gate #include <errno.h>
15*0Sstevel@tonic-gate #include <stdio.h>
16*0Sstevel@tonic-gate #include <sm/exc.h>
17*0Sstevel@tonic-gate #include <sm/heap.h>
18*0Sstevel@tonic-gate #include <sm/string.h>
19*0Sstevel@tonic-gate #include <sm/varargs.h>
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate /*
22*0Sstevel@tonic-gate ** SM_STRINGF_X -- printf() to dynamically allocated string.
23*0Sstevel@tonic-gate **
24*0Sstevel@tonic-gate ** Takes the same arguments as printf.
25*0Sstevel@tonic-gate ** It returns a pointer to a dynamically allocated string
26*0Sstevel@tonic-gate ** containing the text that printf would print to standard output.
27*0Sstevel@tonic-gate ** It raises an exception on error.
28*0Sstevel@tonic-gate ** The name comes from a PWB Unix function called stringf.
29*0Sstevel@tonic-gate **
30*0Sstevel@tonic-gate ** Parameters:
31*0Sstevel@tonic-gate ** fmt -- format string.
32*0Sstevel@tonic-gate ** ... -- arguments for format.
33*0Sstevel@tonic-gate **
34*0Sstevel@tonic-gate ** Returns:
35*0Sstevel@tonic-gate ** Pointer to a dynamically allocated string.
36*0Sstevel@tonic-gate **
37*0Sstevel@tonic-gate ** Exceptions:
38*0Sstevel@tonic-gate ** F:sm_heap -- out of memory (via sm_vstringf_x()).
39*0Sstevel@tonic-gate */
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate char *
42*0Sstevel@tonic-gate #if SM_VA_STD
sm_stringf_x(const char * fmt,...)43*0Sstevel@tonic-gate sm_stringf_x(const char *fmt, ...)
44*0Sstevel@tonic-gate #else /* SM_VA_STD */
45*0Sstevel@tonic-gate sm_stringf_x(fmt, va_alist)
46*0Sstevel@tonic-gate const char *fmt;
47*0Sstevel@tonic-gate va_dcl
48*0Sstevel@tonic-gate #endif /* SM_VA_STD */
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate SM_VA_LOCAL_DECL
51*0Sstevel@tonic-gate char *s;
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate SM_VA_START(ap, fmt);
54*0Sstevel@tonic-gate s = sm_vstringf_x(fmt, ap);
55*0Sstevel@tonic-gate SM_VA_END(ap);
56*0Sstevel@tonic-gate return s;
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate ** SM_VSTRINGF_X -- printf() to dynamically allocated string.
61*0Sstevel@tonic-gate **
62*0Sstevel@tonic-gate ** Parameters:
63*0Sstevel@tonic-gate ** fmt -- format string.
64*0Sstevel@tonic-gate ** ap -- arguments for format.
65*0Sstevel@tonic-gate **
66*0Sstevel@tonic-gate ** Returns:
67*0Sstevel@tonic-gate ** Pointer to a dynamically allocated string.
68*0Sstevel@tonic-gate **
69*0Sstevel@tonic-gate ** Exceptions:
70*0Sstevel@tonic-gate ** F:sm_heap -- out of memory
71*0Sstevel@tonic-gate */
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate char *
sm_vstringf_x(fmt,ap)74*0Sstevel@tonic-gate sm_vstringf_x(fmt, ap)
75*0Sstevel@tonic-gate const char *fmt;
76*0Sstevel@tonic-gate SM_VA_LOCAL_DECL
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate char *s;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate sm_vasprintf(&s, fmt, ap);
81*0Sstevel@tonic-gate if (s == NULL)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate if (errno == ENOMEM)
84*0Sstevel@tonic-gate sm_exc_raise_x(&SmHeapOutOfMemory);
85*0Sstevel@tonic-gate sm_exc_raisenew_x(&SmEtypeOs, errno, "sm_vasprintf", NULL);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate return s;
88*0Sstevel@tonic-gate }
89