xref: /titanic_44/usr/src/lib/libast/common/sfio/sfprintf.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
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
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
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 
67 	/* make a temp stream */
68 	if(!(f = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
69                         -1,SF_WRITE|SF_STRING)) )
70 		return -1;
71 
72 	if((rv = sfvprintf(f,form,args)) < 0 )
73 		return -1;
74 	if(s && n > 0)
75 	{	if((rv+1) >= n)
76 			n--;
77 		else
78 			n = rv;
79 		memcpy(s, f->data, n);
80 		s[n] = 0;
81 	}
82 
83 	sfclose(f);
84 
85 	_Sfi = rv;
86 
87 	return rv;
88 }
89 
90 #if __STD_C
91 ssize_t sfsprintf(char* s, size_t n, const char* form, ...)
92 #else
93 ssize_t sfsprintf(va_alist)
94 va_dcl
95 #endif
96 {
97 	va_list	args;
98 	ssize_t	rv;
99 
100 #if __STD_C
101 	va_start(args,form);
102 #else
103 	reg char*	s;
104 	reg size_t	n;
105 	reg char*	form;
106 	va_start(args);
107 	s = va_arg(args,char*);
108 	n = va_arg(args,size_t);
109 	form = va_arg(args,char*);
110 #endif
111 
112 	rv = sfvsprintf(s,n,form,args);
113 	va_end(args);
114 
115 	return rv;
116 }
117