1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Print data with a given format
25da2e3ebdSchin **
26da2e3ebdSchin ** Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin
29da2e3ebdSchin #if __STD_C
sfprintf(Sfio_t * f,const char * form,...)30da2e3ebdSchin int sfprintf(Sfio_t* f, const char* form, ...)
31da2e3ebdSchin #else
32da2e3ebdSchin int sfprintf(va_alist)
33da2e3ebdSchin va_dcl
34da2e3ebdSchin #endif
35da2e3ebdSchin {
36da2e3ebdSchin va_list args;
37da2e3ebdSchin reg int rv;
38da2e3ebdSchin
39da2e3ebdSchin #if __STD_C
40da2e3ebdSchin va_start(args,form);
41da2e3ebdSchin #else
42da2e3ebdSchin reg Sfio_t* f;
43da2e3ebdSchin reg char* form;
44da2e3ebdSchin va_start(args);
45da2e3ebdSchin f = va_arg(args,Sfio_t*);
46da2e3ebdSchin form = va_arg(args,char*);
47da2e3ebdSchin #endif
48da2e3ebdSchin rv = sfvprintf(f,form,args);
49da2e3ebdSchin
50da2e3ebdSchin va_end(args);
51da2e3ebdSchin return rv;
52da2e3ebdSchin }
53da2e3ebdSchin
54da2e3ebdSchin #if __STD_C
sfvsprintf(char * s,size_t n,const char * form,va_list args)55da2e3ebdSchin ssize_t sfvsprintf(char* s, size_t n, const char* form, va_list args)
56da2e3ebdSchin #else
57da2e3ebdSchin ssize_t sfvsprintf(s, n, form, args)
58da2e3ebdSchin char* s;
59da2e3ebdSchin size_t n;
60da2e3ebdSchin char* form;
61da2e3ebdSchin va_list args;
62da2e3ebdSchin #endif
63da2e3ebdSchin {
64da2e3ebdSchin Sfio_t *f;
65da2e3ebdSchin ssize_t rv;
66da2e3ebdSchin
67da2e3ebdSchin /* make a temp stream */
68da2e3ebdSchin if(!(f = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
69da2e3ebdSchin -1,SF_WRITE|SF_STRING)) )
70da2e3ebdSchin return -1;
71da2e3ebdSchin
727c2fbfb3SApril Chin if((rv = sfvprintf(f,form,args)) >= 0 )
737c2fbfb3SApril Chin { if(s && n > 0)
74da2e3ebdSchin { if((rv+1) >= n)
75da2e3ebdSchin n--;
76da2e3ebdSchin else
77da2e3ebdSchin n = rv;
78da2e3ebdSchin memcpy(s, f->data, n);
79da2e3ebdSchin s[n] = 0;
80da2e3ebdSchin }
817c2fbfb3SApril Chin _Sfi = rv;
827c2fbfb3SApril Chin }
83da2e3ebdSchin
84da2e3ebdSchin sfclose(f);
85da2e3ebdSchin
86da2e3ebdSchin return rv;
87da2e3ebdSchin }
88da2e3ebdSchin
89da2e3ebdSchin #if __STD_C
sfsprintf(char * s,size_t n,const char * form,...)90da2e3ebdSchin ssize_t sfsprintf(char* s, size_t n, const char* form, ...)
91da2e3ebdSchin #else
92da2e3ebdSchin ssize_t sfsprintf(va_alist)
93da2e3ebdSchin va_dcl
94da2e3ebdSchin #endif
95da2e3ebdSchin {
96da2e3ebdSchin va_list args;
97da2e3ebdSchin ssize_t rv;
98da2e3ebdSchin
99da2e3ebdSchin #if __STD_C
100da2e3ebdSchin va_start(args,form);
101da2e3ebdSchin #else
102da2e3ebdSchin reg char* s;
103da2e3ebdSchin reg size_t n;
104da2e3ebdSchin reg char* form;
105da2e3ebdSchin va_start(args);
106da2e3ebdSchin s = va_arg(args,char*);
107da2e3ebdSchin n = va_arg(args,size_t);
108da2e3ebdSchin form = va_arg(args,char*);
109da2e3ebdSchin #endif
110da2e3ebdSchin
111da2e3ebdSchin rv = sfvsprintf(s,n,form,args);
112da2e3ebdSchin va_end(args);
113da2e3ebdSchin
114da2e3ebdSchin return rv;
115da2e3ebdSchin }
116