Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / printf.c @ 6b3f1a54

History | View | Annotate | Download (11.6 KB)

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 available
122
 * space to avoid buffer overflows and it allows some more format specifiers:
123
 * |%I| for formatting of IP addresses (width of 1 is automatically replaced by
124
 * standard IP address width which depends on whether we use IPv4 or IPv6; |%I4|
125
 * or |%I6| can be used for explicit ip4_addr / ip6_addr arguments, |%N| for
126
 * generic network addresses (net_addr *), |%R| for Router / Network ID (u32
127
 * value printed as IPv4 address), |%lR| for 64bit Router / Network ID (u64
128
 * value printed as eight :-separated octets), |%t| for time values (btime) with
129
 * specified subsecond precision, and |%m| resp. |%M| for error messages (uses
130
 * strerror() to translate @errno code to message text). On the other hand, it
131
 * doesn't support floating point numbers.
132
 *
133
 * Result: number of characters of the output string or -1 if
134
 * the buffer space was insufficient.
135
 */
136
int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
137
{
138
        int len;
139
        unsigned long num;
140
        int i, base;
141
        u32 x;
142
        u64 X;
143
        btime t;
144
        s64 t1, t2;
145
        char *str, *start;
146
        const char *s;
147
        char ipbuf[NET_MAX_TEXT_LENGTH+1];
148
        struct iface *iface;
149

    
150
        int flags;                /* flags to number() */
151

    
152
        int field_width;        /* width of output field */
153
        int precision;                /* min. # of digits for integers; max
154
                                   number of chars for from string */
155
        int qualifier;                /* 'h', 'l', or 'L' for integer fields */
156

    
157
        for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
158
                if (*fmt != '%') {
159
                        if (!size)
160
                                return -1;
161
                        *str++ = *fmt;
162
                        continue;
163
                }
164

    
165
                /* process flags */
166
                flags = 0;
167
                repeat:
168
                        ++fmt;                /* this also skips first '%' */
169
                        switch (*fmt) {
170
                                case '-': flags |= LEFT; goto repeat;
171
                                case '+': flags |= PLUS; goto repeat;
172
                                case ' ': flags |= SPACE; goto repeat;
173
                                case '#': flags |= SPECIAL; goto repeat;
174
                                case '0': flags |= ZEROPAD; goto repeat;
175
                        }
176

    
177
                /* get field width */
178
                field_width = -1;
179
                if (is_digit(*fmt))
180
                        field_width = skip_atoi(&fmt);
181
                else if (*fmt == '*') {
182
                        ++fmt;
183
                        /* it's the next argument */
184
                        field_width = va_arg(args, int);
185
                        if (field_width < 0) {
186
                                field_width = -field_width;
187
                                flags |= LEFT;
188
                        }
189
                }
190

    
191
                /* get the precision */
192
                precision = -1;
193
                if (*fmt == '.') {
194
                        ++fmt;
195
                        if (is_digit(*fmt))
196
                                precision = skip_atoi(&fmt);
197
                        else if (*fmt == '*') {
198
                                ++fmt;
199
                                /* it's the next argument */
200
                                precision = va_arg(args, int);
201
                        }
202
                        if (precision < 0)
203
                                precision = 0;
204
                }
205

    
206
                /* get the conversion qualifier */
207
                qualifier = -1;
208
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
209
                        qualifier = *fmt;
210
                        ++fmt;
211
                }
212

    
213
                /* default base */
214
                base = 10;
215

    
216
                if (field_width > size)
217
                        return -1;
218
                switch (*fmt) {
219
                case 'c':
220
                        if (!(flags & LEFT))
221
                                while (--field_width > 0)
222
                                        *str++ = ' ';
223
                        *str++ = (byte) va_arg(args, int);
224
                        while (--field_width > 0)
225
                                *str++ = ' ';
226
                        continue;
227

    
228
                case 'm':
229
                        if (flags & SPECIAL) {
230
                                if (!errno)
231
                                        continue;
232
                                if (size < 2)
233
                                        return -1;
234
                                *str++ = ':';
235
                                *str++ = ' ';
236
                                start += 2;
237
                                size -= 2;
238
                        }
239
                        s = strerror(errno);
240
                        goto str;
241
                case 'M':
242
                        s = strerror(va_arg(args, int));
243
                        goto str;
244
                case 'N': {
245
                        net_addr *n = va_arg(args, net_addr *);
246
                        if (field_width == 1)
247
                                field_width = net_max_text_length[n->type];
248
                        net_format(n, ipbuf, sizeof(ipbuf));
249
                        s = ipbuf;
250
                        goto str;
251
                        }
252
                case 's':
253
                        s = va_arg(args, char *);
254
                        if (!s)
255
                                s = "<NULL>";
256

    
257
                str:
258
                        len = strlen(s);
259
                        if (precision >= 0 && len > precision)
260
                                len = precision;
261
                        if (len > size)
262
                                return -1;
263

    
264
                        if (!(flags & LEFT))
265
                                while (len < field_width--)
266
                                        *str++ = ' ';
267
                        for (i = 0; i < len; ++i)
268
                                *str++ = *s++;
269
                        while (len < field_width--)
270
                                *str++ = ' ';
271
                        continue;
272

    
273
                case 'p':
274
                        if (field_width == -1) {
275
                                field_width = 2*sizeof(void *);
276
                                flags |= ZEROPAD;
277
                        }
278
                        str = number(str,
279
                                (unsigned long) va_arg(args, void *), 16,
280
                                field_width, precision, flags, size);
281
                        if (!str)
282
                                return -1;
283
                        continue;
284

    
285
                case 'n':
286
                        if (qualifier == 'l') {
287
                                long * ip = va_arg(args, long *);
288
                                *ip = (str - buf);
289
                        } else {
290
                                int * ip = va_arg(args, int *);
291
                                *ip = (str - buf);
292
                        }
293
                        continue;
294

    
295
                /* IP address */
296
                case 'I':
297
                        if (fmt[1] == '4') {
298
                                /* Explicit IPv4 address */
299
                                ip4_addr a = va_arg(args, ip4_addr);
300
                                ip4_ntop(a, ipbuf);
301
                                i = IP4_MAX_TEXT_LENGTH;
302
                                fmt++;
303
                        } else if (fmt[1] == '6') {
304
                                /* Explicit IPv6 address */
305
                                ip6_addr a = va_arg(args, ip6_addr);
306
                                ip6_ntop(a, ipbuf);
307
                                i = IP6_MAX_TEXT_LENGTH;
308
                                fmt++;
309
                        } else {
310
                                /* Just IP address */
311
                                ip_addr a = va_arg(args, ip_addr);
312

    
313
                                if (ipa_is_ip4(a)) {
314
                                        ip4_ntop(ipa_to_ip4(a), ipbuf);
315
                                        i = IP4_MAX_TEXT_LENGTH;
316
                                } else {
317
                                        ip6_ntop(ipa_to_ip6(a), ipbuf);
318
                                        i = IP6_MAX_TEXT_LENGTH;
319
                                }
320
                        }
321

    
322
                        s = ipbuf;
323
                        if (field_width == 1)
324
                                field_width = i;
325

    
326
                        goto str;
327

    
328
                /* Interface scope after link-local IP address */
329
                case 'J':
330
                        iface = va_arg(args, struct iface *);
331
                        if (!iface)
332
                                continue;
333
                        if (!size)
334
                                return -1;
335

    
336
                        *str++ = '%';
337
                        start++;
338
                        size--;
339

    
340
                        s = iface->name;
341
                        goto str;
342

    
343
                /* Router/Network ID - essentially IPv4 address in u32 value */
344
                case 'R':
345
                        if (qualifier == 'l') {
346
                                X = va_arg(args, u64);
347
                                bsprintf(ipbuf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
348
                                        ((X >> 56) & 0xff),
349
                                        ((X >> 48) & 0xff),
350
                                        ((X >> 40) & 0xff),
351
                                        ((X >> 32) & 0xff),
352
                                        ((X >> 24) & 0xff),
353
                                        ((X >> 16) & 0xff),
354
                                        ((X >> 8) & 0xff),
355
                                        (X & 0xff));
356
                        }
357
                        else
358
                        {
359
                                x = va_arg(args, u32);
360
                                ip4_ntop(ip4_from_u32(x), ipbuf);
361
                        }
362
                        s = ipbuf;
363
                        goto str;
364

    
365
                case 't':
366
                        t = va_arg(args, btime);
367
                        t1 = t TO_S;
368
                        t2 = t - t1 S;
369

    
370
                        if (precision < 0)
371
                          precision = 3;
372

    
373
                        if (precision > 6)
374
                          precision = 6;
375

    
376
                        /* Compute field_width for second part */
377
                        if ((precision > 0) && (field_width > 0))
378
                          field_width -= (1 + precision);
379

    
380
                        if (field_width < 0)
381
                          field_width = 0;
382

    
383
                        /* Print seconds */
384
                        flags |= SIGN;
385
                        str = number(str, t1, 10, field_width, 0, flags, size);
386
                        if (!str)
387
                          return -1;
388

    
389
                        if (precision > 0)
390
                        {
391
                          size -= (str-start);
392
                          start = str;
393

    
394
                          if ((1 + precision) > size)
395
                            return -1;
396

    
397
                          /* Convert microseconds to requested precision */
398
                          for (i = precision; i < 6; i++)
399
                            t2 /= 10;
400

    
401
                          /* Print sub-seconds */
402
                          *str++ = '.';
403
                          str = number(str, t2, 10, precision, 0, ZEROPAD, size - 1);
404
                          if (!str)
405
                            return -1;
406
                        }
407
                        goto done;
408

    
409
                /* integer number formats - set up the flags and "break" */
410
                case 'o':
411
                        base = 8;
412
                        break;
413

    
414
                case 'X':
415
                        flags |= LARGE;
416
                case 'x':
417
                        base = 16;
418
                        break;
419

    
420
                case 'd':
421
                case 'i':
422
                        flags |= SIGN;
423
                case 'u':
424
                        break;
425

    
426
                default:
427
                        if (size < 2)
428
                                return -1;
429
                        if (*fmt != '%')
430
                                *str++ = '%';
431
                        if (*fmt)
432
                                *str++ = *fmt;
433
                        else
434
                                --fmt;
435
                        continue;
436
                }
437
                if (qualifier == 'l')
438
                        num = va_arg(args, unsigned long);
439
                else if (qualifier == 'h') {
440
                        num = (unsigned short) va_arg(args, int);
441
                        if (flags & SIGN)
442
                                num = (short) num;
443
                } else if (flags & SIGN)
444
                        num = va_arg(args, int);
445
                else
446
                        num = va_arg(args, uint);
447
                str = number(str, num, base, field_width, precision, flags, size);
448
                if (!str)
449
                        return -1;
450
        done:        ;
451
        }
452
        if (!size)
453
                return -1;
454
        *str = '\0';
455
        return str-buf;
456
}
457

    
458
/**
459
 * bvsprintf - BIRD's vsprintf()
460
 * @buf: buffer
461
 * @fmt: format string
462
 * @args: a list of arguments to be formatted
463
 *
464
 * This function is equivalent to bvsnprintf() with an infinite
465
 * buffer size. Please use carefully only when you are absolutely
466
 * sure the buffer won't overflow.
467
 */
468
int bvsprintf(char *buf, const char *fmt, va_list args)
469
{
470
  return bvsnprintf(buf, 1000000000, fmt, args);
471
}
472

    
473
/**
474
 * bsprintf - BIRD's sprintf()
475
 * @buf: buffer
476
 * @fmt: format string
477
 *
478
 * This function is equivalent to bvsnprintf() with an infinite
479
 * buffer size and variable arguments instead of a &va_list.
480
 * Please use carefully only when you are absolutely
481
 * sure the buffer won't overflow.
482
 */
483
int bsprintf(char * buf, const char *fmt, ...)
484
{
485
  va_list args;
486
  int i;
487

    
488
  va_start(args, fmt);
489
  i=bvsnprintf(buf, 1000000000, fmt, args);
490
  va_end(args);
491
  return i;
492
}
493

    
494
/**
495
 * bsnprintf - BIRD's snprintf()
496
 * @buf: buffer
497
 * @size: buffer size
498
 * @fmt: format string
499
 *
500
 * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list.
501
 */
502
int bsnprintf(char * buf, int size, const char *fmt, ...)
503
{
504
  va_list args;
505
  int i;
506

    
507
  va_start(args, fmt);
508
  i=bvsnprintf(buf, size, fmt, args);
509
  va_end(args);
510
  return i;
511
}
512

    
513
int
514
buffer_vprint(buffer *buf, const char *fmt, va_list args)
515
{
516
  int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
517

    
518
  if ((i < 0) && (buf->pos < buf->end))
519
    *buf->pos = 0;
520

    
521
  buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
522
  return i;
523
}
524

    
525
int
526
buffer_print(buffer *buf, const char *fmt, ...)
527
{
528
  va_list args;
529
  int i;
530

    
531
  va_start(args, fmt);
532
  i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
533
  va_end(args);
534

    
535
  if ((i < 0) && (buf->pos < buf->end))
536
    *buf->pos = 0;
537

    
538
  buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
539
  return i;
540
}
541

    
542
void
543
buffer_puts(buffer *buf, const char *str)
544
{
545
  byte *bp = buf->pos;
546
  byte *be = buf->end - 1;
547

    
548
  while (bp < be && *str)
549
    *bp++ = *str++;
550

    
551
  if (bp <= be)
552
    *bp = 0;
553

    
554
  buf->pos = (bp < be) ? bp : buf->end;
555
}