Statistics
| Branch: | Revision:

iof-bird-daemon / lib / printf.c @ fe9f1a6d

History | View | Annotate | Download (9.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
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
 * and |%m| resp. |%M| for error messages (uses strerror() to translate @errno code to
128
 * message text). On the other hand, it doesn't support floating
129
 * point numbers.
130
 *
131
 * Result: number of characters of the output string or -1 if
132
 * the buffer space was insufficient.
133
 */
134
int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
135
{
136
        int len;
137
        unsigned long num;
138
        int i, base;
139
        u32 x;
140
        char *str, *start;
141
        const char *s;
142
        char ipbuf[STD_ADDRESS_P_LENGTH+1];
143
        struct iface *iface;
144

    
145
        int flags;                /* flags to number() */
146

    
147
        int field_width;        /* width of output field */
148
        int precision;                /* min. # of digits for integers; max
149
                                   number of chars for from string */
150
        int qualifier;                /* 'h', 'l', or 'L' for integer fields */
151

    
152
        for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
153
                if (*fmt != '%') {
154
                        if (!size)
155
                                return -1;
156
                        *str++ = *fmt;
157
                        continue;
158
                }
159
                        
160
                /* process flags */
161
                flags = 0;
162
                repeat:
163
                        ++fmt;                /* this also skips first '%' */
164
                        switch (*fmt) {
165
                                case '-': flags |= LEFT; goto repeat;
166
                                case '+': flags |= PLUS; goto repeat;
167
                                case ' ': flags |= SPACE; goto repeat;
168
                                case '#': flags |= SPECIAL; goto repeat;
169
                                case '0': flags |= ZEROPAD; goto repeat;
170
                                }
171
                
172
                /* get field width */
173
                field_width = -1;
174
                if (is_digit(*fmt))
175
                        field_width = skip_atoi(&fmt);
176
                else if (*fmt == '*') {
177
                        ++fmt;
178
                        /* it's the next argument */
179
                        field_width = va_arg(args, int);
180
                        if (field_width < 0) {
181
                                field_width = -field_width;
182
                                flags |= LEFT;
183
                        }
184
                }
185

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

    
201
                /* get the conversion qualifier */
202
                qualifier = -1;
203
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
204
                        qualifier = *fmt;
205
                        ++fmt;
206
                }
207

    
208
                /* default base */
209
                base = 10;
210

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

    
223
                case 'm':
224
                        if (flags & SPECIAL) {
225
                                if (!errno)
226
                                        continue;
227
                                if (size < 2)
228
                                        return -1;
229
                                *str++ = ':';
230
                                *str++ = ' ';
231
                                start += 2;
232
                                size -= 2;
233
                        }
234
                        s = strerror(errno);
235
                        goto str;
236
                case 'M':
237
                        s = strerror(va_arg(args, int));
238
                        goto str;
239
                case 'N':
240
                        if (field_width == 1)
241
                                field_width = STD_ADDRESS_P_LENGTH; /* XXXX */
242
                        net_format(va_arg(args, net_addr *), ipbuf, sizeof(ipbuf));
243
                        s = ipbuf;
244
                        goto str;
245
                case 's':
246
                        s = va_arg(args, char *);
247
                        if (!s)
248
                                s = "<NULL>";
249

    
250
                str:
251
                        len = strlen(s);
252
                        if (precision >= 0 && len > precision)
253
                                len = precision;
254
                        if (len > size)
255
                                return -1;
256

    
257
                        if (!(flags & LEFT))
258
                                while (len < field_width--)
259
                                        *str++ = ' ';
260
                        for (i = 0; i < len; ++i)
261
                                *str++ = *s++;
262
                        while (len < field_width--)
263
                                *str++ = ' ';
264
                        continue;
265

    
266
                case 'p':
267
                        if (field_width == -1) {
268
                                field_width = 2*sizeof(void *);
269
                                flags |= ZEROPAD;
270
                        }
271
                        str = number(str,
272
                                (unsigned long) va_arg(args, void *), 16,
273
                                field_width, precision, flags, size);
274
                        if (!str)
275
                                return -1;
276
                        continue;
277

    
278

    
279
                case 'n':
280
                        if (qualifier == 'l') {
281
                                long * ip = va_arg(args, long *);
282
                                *ip = (str - buf);
283
                        } else {
284
                                int * ip = va_arg(args, int *);
285
                                *ip = (str - buf);
286
                        }
287
                        continue;
288

    
289
                /* IP address */
290
                case 'I':
291
                        if (flags & SPECIAL)
292
                                ipa_ntox(va_arg(args, ip_addr), ipbuf);
293
                        else {
294
                                ipa_ntop(va_arg(args, ip_addr), ipbuf);
295
                                if (field_width == 1)
296
                                        field_width = STD_ADDRESS_P_LENGTH;
297
                        }
298
                        s = ipbuf;
299
                        goto str;
300

    
301
                /* Interface scope after link-local IP address */
302
                case 'J':
303
                        iface = va_arg(args, struct iface *);
304
                        if (!iface)
305
                                continue;
306
                        if (!size)
307
                                return -1;
308

    
309
                        *str++ = '%';
310
                        start++;
311
                        size--;
312

    
313
                        s = iface->name;
314
                        goto str;
315

    
316
                /* Router/Network ID - essentially IPv4 address in u32 value */
317
                case 'R':
318
                        x = va_arg(args, u32);
319
                        bsprintf(ipbuf, "%d.%d.%d.%d",
320
                                 ((x >> 24) & 0xff),
321
                                 ((x >> 16) & 0xff),
322
                                 ((x >> 8) & 0xff),
323
                                 (x & 0xff));
324
                        s = ipbuf;
325
                        goto str;
326

    
327
                /* integer number formats - set up the flags and "break" */
328
                case 'o':
329
                        base = 8;
330
                        break;
331

    
332
                case 'X':
333
                        flags |= LARGE;
334
                case 'x':
335
                        base = 16;
336
                        break;
337

    
338
                case 'd':
339
                case 'i':
340
                        flags |= SIGN;
341
                case 'u':
342
                        break;
343

    
344
                default:
345
                        if (size < 2)
346
                                return -1;
347
                        if (*fmt != '%')
348
                                *str++ = '%';
349
                        if (*fmt)
350
                                *str++ = *fmt;
351
                        else
352
                                --fmt;
353
                        continue;
354
                }
355
                if (qualifier == 'l')
356
                        num = va_arg(args, unsigned long);
357
                else if (qualifier == 'h') {
358
                        num = (unsigned short) va_arg(args, int);
359
                        if (flags & SIGN)
360
                                num = (short) num;
361
                } else if (flags & SIGN)
362
                        num = va_arg(args, int);
363
                else
364
                        num = va_arg(args, uint);
365
                str = number(str, num, base, field_width, precision, flags, size);
366
                if (!str)
367
                        return -1;
368
        }
369
        if (!size)
370
                return -1;
371
        *str = '\0';
372
        return str-buf;
373
}
374

    
375
/**
376
 * bvsprintf - BIRD's vsprintf()
377
 * @buf: buffer
378
 * @fmt: format string
379
 * @args: a list of arguments to be formatted
380
 *
381
 * This function is equivalent to bvsnprintf() with an infinite
382
 * buffer size. Please use carefully only when you are absolutely
383
 * sure the buffer won't overflow.
384
 */
385
int bvsprintf(char *buf, const char *fmt, va_list args)
386
{
387
  return bvsnprintf(buf, 1000000000, fmt, args);
388
}
389

    
390
/**
391
 * bsprintf - BIRD's sprintf()
392
 * @buf: buffer
393
 * @fmt: format string
394
 *
395
 * This function is equivalent to bvsnprintf() with an infinite
396
 * buffer size and variable arguments instead of a &va_list.
397
 * Please use carefully only when you are absolutely
398
 * sure the buffer won't overflow.
399
 */
400
int bsprintf(char * buf, const char *fmt, ...)
401
{
402
  va_list args;
403
  int i;
404

    
405
  va_start(args, fmt);
406
  i=bvsnprintf(buf, 1000000000, fmt, args);
407
  va_end(args);
408
  return i;
409
}
410

    
411
/**
412
 * bsnprintf - BIRD's snprintf()
413
 * @buf: buffer
414
 * @size: buffer size
415
 * @fmt: format string
416
 *
417
 * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list.
418
 */
419
int bsnprintf(char * buf, int size, const char *fmt, ...)
420
{
421
  va_list args;
422
  int i;
423

    
424
  va_start(args, fmt);
425
  i=bvsnprintf(buf, size, fmt, args);
426
  va_end(args);
427
  return i;
428
}
429

    
430
int
431
buffer_vprint(buffer *buf, const char *fmt, va_list args)
432
{
433
  int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
434
  buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
435
  return i;
436
}
437

    
438
int
439
buffer_print(buffer *buf, const char *fmt, ...)
440
{
441
  va_list args;
442
  int i;
443

    
444
  va_start(args, fmt);
445
  i=bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
446
  va_end(args);
447

    
448
  buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
449
  return i;
450
}
451

    
452
void
453
buffer_puts(buffer *buf, const char *str)
454
{
455
  byte *bp = buf->pos;
456
  byte *be = buf->end;
457

    
458
  while (bp < be && *str)
459
    *bp++ = *str++;
460

    
461
  if (bp < be)
462
    *bp = 0;
463

    
464
  buf->pos = bp;
465
}