1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1985-2012 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                 Eclipse Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*          http://www.eclipse.org/org/documents/epl-v10.html           *
11*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                   Phong Vo <kpv@research.att.com>                    *
20*                                                                      *
21***********************************************************************/
22#include	"sfhdr.h"
23
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.
27**
28**	Written by Kiem-Phong Vo.
29*/
30
31#if __STD_C
32char* sfvprints(const char* form, va_list args)
33#else
34char* sfvprints(form, args)
35char*	form;
36va_list	args;
37#endif
38{
39	reg int		rv;
40	Sfnotify_f	notify = _Sfnotify;
41	static Sfio_t*	f;
42
43	if(!f) /* make a string stream to write into */
44	{	_Sfnotify = 0;
45		f = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND, -1,SF_WRITE|SF_STRING);
46		_Sfnotify = notify;
47		if(!f)
48			return NIL(char*);
49	}
50
51	sfseek(f,(Sfoff_t)0,SEEK_SET);
52	rv = sfvprintf(f,form,args);
53
54	if(rv < 0 || sfputc(f,'\0') < 0)
55		return NIL(char*);
56
57	_Sfi = (f->next - f->data) - 1;
58	return (char*)f->data;
59}
60
61#if __STD_C
62char* sfprints(const char* form, ...)
63#else
64char* sfprints(va_alist)
65va_dcl
66#endif
67{
68	char*	s;
69	va_list	args;
70
71#if __STD_C
72	va_start(args,form);
73#else
74	char	*form;
75	va_start(args);
76	form = va_arg(args,char*);
77#endif
78	s = sfvprints(form, args);
79	va_end(args);
80
81	return s;
82}
83
84#if __STD_C
85ssize_t sfvaprints(char** sp, const char* form, va_list args)
86#else
87ssize_t sfvaprints(sp, form, args)
88char**	sp;
89char*	form;
90va_list	args;
91#endif
92{
93	char	*s;
94	ssize_t	n;
95
96	if(!sp || !(s = sfvprints(form,args)) )
97		return -1;
98	else
99	{	if(!(*sp = (char*)malloc(n = strlen(s)+1)) )
100			return -1;
101		memcpy(*sp, s, n);
102		return n-1;
103	}
104}
105
106#if __STD_C
107ssize_t sfaprints(char** sp, const char* form, ...)
108#else
109ssize_t sfaprints(va_alist)
110va_dcl
111#endif
112{
113	ssize_t	n;
114	va_list	args;
115
116#if __STD_C
117	va_start(args,form);
118#else
119	char	**sp, *form;
120	va_start(args);
121	sp = va_arg(args, char**);
122	form = va_arg(args, char*);
123#endif
124	n = sfvaprints(sp, form, args);
125	va_end(args);
126
127	return n;
128}
129