]>
The Tcpdump Group git mirrors - tcpdump/blob - missing/snprintf.c
2 * Copyright (c) 1995-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
43 #include <sys/types.h>
45 #include "netdissect.h"
62 unsigned char *theend
;
65 int (*append_char
)(struct state
*, unsigned char);
66 int (*reserve
)(struct state
*, size_t);
72 as_reserve (struct state
*state
, size_t n
)
74 if (state
->s
+ n
> state
->theend
) {
75 int off
= state
->s
- state
->str
;
78 if (state
->max_sz
&& state
->sz
>= state
->max_sz
)
81 state
->sz
= max(state
->sz
* 2, state
->sz
+ n
);
83 state
->sz
= min(state
->sz
, state
->max_sz
);
84 tmp
= realloc (state
->str
, state
->sz
);
88 state
->s
= state
->str
+ off
;
89 state
->theend
= state
->str
+ state
->sz
- 1;
95 as_append_char (struct state
*state
, unsigned char c
)
97 if(as_reserve (state
, 1))
107 append_number(struct state
*state
,
108 unsigned long num
, unsigned base
, char *rep
,
109 int width
, int prec
, int flags
, int minusp
)
114 /* given precision, ignore zero flag */
119 /* zero value with zero precision -> "" */
120 if(prec
== 0 && num
== 0)
123 if((*state
->append_char
)(state
, rep
[num
% base
]))
129 /* pad with prec zeros */
131 if((*state
->append_char
)(state
, '0'))
135 /* add length of alternate prefix (added later) to len */
136 if(flags
& alternate_flag
&& (base
== 16 || base
== 8))
139 if(flags
& zero_flag
){
141 if(minusp
|| (flags
& space_flag
) || (flags
& plus_flag
))
144 if((*state
->append_char
)(state
, '0'))
149 /* add alternate prefix */
150 if(flags
& alternate_flag
&& (base
== 16 || base
== 8)){
152 if((*state
->append_char
)(state
, rep
[10] + 23)) /* XXX */
154 if((*state
->append_char
)(state
, '0'))
159 if((*state
->append_char
)(state
, '-'))
162 } else if(flags
& plus_flag
) {
163 if((*state
->append_char
)(state
, '+'))
166 } else if(flags
& space_flag
) {
167 if((*state
->append_char
)(state
, ' '))
171 if(flags
& minus_flag
)
172 /* swap before padding with spaces */
173 for(i
= 0; i
< len
/ 2; i
++){
174 char c
= state
->s
[-i
-1];
175 state
->s
[-i
-1] = state
->s
[-len
+i
];
176 state
->s
[-len
+i
] = c
;
180 if((*state
->append_char
)(state
, ' '))
184 if(!(flags
& minus_flag
))
185 /* swap after padding with spaces */
186 for(i
= 0; i
< len
/ 2; i
++){
187 char c
= state
->s
[-i
-1];
188 state
->s
[-i
-1] = state
->s
[-len
+i
];
189 state
->s
[-len
+i
] = c
;
196 append_string (struct state
*state
,
205 width
-= strlen((char *)arg
);
206 if(!(flags
& minus_flag
))
208 if((*state
->append_char
) (state
, ' '))
211 while (*arg
&& prec
--)
212 if ((*state
->append_char
) (state
, *arg
++))
216 if ((*state
->append_char
) (state
, *arg
++))
219 if(flags
& minus_flag
)
221 if((*state
->append_char
) (state
, ' '))
227 append_char(struct state
*state
,
232 while(!(flags
& minus_flag
) && --width
> 0)
233 if((*state
->append_char
) (state
, ' '))
236 if((*state
->append_char
) (state
, arg
))
238 while((flags
& minus_flag
) && --width
> 0)
239 if((*state
->append_char
) (state
, ' '))
246 * This can't be made into a function...
249 #define PARSE_INT_FORMAT(res, arg, unsig) \
251 res = (unsig long)va_arg(arg, unsig long); \
252 else if (short_flag) \
253 res = (unsig short)va_arg(arg, unsig int); \
255 res = (unsig int)va_arg(arg, unsig int)
258 * zyxprintf - return 0 or -1
262 xyzprintf (struct state
*state
, const char *char_format
, va_list ap
)
264 const unsigned char *format
= (const unsigned char *)char_format
;
267 while((c
= *format
++)) {
276 while((c
= *format
++)){
284 flags
|= alternate_flag
;
291 if((flags
& space_flag
) && (flags
& plus_flag
))
294 if((flags
& minus_flag
) && (flags
& zero_flag
))
300 width
= width
* 10 + c
- '0';
304 width
= va_arg(ap
, int);
314 prec
= prec
* 10 + c
- '0';
318 prec
= va_arg(ap
, int);
328 } else if (c
== 'l') {
335 if(append_char(state
, va_arg(ap
, int), width
, flags
))
339 if (append_string(state
,
340 va_arg(ap
, unsigned char*),
352 PARSE_INT_FORMAT(arg
, ap
, signed);
360 if (append_number (state
, num
, 10, "0123456789",
361 width
, prec
, flags
, minusp
))
368 PARSE_INT_FORMAT(arg
, ap
, unsigned);
370 if (append_number (state
, arg
, 10, "0123456789",
371 width
, prec
, flags
, 0))
378 PARSE_INT_FORMAT(arg
, ap
, unsigned);
380 if (append_number (state
, arg
, 010, "01234567",
381 width
, prec
, flags
, 0))
388 PARSE_INT_FORMAT(arg
, ap
, unsigned);
390 if (append_number (state
, arg
, 0x10, "0123456789abcdef",
391 width
, prec
, flags
, 0))
398 PARSE_INT_FORMAT(arg
, ap
, unsigned);
400 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
401 width
, prec
, flags
, 0))
406 unsigned long arg
= (unsigned long)va_arg(ap
, void*);
408 if (append_number (state
, arg
, 0x10, "0123456789ABCDEF",
409 width
, prec
, flags
, 0))
414 int *arg
= va_arg(ap
, int *);
415 *arg
= state
->s
- state
->str
;
422 if ((*state
->append_char
)(state
, c
))
426 if ( (*state
->append_char
)(state
, '%')
427 || (*state
->append_char
)(state
, c
))
432 if ((*state
->append_char
) (state
, c
))
439 #ifndef HAVE_ASPRINTF
441 asprintf (char **ret
, const char *format
, ...)
446 va_start(args
, format
);
447 val
= vasprintf (ret
, format
, args
);
453 tmp
= malloc (val
+ 1);
457 ret2
= vsprintf (tmp
, format
, args
);
458 if (val
!= ret2
|| strcmp(*ret
, tmp
))
469 #ifndef HAVE_VASNPRINTF
471 nd_vasnprintf (char **ret
, size_t max_sz
, const char *format
, va_list args
)
477 state
.max_sz
= max_sz
;
479 state
.str
= malloc(state
.sz
);
480 if (state
.str
== NULL
) {
485 state
.theend
= state
.s
+ state
.sz
- 1;
486 state
.append_char
= as_append_char
;
487 state
.reserve
= as_reserve
;
489 st
= xyzprintf (&state
, format
, args
);
498 len
= state
.s
- state
.str
;
499 tmp
= realloc (state
.str
, len
+1);