2 * Copyright (c) 1995-1997, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: snprintf.c,v 1.24 1999/12/02 16:58:52 joda Exp $");
60 unsigned char *theend;
63 int (*append_char)(struct state *, unsigned char);
64 int (*reserve)(struct state *, size_t);
68 #ifndef HAVE_VSNPRINTF
70 sn_reserve (struct state *state, size_t n)
72 return state->s + n > state->theend;
76 sn_append_char (struct state *state, unsigned char c)
78 if (sn_reserve (state, 1)) {
88 as_reserve (struct state *state, size_t n)
90 if (state->s + n > state->theend) {
91 int off = state->s - state->str;
94 if (state->max_sz && state->sz >= state->max_sz)
97 state->sz = max(state->sz * 2, state->sz + n);
99 state->sz = min(state->sz, state->max_sz);
100 tmp = realloc (state->str, state->sz);
104 state->s = state->str + off;
105 state->theend = state->str + state->sz - 1;
111 as_append_char (struct state *state, unsigned char c)
113 if(as_reserve (state, 1))
122 append_number(struct state *state,
123 unsigned long num, unsigned base, char *rep,
124 int width, int prec, int flags, int minusp)
129 /* given precision, ignore zero flag */
134 /* zero value with zero precision -> "" */
135 if(prec == 0 && num == 0)
138 if((*state->append_char)(state, rep[num % base]))
144 /* pad with prec zeros */
146 if((*state->append_char)(state, '0'))
150 /* add length of alternate prefix (added later) to len */
151 if(flags & alternate_flag && (base == 16 || base == 8))
154 if(flags & zero_flag){
156 if(minusp || (flags & space_flag) || (flags & plus_flag))
159 if((*state->append_char)(state, '0'))
164 /* add alternate prefix */
165 if(flags & alternate_flag && (base == 16 || base == 8)){
167 if((*state->append_char)(state, rep[10] + 23)) /* XXX */
169 if((*state->append_char)(state, '0'))
174 if((*state->append_char)(state, '-'))
177 } else if(flags & plus_flag) {
178 if((*state->append_char)(state, '+'))
181 } else if(flags & space_flag) {
182 if((*state->append_char)(state, ' '))
186 if(flags & minus_flag)
187 /* swap before padding with spaces */
188 for(i = 0; i < len / 2; i++){
189 char c = state->s[-i-1];
190 state->s[-i-1] = state->s[-len+i];
191 state->s[-len+i] = c;
195 if((*state->append_char)(state, ' '))
199 if(!(flags & minus_flag))
200 /* swap after padding with spaces */
201 for(i = 0; i < len / 2; i++){
202 char c = state->s[-i-1];
203 state->s[-i-1] = state->s[-len+i];
204 state->s[-len+i] = c;
211 append_string (struct state *state,
220 width -= strlen((char *)arg);
221 if(!(flags & minus_flag))
223 if((*state->append_char) (state, ' '))
226 while (*arg && prec--)
227 if ((*state->append_char) (state, *arg++))
231 if ((*state->append_char) (state, *arg++))
234 if(flags & minus_flag)
236 if((*state->append_char) (state, ' '))
242 append_char(struct state *state,
247 while(!(flags & minus_flag) && --width > 0)
248 if((*state->append_char) (state, ' '))
251 if((*state->append_char) (state, arg))
253 while((flags & minus_flag) && --width > 0)
254 if((*state->append_char) (state, ' '))
261 * This can't be made into a function...
264 #define PARSE_INT_FORMAT(res, arg, unsig) \
266 res = (unsig long)va_arg(arg, unsig long); \
267 else if (short_flag) \
268 res = (unsig short)va_arg(arg, unsig short); \
270 res = (unsig int)va_arg(arg, unsig int)
273 * zyxprintf - return 0 or -1
277 xyzprintf (struct state *state, const char *char_format, va_list ap)
279 const unsigned char *format = (const unsigned char *)char_format;
282 while((c = *format++)) {
291 while((c = *format++)){
299 flags |= alternate_flag;
306 if((flags & space_flag) && (flags & plus_flag))
309 if((flags & minus_flag) && (flags & zero_flag))
315 width = width * 10 + c - '0';
319 width = va_arg(ap, int);
329 prec = prec * 10 + c - '0';
333 prec = va_arg(ap, int);
343 } else if (c == 'l') {
350 if(append_char(state, va_arg(ap, int), width, flags))
354 if (append_string(state,
355 va_arg(ap, unsigned char*),
367 PARSE_INT_FORMAT(arg, ap, signed);
375 if (append_number (state, num, 10, "0123456789",
376 width, prec, flags, minusp))
383 PARSE_INT_FORMAT(arg, ap, unsigned);
385 if (append_number (state, arg, 10, "0123456789",
386 width, prec, flags, 0))
393 PARSE_INT_FORMAT(arg, ap, unsigned);
395 if (append_number (state, arg, 010, "01234567",
396 width, prec, flags, 0))
403 PARSE_INT_FORMAT(arg, ap, unsigned);
405 if (append_number (state, arg, 0x10, "0123456789abcdef",
406 width, prec, flags, 0))
413 PARSE_INT_FORMAT(arg, ap, unsigned);
415 if (append_number (state, arg, 0x10, "0123456789ABCDEF",
416 width, prec, flags, 0))
421 unsigned long arg = (unsigned long)va_arg(ap, void*);
423 if (append_number (state, arg, 0x10, "0123456789ABCDEF",
424 width, prec, flags, 0))
429 int *arg = va_arg(ap, int*);
430 *arg = state->s - state->str;
437 if ((*state->append_char)(state, c))
441 if ( (*state->append_char)(state, '%')
442 || (*state->append_char)(state, c))
447 if ((*state->append_char) (state, c))
453 #ifndef HAVE_SNPRINTF
455 snprintf (char *str, size_t sz, const char *format, ...)
460 va_start(args, format);
461 ret = vsnprintf (str, sz, format, args);
472 ret2 = vsprintf (tmp, format, args);
473 if (ret != ret2 || strcmp(str, tmp))
484 #ifndef HAVE_ASPRINTF
486 asprintf (char **ret, const char *format, ...)
491 va_start(args, format);
492 val = vasprintf (ret, format, args);
498 tmp = malloc (val + 1);
502 ret2 = vsprintf (tmp, format, args);
503 if (val != ret2 || strcmp(*ret, tmp))
514 #ifndef HAVE_ASNPRINTF
516 asnprintf (char **ret, size_t max_sz, const char *format, ...)
521 va_start(args, format);
522 val = vasnprintf (ret, max_sz, format, args);
528 tmp = malloc (val + 1);
532 ret2 = vsprintf (tmp, format, args);
533 if (val != ret2 || strcmp(*ret, tmp))
544 #ifndef HAVE_VASPRINTF
546 vasprintf (char **ret, const char *format, va_list args)
548 return vasnprintf (ret, 0, format, args);
553 #ifndef HAVE_VASNPRINTF
555 vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
561 state.max_sz = max_sz;
563 state.str = malloc(state.sz);
564 if (state.str == NULL) {
569 state.theend = state.s + state.sz - 1;
570 state.append_char = as_append_char;
571 state.reserve = as_reserve;
573 st = xyzprintf (&state, format, args);
582 len = state.s - state.str;
583 tmp = realloc (state.str, len+1);
595 #ifndef HAVE_VSNPRINTF
597 vsnprintf (char *str, size_t sz, const char *format, va_list args)
601 unsigned char *ustr = (unsigned char *)str;
607 state.theend = ustr + sz - 1;
608 state.append_char = sn_append_char;
609 state.reserve = sn_reserve;
611 ret = xyzprintf (&state, format, args);
616 return state.s - state.str;