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