xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/sfio/sfprintf.c (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
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 /*	Print data with a given format
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
sfprintf(Sfio_t * f,const char * form,...)30 int sfprintf(Sfio_t* f, const char* form, ...)
31 #else
32 int sfprintf(va_alist)
33 va_dcl
34 #endif
35 {
36 	va_list	args;
37 	reg int	rv;
38 
39 #if __STD_C
40 	va_start(args,form);
41 #else
42 	reg Sfio_t*	f;
43 	reg char*	form;
44 	va_start(args);
45 	f = va_arg(args,Sfio_t*);
46 	form = va_arg(args,char*);
47 #endif
48 	rv = sfvprintf(f,form,args);
49 
50 	va_end(args);
51 	return rv;
52 }
53 
54 #if __STD_C
sfvsprintf(char * s,size_t n,const char * form,va_list args)55 ssize_t sfvsprintf(char* s, size_t n, const char* form, va_list args)
56 #else
57 ssize_t sfvsprintf(s, n, form, args)
58 char*	s;
59 size_t	n;
60 char*	form;
61 va_list	args;
62 #endif
63 {
64 	Sfio_t		*f;
65 	ssize_t		rv;
66 	Sfnotify_f	notify = _Sfnotify;
67 
68 	/* make a temp stream */
69 	_Sfnotify = 0;
70 	f = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND, -1,SF_WRITE|SF_STRING);
71 	_Sfnotify = notify;
72 	if(!f)
73 		return -1;
74 
75 	if((rv = sfvprintf(f,form,args)) < 0 )
76 		return -1;
77 	if(s && n > 0)
78 	{	if((rv+1) >= n)
79 			n--;
80 		else
81 			n = rv;
82 		memcpy(s, f->data, n);
83 		s[n] = 0;
84 	}
85 
86 	sfclose(f);
87 
88 	_Sfi = rv;
89 
90 	return rv;
91 }
92 
93 #if __STD_C
sfsprintf(char * s,size_t n,const char * form,...)94 ssize_t sfsprintf(char* s, size_t n, const char* form, ...)
95 #else
96 ssize_t sfsprintf(va_alist)
97 va_dcl
98 #endif
99 {
100 	va_list	args;
101 	ssize_t	rv;
102 
103 #if __STD_C
104 	va_start(args,form);
105 #else
106 	reg char*	s;
107 	reg size_t	n;
108 	reg char*	form;
109 	va_start(args);
110 	s = va_arg(args,char*);
111 	n = va_arg(args,size_t);
112 	form = va_arg(args,char*);
113 #endif
114 
115 	rv = sfvsprintf(s,n,form,args);
116 	va_end(args);
117 
118 	return rv;
119 }
120