xref: /titanic_50/usr/src/lib/libast/common/sfio/sfprintf.c (revision 84ba300aaa958c8e8427c2ec66a932d86bee71c4)
1 /***********************************************************************
2 *                                                                      *
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                     *
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 	{	if(s && n > 0)
74 		{	if((rv+1) >= n)
75 				n--;
76 			else
77 				n = rv;
78 			memcpy(s, f->data, n);
79 			s[n] = 0;
80 		}
81 		_Sfi = rv;
82 	}
83 
84 	sfclose(f);
85 
86 	return rv;
87 }
88 
89 #if __STD_C
90 ssize_t sfsprintf(char* s, size_t n, const char* form, ...)
91 #else
92 ssize_t sfsprintf(va_alist)
93 va_dcl
94 #endif
95 {
96 	va_list	args;
97 	ssize_t	rv;
98 
99 #if __STD_C
100 	va_start(args,form);
101 #else
102 	reg char*	s;
103 	reg size_t	n;
104 	reg char*	form;
105 	va_start(args);
106 	s = va_arg(args,char*);
107 	n = va_arg(args,size_t);
108 	form = va_arg(args,char*);
109 #endif
110 
111 	rv = sfvsprintf(s,n,form,args);
112 	va_end(args);
113 
114 	return rv;
115 }
116