1*0a6a1f1dSLionel Sambuc /* $NetBSD: snprintf.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc #include <stdio.h>
38ebfedea0SLionel Sambuc #include <stdarg.h>
39ebfedea0SLionel Sambuc #include <stdlib.h>
40ebfedea0SLionel Sambuc #include <string.h>
41ebfedea0SLionel Sambuc #include <ctype.h>
42ebfedea0SLionel Sambuc #include <krb5/roken.h>
43ebfedea0SLionel Sambuc #include <assert.h>
44ebfedea0SLionel Sambuc
45ebfedea0SLionel Sambuc enum format_flags {
46ebfedea0SLionel Sambuc minus_flag = 1,
47ebfedea0SLionel Sambuc plus_flag = 2,
48ebfedea0SLionel Sambuc space_flag = 4,
49ebfedea0SLionel Sambuc alternate_flag = 8,
50ebfedea0SLionel Sambuc zero_flag = 16
51ebfedea0SLionel Sambuc };
52ebfedea0SLionel Sambuc
53ebfedea0SLionel Sambuc /*
54ebfedea0SLionel Sambuc * Common state
55ebfedea0SLionel Sambuc */
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc struct snprintf_state {
58ebfedea0SLionel Sambuc unsigned char *str;
59ebfedea0SLionel Sambuc unsigned char *s;
60ebfedea0SLionel Sambuc unsigned char *theend;
61ebfedea0SLionel Sambuc size_t sz;
62ebfedea0SLionel Sambuc size_t max_sz;
63ebfedea0SLionel Sambuc void (*append_char)(struct snprintf_state *, unsigned char);
64ebfedea0SLionel Sambuc /* XXX - methods */
65ebfedea0SLionel Sambuc };
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
68ebfedea0SLionel Sambuc static int
sn_reserve(struct snprintf_state * state,size_t n)69ebfedea0SLionel Sambuc sn_reserve (struct snprintf_state *state, size_t n)
70ebfedea0SLionel Sambuc {
71ebfedea0SLionel Sambuc return state->s + n > state->theend;
72ebfedea0SLionel Sambuc }
73ebfedea0SLionel Sambuc
74ebfedea0SLionel Sambuc static void
sn_append_char(struct snprintf_state * state,unsigned char c)75ebfedea0SLionel Sambuc sn_append_char (struct snprintf_state *state, unsigned char c)
76ebfedea0SLionel Sambuc {
77ebfedea0SLionel Sambuc if (!sn_reserve (state, 1))
78ebfedea0SLionel Sambuc *state->s++ = c;
79ebfedea0SLionel Sambuc }
80ebfedea0SLionel Sambuc #endif
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc static int
as_reserve(struct snprintf_state * state,size_t n)83ebfedea0SLionel Sambuc as_reserve (struct snprintf_state *state, size_t n)
84ebfedea0SLionel Sambuc {
85ebfedea0SLionel Sambuc if (state->s + n > state->theend) {
86ebfedea0SLionel Sambuc int off = state->s - state->str;
87ebfedea0SLionel Sambuc unsigned char *tmp;
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc if (state->max_sz && state->sz >= state->max_sz)
90ebfedea0SLionel Sambuc return 1;
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc state->sz = max(state->sz * 2, state->sz + n);
93ebfedea0SLionel Sambuc if (state->max_sz)
94ebfedea0SLionel Sambuc state->sz = min(state->sz, state->max_sz);
95ebfedea0SLionel Sambuc tmp = realloc (state->str, state->sz);
96ebfedea0SLionel Sambuc if (tmp == NULL)
97ebfedea0SLionel Sambuc return 1;
98ebfedea0SLionel Sambuc state->str = tmp;
99ebfedea0SLionel Sambuc state->s = state->str + off;
100ebfedea0SLionel Sambuc state->theend = state->str + state->sz - 1;
101ebfedea0SLionel Sambuc }
102ebfedea0SLionel Sambuc return 0;
103ebfedea0SLionel Sambuc }
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc static void
as_append_char(struct snprintf_state * state,unsigned char c)106ebfedea0SLionel Sambuc as_append_char (struct snprintf_state *state, unsigned char c)
107ebfedea0SLionel Sambuc {
108ebfedea0SLionel Sambuc if(!as_reserve (state, 1))
109ebfedea0SLionel Sambuc *state->s++ = c;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc /* longest integer types */
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc #ifdef HAVE_LONG_LONG
115ebfedea0SLionel Sambuc typedef unsigned long long u_longest;
116ebfedea0SLionel Sambuc typedef long long longest;
117ebfedea0SLionel Sambuc #else
118ebfedea0SLionel Sambuc typedef unsigned long u_longest;
119ebfedea0SLionel Sambuc typedef long longest;
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc
122ebfedea0SLionel Sambuc
123ebfedea0SLionel Sambuc
124ebfedea0SLionel Sambuc static size_t
pad(struct snprintf_state * state,int width,char c)125ebfedea0SLionel Sambuc pad(struct snprintf_state *state, int width, char c)
126ebfedea0SLionel Sambuc {
127ebfedea0SLionel Sambuc size_t len = 0;
128ebfedea0SLionel Sambuc while(width-- > 0){
129ebfedea0SLionel Sambuc (*state->append_char)(state, c);
130ebfedea0SLionel Sambuc ++len;
131ebfedea0SLionel Sambuc }
132ebfedea0SLionel Sambuc return len;
133ebfedea0SLionel Sambuc }
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc /* return true if we should use alternatve hex form */
136ebfedea0SLionel Sambuc static int
use_alternative(int flags,u_longest num,unsigned base)137ebfedea0SLionel Sambuc use_alternative (int flags, u_longest num, unsigned base)
138ebfedea0SLionel Sambuc {
139ebfedea0SLionel Sambuc return (flags & alternate_flag) && base == 16 && num != 0;
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc static int
append_number(struct snprintf_state * state,u_longest num,unsigned base,const char * rep,int width,int prec,int flags,int minusp)143ebfedea0SLionel Sambuc append_number(struct snprintf_state *state,
144ebfedea0SLionel Sambuc u_longest num, unsigned base, const char *rep,
145ebfedea0SLionel Sambuc int width, int prec, int flags, int minusp)
146ebfedea0SLionel Sambuc {
147ebfedea0SLionel Sambuc int len = 0;
148ebfedea0SLionel Sambuc u_longest n = num;
149ebfedea0SLionel Sambuc char nstr[64]; /* enough for <192 bit octal integers */
150ebfedea0SLionel Sambuc int nstart, nlen;
151ebfedea0SLionel Sambuc char signchar;
152ebfedea0SLionel Sambuc
153ebfedea0SLionel Sambuc /* given precision, ignore zero flag */
154ebfedea0SLionel Sambuc if(prec != -1)
155ebfedea0SLionel Sambuc flags &= ~zero_flag;
156ebfedea0SLionel Sambuc else
157ebfedea0SLionel Sambuc prec = 1;
158ebfedea0SLionel Sambuc
159ebfedea0SLionel Sambuc /* format number as string */
160ebfedea0SLionel Sambuc nstart = sizeof(nstr);
161ebfedea0SLionel Sambuc nlen = 0;
162ebfedea0SLionel Sambuc nstr[--nstart] = '\0';
163ebfedea0SLionel Sambuc do {
164ebfedea0SLionel Sambuc assert(nstart > 0);
165ebfedea0SLionel Sambuc nstr[--nstart] = rep[n % base];
166ebfedea0SLionel Sambuc ++nlen;
167ebfedea0SLionel Sambuc n /= base;
168ebfedea0SLionel Sambuc } while(n);
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc /* zero value with zero precision should produce no digits */
171ebfedea0SLionel Sambuc if(prec == 0 && num == 0) {
172ebfedea0SLionel Sambuc nlen--;
173ebfedea0SLionel Sambuc nstart++;
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc
176ebfedea0SLionel Sambuc /* figure out what char to use for sign */
177ebfedea0SLionel Sambuc if(minusp)
178ebfedea0SLionel Sambuc signchar = '-';
179ebfedea0SLionel Sambuc else if((flags & plus_flag))
180ebfedea0SLionel Sambuc signchar = '+';
181ebfedea0SLionel Sambuc else if((flags & space_flag))
182ebfedea0SLionel Sambuc signchar = ' ';
183ebfedea0SLionel Sambuc else
184ebfedea0SLionel Sambuc signchar = '\0';
185ebfedea0SLionel Sambuc
186ebfedea0SLionel Sambuc if((flags & alternate_flag) && base == 8) {
187ebfedea0SLionel Sambuc /* if necessary, increase the precision to
188ebfedea0SLionel Sambuc make first digit a zero */
189ebfedea0SLionel Sambuc
190ebfedea0SLionel Sambuc /* XXX C99 claims (regarding # and %o) that "if the value and
191ebfedea0SLionel Sambuc precision are both 0, a single 0 is printed", but there is
192ebfedea0SLionel Sambuc no such wording for %x. This would mean that %#.o would
193ebfedea0SLionel Sambuc output "0", but %#.x "". This does not make sense, and is
194ebfedea0SLionel Sambuc also not what other printf implementations are doing. */
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc if(prec <= nlen && nstr[nstart] != '0' && nstr[nstart] != '\0')
197ebfedea0SLionel Sambuc prec = nlen + 1;
198ebfedea0SLionel Sambuc }
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc /* possible formats:
201ebfedea0SLionel Sambuc pad | sign | alt | zero | digits
202ebfedea0SLionel Sambuc sign | alt | zero | digits | pad minus_flag
203ebfedea0SLionel Sambuc sign | alt | zero | digits zero_flag */
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc /* if not right justifying or padding with zeros, we need to
206ebfedea0SLionel Sambuc compute the length of the rest of the string, and then pad with
207ebfedea0SLionel Sambuc spaces */
208ebfedea0SLionel Sambuc if(!(flags & (minus_flag | zero_flag))) {
209ebfedea0SLionel Sambuc if(prec > nlen)
210ebfedea0SLionel Sambuc width -= prec;
211ebfedea0SLionel Sambuc else
212ebfedea0SLionel Sambuc width -= nlen;
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc if(use_alternative(flags, num, base))
215ebfedea0SLionel Sambuc width -= 2;
216ebfedea0SLionel Sambuc
217ebfedea0SLionel Sambuc if(signchar != '\0')
218ebfedea0SLionel Sambuc width--;
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc /* pad to width */
221ebfedea0SLionel Sambuc len += pad(state, width, ' ');
222ebfedea0SLionel Sambuc }
223ebfedea0SLionel Sambuc if(signchar != '\0') {
224ebfedea0SLionel Sambuc (*state->append_char)(state, signchar);
225ebfedea0SLionel Sambuc ++len;
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc if(use_alternative(flags, num, base)) {
228ebfedea0SLionel Sambuc (*state->append_char)(state, '0');
229ebfedea0SLionel Sambuc (*state->append_char)(state, rep[10] + 23); /* XXX */
230ebfedea0SLionel Sambuc len += 2;
231ebfedea0SLionel Sambuc }
232ebfedea0SLionel Sambuc if(flags & zero_flag) {
233ebfedea0SLionel Sambuc /* pad to width with zeros */
234ebfedea0SLionel Sambuc if(prec - nlen > width - len - nlen)
235ebfedea0SLionel Sambuc len += pad(state, prec - nlen, '0');
236ebfedea0SLionel Sambuc else
237ebfedea0SLionel Sambuc len += pad(state, width - len - nlen, '0');
238ebfedea0SLionel Sambuc } else
239ebfedea0SLionel Sambuc /* pad to prec with zeros */
240ebfedea0SLionel Sambuc len += pad(state, prec - nlen, '0');
241ebfedea0SLionel Sambuc
242ebfedea0SLionel Sambuc while(nstr[nstart] != '\0') {
243ebfedea0SLionel Sambuc (*state->append_char)(state, nstr[nstart++]);
244ebfedea0SLionel Sambuc ++len;
245ebfedea0SLionel Sambuc }
246ebfedea0SLionel Sambuc
247ebfedea0SLionel Sambuc if(flags & minus_flag)
248ebfedea0SLionel Sambuc len += pad(state, width - len, ' ');
249ebfedea0SLionel Sambuc
250ebfedea0SLionel Sambuc return len;
251ebfedea0SLionel Sambuc }
252ebfedea0SLionel Sambuc
253ebfedea0SLionel Sambuc /*
254ebfedea0SLionel Sambuc * return length
255ebfedea0SLionel Sambuc */
256ebfedea0SLionel Sambuc
257ebfedea0SLionel Sambuc static size_t
append_string(struct snprintf_state * state,const unsigned char * arg,int width,int prec,int flags)258ebfedea0SLionel Sambuc append_string (struct snprintf_state *state,
259ebfedea0SLionel Sambuc const unsigned char *arg,
260ebfedea0SLionel Sambuc int width,
261ebfedea0SLionel Sambuc int prec,
262ebfedea0SLionel Sambuc int flags)
263ebfedea0SLionel Sambuc {
264ebfedea0SLionel Sambuc size_t len = 0;
265ebfedea0SLionel Sambuc
266ebfedea0SLionel Sambuc if(arg == NULL)
267ebfedea0SLionel Sambuc arg = (const unsigned char*)"(null)";
268ebfedea0SLionel Sambuc
269ebfedea0SLionel Sambuc if(prec != -1)
270ebfedea0SLionel Sambuc width -= prec;
271ebfedea0SLionel Sambuc else
272ebfedea0SLionel Sambuc width -= strlen((const char *)arg);
273ebfedea0SLionel Sambuc if(!(flags & minus_flag))
274ebfedea0SLionel Sambuc len += pad(state, width, ' ');
275ebfedea0SLionel Sambuc
276ebfedea0SLionel Sambuc if (prec != -1) {
277ebfedea0SLionel Sambuc while (*arg && prec--) {
278ebfedea0SLionel Sambuc (*state->append_char) (state, *arg++);
279ebfedea0SLionel Sambuc ++len;
280ebfedea0SLionel Sambuc }
281ebfedea0SLionel Sambuc } else {
282ebfedea0SLionel Sambuc while (*arg) {
283ebfedea0SLionel Sambuc (*state->append_char) (state, *arg++);
284ebfedea0SLionel Sambuc ++len;
285ebfedea0SLionel Sambuc }
286ebfedea0SLionel Sambuc }
287ebfedea0SLionel Sambuc if(flags & minus_flag)
288ebfedea0SLionel Sambuc len += pad(state, width, ' ');
289ebfedea0SLionel Sambuc return len;
290ebfedea0SLionel Sambuc }
291ebfedea0SLionel Sambuc
292ebfedea0SLionel Sambuc static int
append_char(struct snprintf_state * state,unsigned char arg,int width,int flags)293ebfedea0SLionel Sambuc append_char(struct snprintf_state *state,
294ebfedea0SLionel Sambuc unsigned char arg,
295ebfedea0SLionel Sambuc int width,
296ebfedea0SLionel Sambuc int flags)
297ebfedea0SLionel Sambuc {
298ebfedea0SLionel Sambuc int len = 0;
299ebfedea0SLionel Sambuc
300ebfedea0SLionel Sambuc while(!(flags & minus_flag) && --width > 0) {
301ebfedea0SLionel Sambuc (*state->append_char) (state, ' ') ;
302ebfedea0SLionel Sambuc ++len;
303ebfedea0SLionel Sambuc }
304ebfedea0SLionel Sambuc (*state->append_char) (state, arg);
305ebfedea0SLionel Sambuc ++len;
306ebfedea0SLionel Sambuc while((flags & minus_flag) && --width > 0) {
307ebfedea0SLionel Sambuc (*state->append_char) (state, ' ');
308ebfedea0SLionel Sambuc ++len;
309ebfedea0SLionel Sambuc }
310ebfedea0SLionel Sambuc return 0;
311ebfedea0SLionel Sambuc }
312ebfedea0SLionel Sambuc
313ebfedea0SLionel Sambuc /*
314ebfedea0SLionel Sambuc * This can't be made into a function...
315ebfedea0SLionel Sambuc */
316ebfedea0SLionel Sambuc
317ebfedea0SLionel Sambuc #ifdef HAVE_LONG_LONG
318ebfedea0SLionel Sambuc
319ebfedea0SLionel Sambuc #define PARSE_INT_FORMAT(res, arg, unsig) \
320ebfedea0SLionel Sambuc if (long_long_flag) \
321ebfedea0SLionel Sambuc res = (unsig long long)va_arg(arg, unsig long long); \
322ebfedea0SLionel Sambuc else if (long_flag) \
323ebfedea0SLionel Sambuc res = (unsig long)va_arg(arg, unsig long); \
324ebfedea0SLionel Sambuc else if (size_t_flag) \
325ebfedea0SLionel Sambuc res = (unsig long)va_arg(arg, size_t); \
326ebfedea0SLionel Sambuc else if (short_flag) \
327ebfedea0SLionel Sambuc res = (unsig short)va_arg(arg, unsig int); \
328ebfedea0SLionel Sambuc else \
329ebfedea0SLionel Sambuc res = (unsig int)va_arg(arg, unsig int)
330ebfedea0SLionel Sambuc
331ebfedea0SLionel Sambuc #else
332ebfedea0SLionel Sambuc
333ebfedea0SLionel Sambuc #define PARSE_INT_FORMAT(res, arg, unsig) \
334ebfedea0SLionel Sambuc if (long_flag) \
335ebfedea0SLionel Sambuc res = (unsig long)va_arg(arg, unsig long); \
336ebfedea0SLionel Sambuc else if (size_t_flag) \
337ebfedea0SLionel Sambuc res = (unsig long)va_arg(arg, size_t); \
338ebfedea0SLionel Sambuc else if (short_flag) \
339ebfedea0SLionel Sambuc res = (unsig short)va_arg(arg, unsig int); \
340ebfedea0SLionel Sambuc else \
341ebfedea0SLionel Sambuc res = (unsig int)va_arg(arg, unsig int)
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc #endif
344ebfedea0SLionel Sambuc
345ebfedea0SLionel Sambuc /*
346ebfedea0SLionel Sambuc * zyxprintf - return length, as snprintf
347ebfedea0SLionel Sambuc */
348ebfedea0SLionel Sambuc
349ebfedea0SLionel Sambuc static size_t
xyzprintf(struct snprintf_state * state,const char * char_format,va_list ap)350ebfedea0SLionel Sambuc xyzprintf (struct snprintf_state *state, const char *char_format, va_list ap)
351ebfedea0SLionel Sambuc {
352ebfedea0SLionel Sambuc const unsigned char *format = (const unsigned char *)char_format;
353ebfedea0SLionel Sambuc unsigned char c;
354ebfedea0SLionel Sambuc size_t len = 0;
355ebfedea0SLionel Sambuc
356ebfedea0SLionel Sambuc while((c = *format++)) {
357ebfedea0SLionel Sambuc if (c == '%') {
358ebfedea0SLionel Sambuc int flags = 0;
359ebfedea0SLionel Sambuc int width = 0;
360ebfedea0SLionel Sambuc int prec = -1;
361ebfedea0SLionel Sambuc int size_t_flag = 0;
362ebfedea0SLionel Sambuc int long_long_flag = 0;
363ebfedea0SLionel Sambuc int long_flag = 0;
364ebfedea0SLionel Sambuc int short_flag = 0;
365ebfedea0SLionel Sambuc
366ebfedea0SLionel Sambuc /* flags */
367ebfedea0SLionel Sambuc while((c = *format++)){
368ebfedea0SLionel Sambuc if(c == '-')
369ebfedea0SLionel Sambuc flags |= minus_flag;
370ebfedea0SLionel Sambuc else if(c == '+')
371ebfedea0SLionel Sambuc flags |= plus_flag;
372ebfedea0SLionel Sambuc else if(c == ' ')
373ebfedea0SLionel Sambuc flags |= space_flag;
374ebfedea0SLionel Sambuc else if(c == '#')
375ebfedea0SLionel Sambuc flags |= alternate_flag;
376ebfedea0SLionel Sambuc else if(c == '0')
377ebfedea0SLionel Sambuc flags |= zero_flag;
378ebfedea0SLionel Sambuc else if(c == '\'')
379ebfedea0SLionel Sambuc ; /* just ignore */
380ebfedea0SLionel Sambuc else
381ebfedea0SLionel Sambuc break;
382ebfedea0SLionel Sambuc }
383ebfedea0SLionel Sambuc
384ebfedea0SLionel Sambuc if((flags & space_flag) && (flags & plus_flag))
385ebfedea0SLionel Sambuc flags ^= space_flag;
386ebfedea0SLionel Sambuc
387ebfedea0SLionel Sambuc if((flags & minus_flag) && (flags & zero_flag))
388ebfedea0SLionel Sambuc flags ^= zero_flag;
389ebfedea0SLionel Sambuc
390ebfedea0SLionel Sambuc /* width */
391ebfedea0SLionel Sambuc if (isdigit(c))
392ebfedea0SLionel Sambuc do {
393ebfedea0SLionel Sambuc width = width * 10 + c - '0';
394ebfedea0SLionel Sambuc c = *format++;
395ebfedea0SLionel Sambuc } while(isdigit(c));
396ebfedea0SLionel Sambuc else if(c == '*') {
397ebfedea0SLionel Sambuc width = va_arg(ap, int);
398ebfedea0SLionel Sambuc c = *format++;
399ebfedea0SLionel Sambuc }
400ebfedea0SLionel Sambuc
401ebfedea0SLionel Sambuc /* precision */
402ebfedea0SLionel Sambuc if (c == '.') {
403ebfedea0SLionel Sambuc prec = 0;
404ebfedea0SLionel Sambuc c = *format++;
405ebfedea0SLionel Sambuc if (isdigit(c))
406ebfedea0SLionel Sambuc do {
407ebfedea0SLionel Sambuc prec = prec * 10 + c - '0';
408ebfedea0SLionel Sambuc c = *format++;
409ebfedea0SLionel Sambuc } while(isdigit(c));
410ebfedea0SLionel Sambuc else if (c == '*') {
411ebfedea0SLionel Sambuc prec = va_arg(ap, int);
412ebfedea0SLionel Sambuc c = *format++;
413ebfedea0SLionel Sambuc }
414ebfedea0SLionel Sambuc }
415ebfedea0SLionel Sambuc
416ebfedea0SLionel Sambuc /* size */
417ebfedea0SLionel Sambuc
418ebfedea0SLionel Sambuc if (c == 'h') {
419ebfedea0SLionel Sambuc short_flag = 1;
420ebfedea0SLionel Sambuc c = *format++;
421ebfedea0SLionel Sambuc } else if (c == 'z') {
422ebfedea0SLionel Sambuc size_t_flag = 1;
423ebfedea0SLionel Sambuc c = *format++;
424ebfedea0SLionel Sambuc } else if (c == 'l') {
425ebfedea0SLionel Sambuc long_flag = 1;
426ebfedea0SLionel Sambuc c = *format++;
427ebfedea0SLionel Sambuc if (c == 'l') {
428ebfedea0SLionel Sambuc long_long_flag = 1;
429ebfedea0SLionel Sambuc c = *format++;
430ebfedea0SLionel Sambuc }
431ebfedea0SLionel Sambuc }
432ebfedea0SLionel Sambuc
433ebfedea0SLionel Sambuc if(c != 'd' && c != 'i')
434ebfedea0SLionel Sambuc flags &= ~(plus_flag | space_flag);
435ebfedea0SLionel Sambuc
436ebfedea0SLionel Sambuc switch (c) {
437ebfedea0SLionel Sambuc case 'c' :
438ebfedea0SLionel Sambuc append_char(state, va_arg(ap, int), width, flags);
439ebfedea0SLionel Sambuc ++len;
440ebfedea0SLionel Sambuc break;
441ebfedea0SLionel Sambuc case 's' :
442ebfedea0SLionel Sambuc len += append_string(state,
443ebfedea0SLionel Sambuc va_arg(ap, unsigned char*),
444ebfedea0SLionel Sambuc width,
445ebfedea0SLionel Sambuc prec,
446ebfedea0SLionel Sambuc flags);
447ebfedea0SLionel Sambuc break;
448ebfedea0SLionel Sambuc case 'd' :
449ebfedea0SLionel Sambuc case 'i' : {
450ebfedea0SLionel Sambuc longest arg;
451ebfedea0SLionel Sambuc u_longest num;
452ebfedea0SLionel Sambuc int minusp = 0;
453ebfedea0SLionel Sambuc
454ebfedea0SLionel Sambuc PARSE_INT_FORMAT(arg, ap, signed);
455ebfedea0SLionel Sambuc
456ebfedea0SLionel Sambuc if (arg < 0) {
457ebfedea0SLionel Sambuc minusp = 1;
458ebfedea0SLionel Sambuc num = -arg;
459ebfedea0SLionel Sambuc } else
460ebfedea0SLionel Sambuc num = arg;
461ebfedea0SLionel Sambuc
462ebfedea0SLionel Sambuc len += append_number (state, num, 10, "0123456789",
463ebfedea0SLionel Sambuc width, prec, flags, minusp);
464ebfedea0SLionel Sambuc break;
465ebfedea0SLionel Sambuc }
466ebfedea0SLionel Sambuc case 'u' : {
467ebfedea0SLionel Sambuc u_longest arg;
468ebfedea0SLionel Sambuc
469ebfedea0SLionel Sambuc PARSE_INT_FORMAT(arg, ap, unsigned);
470ebfedea0SLionel Sambuc
471ebfedea0SLionel Sambuc len += append_number (state, arg, 10, "0123456789",
472ebfedea0SLionel Sambuc width, prec, flags, 0);
473ebfedea0SLionel Sambuc break;
474ebfedea0SLionel Sambuc }
475ebfedea0SLionel Sambuc case 'o' : {
476ebfedea0SLionel Sambuc u_longest arg;
477ebfedea0SLionel Sambuc
478ebfedea0SLionel Sambuc PARSE_INT_FORMAT(arg, ap, unsigned);
479ebfedea0SLionel Sambuc
480ebfedea0SLionel Sambuc len += append_number (state, arg, 010, "01234567",
481ebfedea0SLionel Sambuc width, prec, flags, 0);
482ebfedea0SLionel Sambuc break;
483ebfedea0SLionel Sambuc }
484ebfedea0SLionel Sambuc case 'x' : {
485ebfedea0SLionel Sambuc u_longest arg;
486ebfedea0SLionel Sambuc
487ebfedea0SLionel Sambuc PARSE_INT_FORMAT(arg, ap, unsigned);
488ebfedea0SLionel Sambuc
489ebfedea0SLionel Sambuc len += append_number (state, arg, 0x10, "0123456789abcdef",
490ebfedea0SLionel Sambuc width, prec, flags, 0);
491ebfedea0SLionel Sambuc break;
492ebfedea0SLionel Sambuc }
493ebfedea0SLionel Sambuc case 'X' :{
494ebfedea0SLionel Sambuc u_longest arg;
495ebfedea0SLionel Sambuc
496ebfedea0SLionel Sambuc PARSE_INT_FORMAT(arg, ap, unsigned);
497ebfedea0SLionel Sambuc
498ebfedea0SLionel Sambuc len += append_number (state, arg, 0x10, "0123456789ABCDEF",
499ebfedea0SLionel Sambuc width, prec, flags, 0);
500ebfedea0SLionel Sambuc break;
501ebfedea0SLionel Sambuc }
502ebfedea0SLionel Sambuc case 'p' : {
503ebfedea0SLionel Sambuc u_longest arg = (u_longest)va_arg(ap, void*);
504ebfedea0SLionel Sambuc
505ebfedea0SLionel Sambuc len += append_number (state, arg, 0x10, "0123456789ABCDEF",
506ebfedea0SLionel Sambuc width, prec, flags, 0);
507ebfedea0SLionel Sambuc break;
508ebfedea0SLionel Sambuc }
509ebfedea0SLionel Sambuc case 'n' : {
510ebfedea0SLionel Sambuc int *arg = va_arg(ap, int*);
511ebfedea0SLionel Sambuc *arg = state->s - state->str;
512ebfedea0SLionel Sambuc break;
513ebfedea0SLionel Sambuc }
514ebfedea0SLionel Sambuc case '\0' :
515ebfedea0SLionel Sambuc --format;
516ebfedea0SLionel Sambuc /* FALLTHROUGH */
517ebfedea0SLionel Sambuc case '%' :
518ebfedea0SLionel Sambuc (*state->append_char)(state, c);
519ebfedea0SLionel Sambuc ++len;
520ebfedea0SLionel Sambuc break;
521ebfedea0SLionel Sambuc default :
522ebfedea0SLionel Sambuc (*state->append_char)(state, '%');
523ebfedea0SLionel Sambuc (*state->append_char)(state, c);
524ebfedea0SLionel Sambuc len += 2;
525ebfedea0SLionel Sambuc break;
526ebfedea0SLionel Sambuc }
527ebfedea0SLionel Sambuc } else {
528ebfedea0SLionel Sambuc (*state->append_char) (state, c);
529ebfedea0SLionel Sambuc ++len;
530ebfedea0SLionel Sambuc }
531ebfedea0SLionel Sambuc }
532ebfedea0SLionel Sambuc return len;
533ebfedea0SLionel Sambuc }
534ebfedea0SLionel Sambuc
535ebfedea0SLionel Sambuc #if !defined(HAVE_SNPRINTF) || defined(TEST_SNPRINTF)
536ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_snprintf(char * str,size_t sz,const char * format,...)537ebfedea0SLionel Sambuc rk_snprintf (char *str, size_t sz, const char *format, ...)
538ebfedea0SLionel Sambuc {
539ebfedea0SLionel Sambuc va_list args;
540ebfedea0SLionel Sambuc int ret;
541ebfedea0SLionel Sambuc
542ebfedea0SLionel Sambuc va_start(args, format);
543ebfedea0SLionel Sambuc ret = vsnprintf (str, sz, format, args);
544ebfedea0SLionel Sambuc va_end(args);
545ebfedea0SLionel Sambuc
546ebfedea0SLionel Sambuc #ifdef PARANOIA
547ebfedea0SLionel Sambuc {
548ebfedea0SLionel Sambuc int ret2;
549ebfedea0SLionel Sambuc char *tmp;
550ebfedea0SLionel Sambuc
551ebfedea0SLionel Sambuc tmp = malloc (sz);
552ebfedea0SLionel Sambuc if (tmp == NULL)
553ebfedea0SLionel Sambuc abort ();
554ebfedea0SLionel Sambuc
555ebfedea0SLionel Sambuc va_start(args, format);
556ebfedea0SLionel Sambuc ret2 = vsprintf (tmp, format, args);
557ebfedea0SLionel Sambuc va_end(args);
558ebfedea0SLionel Sambuc if (ret != ret2 || strcmp(str, tmp))
559ebfedea0SLionel Sambuc abort ();
560ebfedea0SLionel Sambuc free (tmp);
561ebfedea0SLionel Sambuc }
562ebfedea0SLionel Sambuc #endif
563ebfedea0SLionel Sambuc
564ebfedea0SLionel Sambuc return ret;
565ebfedea0SLionel Sambuc }
566ebfedea0SLionel Sambuc #endif
567ebfedea0SLionel Sambuc
568ebfedea0SLionel Sambuc #if !defined(HAVE_ASPRINTF) || defined(TEST_SNPRINTF)
569ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_asprintf(char ** ret,const char * format,...)570ebfedea0SLionel Sambuc rk_asprintf (char **ret, const char *format, ...)
571ebfedea0SLionel Sambuc {
572ebfedea0SLionel Sambuc va_list args;
573ebfedea0SLionel Sambuc int val;
574ebfedea0SLionel Sambuc
575ebfedea0SLionel Sambuc va_start(args, format);
576ebfedea0SLionel Sambuc val = vasprintf (ret, format, args);
577ebfedea0SLionel Sambuc va_end(args);
578ebfedea0SLionel Sambuc
579ebfedea0SLionel Sambuc #ifdef PARANOIA
580ebfedea0SLionel Sambuc {
581ebfedea0SLionel Sambuc int ret2;
582ebfedea0SLionel Sambuc char *tmp;
583ebfedea0SLionel Sambuc tmp = malloc (val + 1);
584ebfedea0SLionel Sambuc if (tmp == NULL)
585ebfedea0SLionel Sambuc abort ();
586ebfedea0SLionel Sambuc
587ebfedea0SLionel Sambuc va_start(args, format);
588ebfedea0SLionel Sambuc ret2 = vsprintf (tmp, format, args);
589ebfedea0SLionel Sambuc va_end(args);
590ebfedea0SLionel Sambuc if (val != ret2 || strcmp(*ret, tmp))
591ebfedea0SLionel Sambuc abort ();
592ebfedea0SLionel Sambuc free (tmp);
593ebfedea0SLionel Sambuc }
594ebfedea0SLionel Sambuc #endif
595ebfedea0SLionel Sambuc
596ebfedea0SLionel Sambuc return val;
597ebfedea0SLionel Sambuc }
598ebfedea0SLionel Sambuc #endif
599ebfedea0SLionel Sambuc
600ebfedea0SLionel Sambuc #if !defined(HAVE_ASNPRINTF) || defined(TEST_SNPRINTF)
601ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_asnprintf(char ** ret,size_t max_sz,const char * format,...)602ebfedea0SLionel Sambuc rk_asnprintf (char **ret, size_t max_sz, const char *format, ...)
603ebfedea0SLionel Sambuc {
604ebfedea0SLionel Sambuc va_list args;
605ebfedea0SLionel Sambuc int val;
606ebfedea0SLionel Sambuc
607ebfedea0SLionel Sambuc va_start(args, format);
608ebfedea0SLionel Sambuc val = vasnprintf (ret, max_sz, format, args);
609ebfedea0SLionel Sambuc
610ebfedea0SLionel Sambuc #ifdef PARANOIA
611ebfedea0SLionel Sambuc {
612ebfedea0SLionel Sambuc int ret2;
613ebfedea0SLionel Sambuc char *tmp;
614ebfedea0SLionel Sambuc tmp = malloc (val + 1);
615ebfedea0SLionel Sambuc if (tmp == NULL)
616ebfedea0SLionel Sambuc abort ();
617ebfedea0SLionel Sambuc
618ebfedea0SLionel Sambuc ret2 = vsprintf (tmp, format, args);
619ebfedea0SLionel Sambuc if (val != ret2 || strcmp(*ret, tmp))
620ebfedea0SLionel Sambuc abort ();
621ebfedea0SLionel Sambuc free (tmp);
622ebfedea0SLionel Sambuc }
623ebfedea0SLionel Sambuc #endif
624ebfedea0SLionel Sambuc
625ebfedea0SLionel Sambuc va_end(args);
626ebfedea0SLionel Sambuc return val;
627ebfedea0SLionel Sambuc }
628ebfedea0SLionel Sambuc #endif
629ebfedea0SLionel Sambuc
630ebfedea0SLionel Sambuc #if !defined(HAVE_VASPRINTF) || defined(TEST_SNPRINTF)
631ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_vasprintf(char ** ret,const char * format,va_list args)632ebfedea0SLionel Sambuc rk_vasprintf (char **ret, const char *format, va_list args)
633ebfedea0SLionel Sambuc {
634ebfedea0SLionel Sambuc return vasnprintf (ret, 0, format, args);
635ebfedea0SLionel Sambuc }
636ebfedea0SLionel Sambuc #endif
637ebfedea0SLionel Sambuc
638ebfedea0SLionel Sambuc
639ebfedea0SLionel Sambuc #if !defined(HAVE_VASNPRINTF) || defined(TEST_SNPRINTF)
640ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_vasnprintf(char ** ret,size_t max_sz,const char * format,va_list args)641ebfedea0SLionel Sambuc rk_vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
642ebfedea0SLionel Sambuc {
643ebfedea0SLionel Sambuc size_t st;
644ebfedea0SLionel Sambuc struct snprintf_state state;
645ebfedea0SLionel Sambuc
646ebfedea0SLionel Sambuc state.max_sz = max_sz;
647ebfedea0SLionel Sambuc state.sz = 1;
648ebfedea0SLionel Sambuc state.str = malloc(state.sz);
649ebfedea0SLionel Sambuc if (state.str == NULL) {
650ebfedea0SLionel Sambuc *ret = NULL;
651ebfedea0SLionel Sambuc return -1;
652ebfedea0SLionel Sambuc }
653ebfedea0SLionel Sambuc state.s = state.str;
654ebfedea0SLionel Sambuc state.theend = state.s + state.sz - 1;
655ebfedea0SLionel Sambuc state.append_char = as_append_char;
656ebfedea0SLionel Sambuc
657ebfedea0SLionel Sambuc st = xyzprintf (&state, format, args);
658ebfedea0SLionel Sambuc if (st > state.sz) {
659ebfedea0SLionel Sambuc free (state.str);
660ebfedea0SLionel Sambuc *ret = NULL;
661ebfedea0SLionel Sambuc return -1;
662ebfedea0SLionel Sambuc } else {
663ebfedea0SLionel Sambuc char *tmp;
664ebfedea0SLionel Sambuc
665ebfedea0SLionel Sambuc *state.s = '\0';
666ebfedea0SLionel Sambuc tmp = realloc (state.str, st+1);
667ebfedea0SLionel Sambuc if (tmp == NULL) {
668ebfedea0SLionel Sambuc free (state.str);
669ebfedea0SLionel Sambuc *ret = NULL;
670ebfedea0SLionel Sambuc return -1;
671ebfedea0SLionel Sambuc }
672ebfedea0SLionel Sambuc *ret = tmp;
673ebfedea0SLionel Sambuc return st;
674ebfedea0SLionel Sambuc }
675ebfedea0SLionel Sambuc }
676ebfedea0SLionel Sambuc #endif
677ebfedea0SLionel Sambuc
678ebfedea0SLionel Sambuc #if !defined(HAVE_VSNPRINTF) || defined(TEST_SNPRINTF)
679ebfedea0SLionel Sambuc ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_vsnprintf(char * str,size_t sz,const char * format,va_list args)680ebfedea0SLionel Sambuc rk_vsnprintf (char *str, size_t sz, const char *format, va_list args)
681ebfedea0SLionel Sambuc {
682ebfedea0SLionel Sambuc struct snprintf_state state;
683ebfedea0SLionel Sambuc int ret;
684ebfedea0SLionel Sambuc unsigned char *ustr = (unsigned char *)str;
685ebfedea0SLionel Sambuc
686ebfedea0SLionel Sambuc state.max_sz = 0;
687ebfedea0SLionel Sambuc state.sz = sz;
688ebfedea0SLionel Sambuc state.str = ustr;
689ebfedea0SLionel Sambuc state.s = ustr;
690ebfedea0SLionel Sambuc state.theend = ustr + sz - (sz > 0);
691ebfedea0SLionel Sambuc state.append_char = sn_append_char;
692ebfedea0SLionel Sambuc
693ebfedea0SLionel Sambuc ret = xyzprintf (&state, format, args);
694ebfedea0SLionel Sambuc if (state.s != NULL && sz != 0)
695ebfedea0SLionel Sambuc *state.s = '\0';
696ebfedea0SLionel Sambuc return ret;
697ebfedea0SLionel Sambuc }
698ebfedea0SLionel Sambuc #endif
699