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