Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (6.54 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
/* we use this so that we can do without the ctype library */
16
#define is_digit(c)        ((c) >= '0' && (c) <= '9')
17

    
18
static int skip_atoi(const char **s)
19
{
20
        int i=0;
21

    
22
        while (is_digit(**s))
23
                i = i*10 + *((*s)++) - '0';
24
        return i;
25
}
26

    
27
#define ZEROPAD        1                /* pad with zero */
28
#define SIGN        2                /* unsigned/signed long */
29
#define PLUS        4                /* show plus */
30
#define SPACE        8                /* space if plus */
31
#define LEFT        16                /* left justified */
32
#define SPECIAL        32                /* 0x */
33
#define LARGE        64                /* use 'ABCDEF' instead of 'abcdef' */
34

    
35
#define do_div(n,base) ({ \
36
int __res; \
37
__res = ((unsigned long) n) % (unsigned) base; \
38
n = ((unsigned long) n) / (unsigned) base; \
39
__res; })
40

    
41
static char * number(char * str, long num, int base, int size, int precision,
42
        int type, int remains)
43
{
44
        char c,sign,tmp[66];
45
        const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
46
        int i;
47

    
48
        if (size >= 0 && (remains -= size) < 0)
49
                return NULL;
50
        if (type & LARGE)
51
                digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
52
        if (type & LEFT)
53
                type &= ~ZEROPAD;
54
        if (base < 2 || base > 36)
55
                return 0;
56
        c = (type & ZEROPAD) ? '0' : ' ';
57
        sign = 0;
58
        if (type & SIGN) {
59
                if (num < 0) {
60
                        sign = '-';
61
                        num = -num;
62
                        size--;
63
                } else if (type & PLUS) {
64
                        sign = '+';
65
                        size--;
66
                } else if (type & SPACE) {
67
                        sign = ' ';
68
                        size--;
69
                }
70
        }
71
        if (type & SPECIAL) {
72
                if (base == 16)
73
                        size -= 2;
74
                else if (base == 8)
75
                        size--;
76
        }
77
        i = 0;
78
        if (num == 0)
79
                tmp[i++]='0';
80
        else while (num != 0)
81
                tmp[i++] = digits[do_div(num,base)];
82
        if (i > precision)
83
                precision = i;
84
        size -= precision;
85
        if (size < 0 && -size > remains)
86
                return NULL;
87
        if (!(type&(ZEROPAD+LEFT)))
88
                while(size-->0)
89
                        *str++ = ' ';
90
        if (sign)
91
                *str++ = sign;
92
        if (type & SPECIAL) {
93
                if (base==8)
94
                        *str++ = '0';
95
                else if (base==16) {
96
                        *str++ = '0';
97
                        *str++ = digits[33];
98
                }
99
        }
100
        if (!(type & LEFT))
101
                while (size-- > 0)
102
                        *str++ = c;
103
        while (i < precision--)
104
                *str++ = '0';
105
        while (i-- > 0)
106
                *str++ = tmp[i];
107
        while (size-- > 0)
108
                *str++ = ' ';
109
        return str;
110
}
111

    
112
int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
113
{
114
        int len;
115
        unsigned long num;
116
        int i, base;
117
        char *str, *start;
118
        const char *s;
119

    
120
        int flags;                /* flags to number() */
121

    
122
        int field_width;        /* width of output field */
123
        int precision;                /* min. # of digits for integers; max
124
                                   number of chars for from string */
125
        int qualifier;                /* 'h', 'l', or 'L' for integer fields */
126

    
127
        for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
128
                if (*fmt != '%') {
129
                        if (!size)
130
                                return -1;
131
                        *str++ = *fmt;
132
                        continue;
133
                }
134
                        
135
                /* process flags */
136
                flags = 0;
137
                repeat:
138
                        ++fmt;                /* this also skips first '%' */
139
                        switch (*fmt) {
140
                                case '-': flags |= LEFT; goto repeat;
141
                                case '+': flags |= PLUS; goto repeat;
142
                                case ' ': flags |= SPACE; goto repeat;
143
                                case '#': flags |= SPECIAL; goto repeat;
144
                                case '0': flags |= ZEROPAD; goto repeat;
145
                                }
146
                
147
                /* get field width */
148
                field_width = -1;
149
                if (is_digit(*fmt))
150
                        field_width = skip_atoi(&fmt);
151
                else if (*fmt == '*') {
152
                        ++fmt;
153
                        /* it's the next argument */
154
                        field_width = va_arg(args, int);
155
                        if (field_width < 0) {
156
                                field_width = -field_width;
157
                                flags |= LEFT;
158
                        }
159
                }
160

    
161
                /* get the precision */
162
                precision = -1;
163
                if (*fmt == '.') {
164
                        ++fmt;        
165
                        if (is_digit(*fmt))
166
                                precision = skip_atoi(&fmt);
167
                        else if (*fmt == '*') {
168
                                ++fmt;
169
                                /* it's the next argument */
170
                                precision = va_arg(args, int);
171
                        }
172
                        if (precision < 0)
173
                                precision = 0;
174
                }
175

    
176
                /* get the conversion qualifier */
177
                qualifier = -1;
178
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
179
                        qualifier = *fmt;
180
                        ++fmt;
181
                }
182

    
183
                /* default base */
184
                base = 10;
185

    
186
                if (field_width > size)
187
                        return -1;
188
                switch (*fmt) {
189
                case 'c':
190
                        if (!(flags & LEFT))
191
                                while (--field_width > 0)
192
                                        *str++ = ' ';
193
                        *str++ = (unsigned char) va_arg(args, int);
194
                        while (--field_width > 0)
195
                                *str++ = ' ';
196
                        continue;
197

    
198
                case 'm':
199
                        s = strerror(errno);
200
                        goto str;
201
                case 'M':
202
                        s = strerror(va_arg(args, int));
203
                        goto str;
204
                case 's':
205
                        s = va_arg(args, char *);
206
                        if (!s)
207
                                s = "<NULL>";
208

    
209
                str:
210
                        len = strlen(s);
211
                        if (precision >= 0 && len > precision)
212
                                len = precision;
213
                        if (len > size)
214
                                return -1;
215

    
216
                        if (!(flags & LEFT))
217
                                while (len < field_width--)
218
                                        *str++ = ' ';
219
                        for (i = 0; i < len; ++i)
220
                                *str++ = *s++;
221
                        while (len < field_width--)
222
                                *str++ = ' ';
223
                        continue;
224

    
225
                case 'p':
226
                        if (field_width == -1) {
227
                                field_width = 2*sizeof(void *);
228
                                flags |= ZEROPAD;
229
                        }
230
                        str = number(str,
231
                                (unsigned long) va_arg(args, void *), 16,
232
                                field_width, precision, flags, size);
233
                        if (!str)
234
                                return -1;
235
                        continue;
236

    
237

    
238
                case 'n':
239
                        if (qualifier == 'l') {
240
                                long * ip = va_arg(args, long *);
241
                                *ip = (str - buf);
242
                        } else {
243
                                int * ip = va_arg(args, int *);
244
                                *ip = (str - buf);
245
                        }
246
                        continue;
247

    
248
                /* IP address */
249
                case 'I':
250
                        if (size < STD_ADDRESS_P_LENGTH)
251
                                return -1;
252
                        if (flags & SPECIAL)
253
                                str = ip_ntox(va_arg(args, ip_addr), str);
254
                        else {
255
                                len = ip_ntop(va_arg(args, ip_addr), str) - str;
256
                                str += len;
257
                                if (field_width >= 0)
258
                                        while (len++ < STD_ADDRESS_P_LENGTH)
259
                                                *str++ = ' ';
260
                        }
261
                        continue;
262

    
263
                /* integer number formats - set up the flags and "break" */
264
                case 'o':
265
                        base = 8;
266
                        break;
267

    
268
                case 'X':
269
                        flags |= LARGE;
270
                case 'x':
271
                        base = 16;
272
                        break;
273

    
274
                case 'd':
275
                case 'i':
276
                        flags |= SIGN;
277
                case 'u':
278
                        break;
279

    
280
                default:
281
                        if (size < 2)
282
                                return -1;
283
                        if (*fmt != '%')
284
                                *str++ = '%';
285
                        if (*fmt)
286
                                *str++ = *fmt;
287
                        else
288
                                --fmt;
289
                        continue;
290
                }
291
                if (qualifier == 'l')
292
                        num = va_arg(args, unsigned long);
293
                else if (qualifier == 'h') {
294
                        num = (unsigned short) va_arg(args, int);
295
                        if (flags & SIGN)
296
                                num = (short) num;
297
                } else if (flags & SIGN)
298
                        num = va_arg(args, int);
299
                else
300
                        num = va_arg(args, unsigned int);
301
                str = number(str, num, base, field_width, precision, flags, size);
302
                if (!str)
303
                        return -1;
304
        }
305
        if (!size)
306
                return -1;
307
        *str = '\0';
308
        return str-buf;
309
}
310

    
311
int bvsprintf(char *buf, const char *fmt, va_list args)
312
{
313
  return bvsnprintf(buf, 1000000000, fmt, args);
314
}
315

    
316
int bsprintf(char * buf, const char *fmt, ...)
317
{
318
  va_list args;
319
  int i;
320

    
321
  va_start(args, fmt);
322
  i=bvsnprintf(buf, 1000000000, fmt, args);
323
  va_end(args);
324
  return i;
325
}
326

    
327
int bsnprintf(char * buf, int size, const char *fmt, ...)
328
{
329
  va_list args;
330
  int i;
331

    
332
  va_start(args, fmt);
333
  i=bvsnprintf(buf, size, fmt, args);
334
  va_end(args);
335
  return i;
336
}