1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 /* Construct a string with the given format and data.
25 ** These functions allocate space as necessary to store the string.
26 ** This avoids overflow problems typical with sprintf() in stdio.
28 ** Written by Kiem-Phong Vo.
32 char* sfvprints(const char* form
, va_list args
)
34 char* sfvprints(form
, args
)
42 /* make a fake stream */
44 !(f
= sfnew(NIL(Sfio_t
*),NIL(char*),(size_t)SF_UNBOUND
,
45 -1,SF_WRITE
|SF_STRING
)) )
48 sfseek(f
,(Sfoff_t
)0,SEEK_SET
);
49 rv
= sfvprintf(f
,form
,args
);
51 if(rv
< 0 || sfputc(f
,'\0') < 0)
54 _Sfi
= (f
->next
- f
->data
) - 1;
55 return (char*)f
->data
;
59 char* sfprints(const char* form
, ...)
61 char* sfprints(va_alist
)
73 form
= va_arg(args
,char*);
75 s
= sfvprints(form
, args
);
82 ssize_t
sfvaprints(char** sp
, const char* form
, va_list args
)
84 ssize_t
sfvaprints(sp
, form
, args
)
93 if(!sp
|| !(s
= sfvprints(form
,args
)) )
96 { if(!(*sp
= (char*)malloc(n
= strlen(s
)+1)) )
104 ssize_t
sfaprints(char** sp
, const char* form
, ...)
106 ssize_t
sfaprints(va_alist
)
118 sp
= va_arg(args
, char**);
119 form
= va_arg(args
, char*);
121 n
= sfvaprints(sp
, form
, args
);