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