Annotation of embedaddon/bird/lib/printf.c, revision 1.1.1.2
1.1 misho 1: /*
2: * BIRD Library -- Formatted Output
3: *
4: * (c) 1991, 1992 Lars Wirzenius & Linus Torvalds
5: *
6: * Hacked up for BIRD by Martin Mares <mj@ucw.cz>
7: * Buffer size limitation implemented by Martin Mares.
8: */
9:
10: #include "nest/bird.h"
11: #include "string.h"
12:
13: #include <errno.h>
14:
15: #include "nest/iface.h"
16:
17: /* we use this so that we can do without the ctype library */
18: #define is_digit(c) ((c) >= '0' && (c) <= '9')
19:
20: static int skip_atoi(const char **s)
21: {
22: int i=0;
23:
24: while (is_digit(**s))
25: i = i*10 + *((*s)++) - '0';
26: return i;
27: }
28:
29: #define ZEROPAD 1 /* pad with zero */
30: #define SIGN 2 /* unsigned/signed long */
31: #define PLUS 4 /* show plus */
32: #define SPACE 8 /* space if plus */
33: #define LEFT 16 /* left justified */
34: #define SPECIAL 32 /* 0x */
35: #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
36:
37: #define do_div(n,base) ({ \
38: int __res; \
39: __res = ((unsigned long) n) % (unsigned) base; \
40: n = ((unsigned long) n) / (unsigned) base; \
41: __res; })
42:
43: static char * number(char * str, long num, int base, int size, int precision,
44: int type, int remains)
45: {
46: char c,sign,tmp[66];
47: const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
48: int i;
49:
50: if (size >= 0 && (remains -= size) < 0)
51: return NULL;
52: if (type & LARGE)
53: digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
54: if (type & LEFT)
55: type &= ~ZEROPAD;
56: if (base < 2 || base > 36)
57: return 0;
58: c = (type & ZEROPAD) ? '0' : ' ';
59: sign = 0;
60: if (type & SIGN) {
61: if (num < 0) {
62: sign = '-';
63: num = -num;
64: size--;
65: } else if (type & PLUS) {
66: sign = '+';
67: size--;
68: } else if (type & SPACE) {
69: sign = ' ';
70: size--;
71: }
72: }
73: if (type & SPECIAL) {
74: if (base == 16)
75: size -= 2;
76: else if (base == 8)
77: size--;
78: }
79: i = 0;
80: if (num == 0)
81: tmp[i++]='0';
82: else while (num != 0)
83: tmp[i++] = digits[do_div(num,base)];
84: if (i > precision)
85: precision = i;
86: size -= precision;
87: if (size < 0 && -size > remains)
88: return NULL;
89: if (!(type&(ZEROPAD+LEFT)))
90: while(size-->0)
91: *str++ = ' ';
92: if (sign)
93: *str++ = sign;
94: if (type & SPECIAL) {
95: if (base==8)
96: *str++ = '0';
97: else if (base==16) {
98: *str++ = '0';
99: *str++ = digits[33];
100: }
101: }
102: if (!(type & LEFT))
103: while (size-- > 0)
104: *str++ = c;
105: while (i < precision--)
106: *str++ = '0';
107: while (i-- > 0)
108: *str++ = tmp[i];
109: while (size-- > 0)
110: *str++ = ' ';
111: return str;
112: }
113:
114: /**
115: * bvsnprintf - BIRD's vsnprintf()
116: * @buf: destination buffer
117: * @size: size of the buffer
118: * @fmt: format string
119: * @args: a list of arguments to be formatted
120: *
121: * This functions acts like ordinary sprintf() except that it checks
122: * available space to avoid buffer overflows and it allows some more
123: * format specifiers: |%I| for formatting of IP addresses (any non-zero
124: * width is automatically replaced by standard IP address width which
125: * depends on whether we use IPv4 or IPv6; |%#I| gives hexadecimal format),
126: * |%R| for Router / Network ID (u32 value printed as IPv4 address)
127: * |%lR| for 64bit Router / Network ID (u64 value printed as eight :-separated octets)
128: * and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to
129: * message text). On the other hand, it doesn't support floating
130: * point numbers.
131: *
132: * Result: number of characters of the output string or -1 if
133: * the buffer space was insufficient.
134: */
135: int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
136: {
137: int len;
138: unsigned long num;
139: int i, base;
140: u32 x;
141: u64 X;
142: char *str, *start;
143: const char *s;
144: char ipbuf[MAX(STD_ADDRESS_P_LENGTH,ROUTER_ID_64_LENGTH)+1];
145: struct iface *iface;
146:
147: int flags; /* flags to number() */
148:
149: int field_width; /* width of output field */
150: int precision; /* min. # of digits for integers; max
151: number of chars for from string */
152: int qualifier; /* 'h', 'l', or 'L' for integer fields */
153:
154: for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
155: if (*fmt != '%') {
156: if (!size)
157: return -1;
158: *str++ = *fmt;
159: continue;
160: }
161:
162: /* process flags */
163: flags = 0;
164: repeat:
165: ++fmt; /* this also skips first '%' */
166: switch (*fmt) {
167: case '-': flags |= LEFT; goto repeat;
168: case '+': flags |= PLUS; goto repeat;
169: case ' ': flags |= SPACE; goto repeat;
170: case '#': flags |= SPECIAL; goto repeat;
171: case '0': flags |= ZEROPAD; goto repeat;
172: }
173:
174: /* get field width */
175: field_width = -1;
176: if (is_digit(*fmt))
177: field_width = skip_atoi(&fmt);
178: else if (*fmt == '*') {
179: ++fmt;
180: /* it's the next argument */
181: field_width = va_arg(args, int);
182: if (field_width < 0) {
183: field_width = -field_width;
184: flags |= LEFT;
185: }
186: }
187:
188: /* get the precision */
189: precision = -1;
190: if (*fmt == '.') {
191: ++fmt;
192: if (is_digit(*fmt))
193: precision = skip_atoi(&fmt);
194: else if (*fmt == '*') {
195: ++fmt;
196: /* it's the next argument */
197: precision = va_arg(args, int);
198: }
199: if (precision < 0)
200: precision = 0;
201: }
202:
203: /* get the conversion qualifier */
204: qualifier = -1;
205: if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
206: qualifier = *fmt;
207: ++fmt;
208: }
209:
210: /* default base */
211: base = 10;
212:
213: if (field_width > size)
214: return -1;
215: switch (*fmt) {
216: case 'c':
217: if (!(flags & LEFT))
218: while (--field_width > 0)
219: *str++ = ' ';
220: *str++ = (byte) va_arg(args, int);
221: while (--field_width > 0)
222: *str++ = ' ';
223: continue;
224:
225: case 'm':
226: if (flags & SPECIAL) {
227: if (!errno)
228: continue;
229: if (size < 2)
230: return -1;
231: *str++ = ':';
232: *str++ = ' ';
233: start += 2;
234: size -= 2;
235: }
236: s = strerror(errno);
237: goto str;
238: case 'M':
239: s = strerror(va_arg(args, int));
240: goto str;
241: case 's':
242: s = va_arg(args, char *);
243: if (!s)
244: s = "<NULL>";
245:
246: str:
247: len = strlen(s);
248: if (precision >= 0 && len > precision)
249: len = precision;
250: if (len > size)
251: return -1;
252:
253: if (!(flags & LEFT))
254: while (len < field_width--)
255: *str++ = ' ';
256: for (i = 0; i < len; ++i)
257: *str++ = *s++;
258: while (len < field_width--)
259: *str++ = ' ';
260: continue;
261:
262: case 'p':
263: if (field_width == -1) {
264: field_width = 2*sizeof(void *);
265: flags |= ZEROPAD;
266: }
267: str = number(str,
268: (unsigned long) va_arg(args, void *), 16,
269: field_width, precision, flags, size);
270: if (!str)
271: return -1;
272: continue;
273:
274:
275: case 'n':
276: if (qualifier == 'l') {
277: long * ip = va_arg(args, long *);
278: *ip = (str - buf);
279: } else {
280: int * ip = va_arg(args, int *);
281: *ip = (str - buf);
282: }
283: continue;
284:
285: /* IP address */
286: case 'I':
287: if (flags & SPECIAL)
288: ipa_ntox(va_arg(args, ip_addr), ipbuf);
289: else {
290: ipa_ntop(va_arg(args, ip_addr), ipbuf);
291: if (field_width == 1)
292: field_width = STD_ADDRESS_P_LENGTH;
293: }
294: s = ipbuf;
295: goto str;
296:
297: /* Interface scope after link-local IP address */
298: case 'J':
299: iface = va_arg(args, struct iface *);
300: if (!iface)
301: continue;
302: if (!size)
303: return -1;
304:
305: *str++ = '%';
306: start++;
307: size--;
308:
309: s = iface->name;
310: goto str;
311:
312: /* Router/Network ID - essentially IPv4 address in u32 value */
313: case 'R':
314: if(qualifier == 'l') {
315: X = va_arg(args, u64);
316: bsprintf(ipbuf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1.1.1.2 ! misho 317: (uint) ((X >> 56) & 0xff),
! 318: (uint) ((X >> 48) & 0xff),
! 319: (uint) ((X >> 40) & 0xff),
! 320: (uint) ((X >> 32) & 0xff),
! 321: (uint) ((X >> 24) & 0xff),
! 322: (uint) ((X >> 16) & 0xff),
! 323: (uint) ((X >> 8) & 0xff),
! 324: (uint) (X & 0xff));
1.1 misho 325: }
326: else
327: {
328: x = va_arg(args, u32);
329: bsprintf(ipbuf, "%d.%d.%d.%d",
330: ((x >> 24) & 0xff),
331: ((x >> 16) & 0xff),
332: ((x >> 8) & 0xff),
333: (x & 0xff));
334: }
335: s = ipbuf;
336: goto str;
337:
338: /* integer number formats - set up the flags and "break" */
339: case 'o':
340: base = 8;
341: break;
342:
343: case 'X':
344: flags |= LARGE;
1.1.1.2 ! misho 345: /* fallthrough */
1.1 misho 346: case 'x':
347: base = 16;
348: break;
349:
350: case 'd':
351: case 'i':
352: flags |= SIGN;
353: case 'u':
354: break;
355:
356: default:
357: if (size < 2)
358: return -1;
359: if (*fmt != '%')
360: *str++ = '%';
361: if (*fmt)
362: *str++ = *fmt;
363: else
364: --fmt;
365: continue;
366: }
367: if (qualifier == 'l')
368: num = va_arg(args, unsigned long);
369: else if (qualifier == 'h') {
370: num = (unsigned short) va_arg(args, int);
371: if (flags & SIGN)
372: num = (short) num;
373: } else if (flags & SIGN)
374: num = va_arg(args, int);
375: else
376: num = va_arg(args, uint);
377: str = number(str, num, base, field_width, precision, flags, size);
378: if (!str)
379: return -1;
380: }
381: if (!size)
382: return -1;
383: *str = '\0';
384: return str-buf;
385: }
386:
387: /**
388: * bvsprintf - BIRD's vsprintf()
389: * @buf: buffer
390: * @fmt: format string
391: * @args: a list of arguments to be formatted
392: *
393: * This function is equivalent to bvsnprintf() with an infinite
394: * buffer size. Please use carefully only when you are absolutely
395: * sure the buffer won't overflow.
396: */
397: int bvsprintf(char *buf, const char *fmt, va_list args)
398: {
399: return bvsnprintf(buf, 1000000000, fmt, args);
400: }
401:
402: /**
403: * bsprintf - BIRD's sprintf()
404: * @buf: buffer
405: * @fmt: format string
406: *
407: * This function is equivalent to bvsnprintf() with an infinite
408: * buffer size and variable arguments instead of a &va_list.
409: * Please use carefully only when you are absolutely
410: * sure the buffer won't overflow.
411: */
412: int bsprintf(char * buf, const char *fmt, ...)
413: {
414: va_list args;
415: int i;
416:
417: va_start(args, fmt);
418: i=bvsnprintf(buf, 1000000000, fmt, args);
419: va_end(args);
420: return i;
421: }
422:
423: /**
424: * bsnprintf - BIRD's snprintf()
425: * @buf: buffer
426: * @size: buffer size
427: * @fmt: format string
428: *
429: * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list.
430: */
431: int bsnprintf(char * buf, int size, const char *fmt, ...)
432: {
433: va_list args;
434: int i;
435:
436: va_start(args, fmt);
437: i=bvsnprintf(buf, size, fmt, args);
438: va_end(args);
439: return i;
440: }
441:
442: int
443: buffer_vprint(buffer *buf, const char *fmt, va_list args)
444: {
445: int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
446: buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
447: return i;
448: }
449:
450: int
451: buffer_print(buffer *buf, const char *fmt, ...)
452: {
453: va_list args;
454: int i;
455:
456: va_start(args, fmt);
457: i=bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
458: va_end(args);
459:
460: buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
461: return i;
462: }
463:
464: void
465: buffer_puts(buffer *buf, const char *str)
466: {
467: byte *bp = buf->pos;
468: byte *be = buf->end;
469:
470: while (bp < be && *str)
471: *bp++ = *str++;
472:
473: if (bp < be)
474: *bp = 0;
475:
476: buf->pos = bp;
477: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>