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 /* Construct a string with the given format and data.
25da2e3ebdSchin ** These functions allocate space as necessary to store the string.
26da2e3ebdSchin ** This avoids overflow problems typical with sprintf() in stdio.
27da2e3ebdSchin **
28da2e3ebdSchin ** Written by Kiem-Phong Vo.
29da2e3ebdSchin */
30da2e3ebdSchin
31da2e3ebdSchin #if __STD_C
sfvprints(const char * form,va_list args)32da2e3ebdSchin char* sfvprints(const char* form, va_list args)
33da2e3ebdSchin #else
34da2e3ebdSchin char* sfvprints(form, args)
35da2e3ebdSchin char* form;
36da2e3ebdSchin va_list args;
37da2e3ebdSchin #endif
38da2e3ebdSchin {
39da2e3ebdSchin reg int rv;
40da2e3ebdSchin static Sfio_t* f;
41da2e3ebdSchin
42da2e3ebdSchin /* make a fake stream */
43da2e3ebdSchin if(!f &&
44da2e3ebdSchin !(f = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
45da2e3ebdSchin -1,SF_WRITE|SF_STRING)) )
46da2e3ebdSchin return NIL(char*);
47da2e3ebdSchin
48da2e3ebdSchin sfseek(f,(Sfoff_t)0,SEEK_SET);
49da2e3ebdSchin rv = sfvprintf(f,form,args);
50da2e3ebdSchin
51da2e3ebdSchin if(rv < 0 || sfputc(f,'\0') < 0)
52da2e3ebdSchin return NIL(char*);
53da2e3ebdSchin
54da2e3ebdSchin _Sfi = (f->next - f->data) - 1;
55da2e3ebdSchin return (char*)f->data;
56da2e3ebdSchin }
57da2e3ebdSchin
58da2e3ebdSchin #if __STD_C
sfprints(const char * form,...)59da2e3ebdSchin char* sfprints(const char* form, ...)
60da2e3ebdSchin #else
61da2e3ebdSchin char* sfprints(va_alist)
62da2e3ebdSchin va_dcl
63da2e3ebdSchin #endif
64da2e3ebdSchin {
65da2e3ebdSchin char* s;
66da2e3ebdSchin va_list args;
67da2e3ebdSchin
68da2e3ebdSchin #if __STD_C
69da2e3ebdSchin va_start(args,form);
70da2e3ebdSchin #else
71da2e3ebdSchin char *form;
72da2e3ebdSchin va_start(args);
73da2e3ebdSchin form = va_arg(args,char*);
74da2e3ebdSchin #endif
75da2e3ebdSchin s = sfvprints(form, args);
76da2e3ebdSchin va_end(args);
77da2e3ebdSchin
78da2e3ebdSchin return s;
79da2e3ebdSchin }
80da2e3ebdSchin
81da2e3ebdSchin #if __STD_C
sfvaprints(char ** sp,const char * form,va_list args)82da2e3ebdSchin ssize_t sfvaprints(char** sp, const char* form, va_list args)
83da2e3ebdSchin #else
84da2e3ebdSchin ssize_t sfvaprints(sp, form, args)
85da2e3ebdSchin char** sp;
86da2e3ebdSchin char* form;
87da2e3ebdSchin va_list args;
88da2e3ebdSchin #endif
89da2e3ebdSchin {
90da2e3ebdSchin char *s;
91da2e3ebdSchin ssize_t n;
92da2e3ebdSchin
93da2e3ebdSchin if(!sp || !(s = sfvprints(form,args)) )
94da2e3ebdSchin return -1;
95da2e3ebdSchin else
96da2e3ebdSchin { if(!(*sp = (char*)malloc(n = strlen(s)+1)) )
97da2e3ebdSchin return -1;
98da2e3ebdSchin memcpy(*sp, s, n);
9934f9b3eeSRoland Mainz return n - 1;
100da2e3ebdSchin }
101da2e3ebdSchin }
102da2e3ebdSchin
103da2e3ebdSchin #if __STD_C
sfaprints(char ** sp,const char * form,...)104da2e3ebdSchin ssize_t sfaprints(char** sp, const char* form, ...)
105da2e3ebdSchin #else
106da2e3ebdSchin ssize_t sfaprints(va_alist)
107da2e3ebdSchin va_dcl
108da2e3ebdSchin #endif
109da2e3ebdSchin {
110da2e3ebdSchin ssize_t n;
111da2e3ebdSchin va_list args;
112da2e3ebdSchin
113da2e3ebdSchin #if __STD_C
114da2e3ebdSchin va_start(args,form);
115da2e3ebdSchin #else
116da2e3ebdSchin char **sp, *form;
117da2e3ebdSchin va_start(args);
118da2e3ebdSchin sp = va_arg(args, char**);
119da2e3ebdSchin form = va_arg(args, char*);
120da2e3ebdSchin #endif
121da2e3ebdSchin n = sfvaprints(sp, form, args);
122da2e3ebdSchin va_end(args);
123da2e3ebdSchin
124da2e3ebdSchin return n;
125da2e3ebdSchin }
126