Subversion Repositories HelenOS-historic

Rev

Rev 1073 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1073 Rev 1173
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * Copyright (C) 2006 Josef Cejka
3
 * Copyright (C) 2006 Josef Cejka
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
#include <stdio.h>
30
#include <stdio.h>
31
#include <unistd.h>
31
#include <unistd.h>
32
#include <io/io.h>
32
#include <io/io.h>
33
#include <stdarg.h>
33
#include <stdarg.h>
-
 
34
#include <ctype.h>
-
 
35
#include <string.h>
34
 
36
 
35
#define __PRINTF_FLAG_PREFIX        0x00000001  /* show prefixes 0x or 0*/
37
#define __PRINTF_FLAG_PREFIX        0x00000001  /* show prefixes 0x or 0*/
36
#define __PRINTF_FLAG_SIGNED        0x00000002  /* signed / unsigned number */
38
#define __PRINTF_FLAG_SIGNED        0x00000002  /* signed / unsigned number */
37
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /* print leading zeroes */
39
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /* print leading zeroes */
38
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /* align to left */
40
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /* align to left */
39
#define __PRINTF_FLAG_SHOWPLUS      0x00000020  /* always show + sign */
41
#define __PRINTF_FLAG_SHOWPLUS      0x00000020  /* always show + sign */
40
#define __PRINTF_FLAG_SPACESIGN     0x00000040  /* print space instead of plus */
42
#define __PRINTF_FLAG_SPACESIGN     0x00000040  /* print space instead of plus */
41
#define __PRINTF_FLAG_BIGCHARS      0x00000080  /* show big characters */
43
#define __PRINTF_FLAG_BIGCHARS      0x00000080  /* show big characters */
42
#define __PRINTF_FLAG_NEGATIVE      0x00000100  /* number has - sign */
44
#define __PRINTF_FLAG_NEGATIVE      0x00000100  /* number has - sign */
43
 
45
 
44
#define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /* Buffer big enought for 64 bit number
46
#define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /* Buffer big enought for 64 bit number
45
                             * printed in base 2, sign, prefix and
47
                             * printed in base 2, sign, prefix and
46
                             * 0 to terminate string.. (last one is only for better testing
48
                             * 0 to terminate string.. (last one is only for better testing
47
                             * end of buffer by zero-filling subroutine)
49
                             * end of buffer by zero-filling subroutine)
48
                             */
50
                             */
49
typedef enum {
51
typedef enum {
50
    PrintfQualifierByte = 0,
52
    PrintfQualifierByte = 0,
51
    PrintfQualifierShort,
53
    PrintfQualifierShort,
52
    PrintfQualifierInt,
54
    PrintfQualifierInt,
53
    PrintfQualifierLong,
55
    PrintfQualifierLong,
54
    PrintfQualifierLongLong,
56
    PrintfQualifierLongLong,
55
    PrintfQualifierSizeT,
57
    PrintfQualifierSizeT,
56
    PrintfQualifierPointer
58
    PrintfQualifierPointer
57
} qualifier_t;
59
} qualifier_t;
58
 
60
 
59
static char digits_small[] = "0123456789abcdef";    /* Small hexadecimal characters */
61
static char digits_small[] = "0123456789abcdef";    /* Small hexadecimal characters */
60
static char digits_big[] = "0123456789ABCDEF";  /* Big hexadecimal characters */
62
static char digits_big[] = "0123456789ABCDEF";  /* Big hexadecimal characters */
61
 
63
 
-
 
64
 
-
 
65
/** Print one formatted character
-
 
66
 * @param c character to print
-
 
67
 * @param width
-
 
68
 * @param flags
-
 
69
 * @return number of printed characters or EOF
-
 
70
 */
-
 
71
                       
-
 
72
static int print_char(char c, int width, uint64_t flags)
-
 
73
{
-
 
74
    int counter = 0;
-
 
75
    char space = ' ';
-
 
76
   
-
 
77
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
78
        while (--width > 0) {   /* one space is consumed by character itself hence predecrement */
-
 
79
            /* FIXME: painful slow */
-
 
80
            putchar(' ');  
-
 
81
            ++counter;
-
 
82
        }
-
 
83
    }
-
 
84
   
-
 
85
    if (putchar(c) == EOF) {
-
 
86
        return EOF;
-
 
87
    }
-
 
88
 
-
 
89
    while (--width > 0) { /* one space is consumed by character itself hence predecrement */
-
 
90
        putchar(' ');
-
 
91
        ++counter;
-
 
92
    }
-
 
93
   
-
 
94
    return ++counter;
-
 
95
}
-
 
96
 
-
 
97
/** Print one string
-
 
98
 * @param s string
-
 
99
 * @param width
-
 
100
 * @param precision
-
 
101
 * @param flags
-
 
102
 * @return number of printed characters or EOF
-
 
103
 */
-
 
104
                       
-
 
105
static int print_string(char *s, int width, int precision, uint64_t flags)
-
 
106
{
-
 
107
    int counter = 0;
-
 
108
    size_t size;
-
 
109
 
-
 
110
    if (s == NULL) {
-
 
111
        return putstr("(NULL)");
-
 
112
    }
-
 
113
   
-
 
114
    size = strlen(s);
-
 
115
 
-
 
116
    /* print leading spaces */
-
 
117
 
-
 
118
    if (precision == 0)
-
 
119
        precision = size;
-
 
120
 
-
 
121
    width -= precision;
-
 
122
   
-
 
123
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
124
        while (width-- > 0) {  
-
 
125
            putchar(' ');  
-
 
126
            counter++;
-
 
127
        }
-
 
128
    }
-
 
129
 
-
 
130
    while (precision > size) {
-
 
131
        precision--;
-
 
132
        putchar(' ');  
-
 
133
        ++counter;
-
 
134
    }
-
 
135
   
-
 
136
    if (putnchars(s, precision) == EOF) {
-
 
137
        return EOF;
-
 
138
    }
-
 
139
 
-
 
140
    counter += precision;
-
 
141
 
-
 
142
    while (width-- > 0) {
-
 
143
        putchar(' ');  
-
 
144
        ++counter;
-
 
145
    }
-
 
146
   
-
 
147
    return ++counter;
-
 
148
}
-
 
149
 
-
 
150
 
62
/** Print number in given base
151
/** Print number in given base
63
 *
152
 *
64
 * Print significant digits of a number in given
153
 * Print significant digits of a number in given
65
 * base.
154
 * base.
66
 *
155
 *
67
 * @param num  Number to print.
156
 * @param num  Number to print.
-
 
157
 * @param width
68
 * @param size not used, in future releases will be replaced with precision and width params
158
 * @param precision
69
 * @param base Base to print the number in (should
159
 * @param base Base to print the number in (should
70
 *             be in range 2 .. 16).
160
 *             be in range 2 .. 16).
71
 * @param flags output modifiers
161
 * @param flags output modifiers
72
 * @return number of written characters or EOF
162
 * @return number of written characters or EOF
73
 *
163
 *
74
 */
164
 */
75
static int print_number(uint64_t num, size_t size, int base , uint64_t flags)
165
static int print_number(uint64_t num, int width, int precision, int base , uint64_t flags)
76
{
166
{
77
    /* FIXME: This is only first version.
-
 
78
     * Printf does not have support for specification of size
-
 
79
     * and precision, so this function writes with parameters defined by
-
 
80
     * their type size.
-
 
81
     */
-
 
82
    char *digits = digits_small;
167
    char *digits = digits_small;
83
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for base == 2, prefix and sign */
168
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for base == 2, prefix and sign */
84
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
169
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
-
 
170
    int size = 0;
-
 
171
    int written = 0;
-
 
172
    char sgn;
85
   
173
   
86
    if (flags & __PRINTF_FLAG_BIGCHARS)
174
    if (flags & __PRINTF_FLAG_BIGCHARS)
87
        digits = digits_big;   
175
        digits = digits_big;   
88
   
176
   
89
    *ptr-- = 0; /* Put zero at end of string */
177
    *ptr-- = 0; /* Put zero at end of string */
90
 
178
 
91
    if (num == 0) {
179
    if (num == 0) {
92
        *ptr-- = '0';
180
        *ptr-- = '0';
-
 
181
        size++;
93
    } else {
182
    } else {
94
        do {
183
        do {
95
            *ptr-- = digits[num % base];
184
            *ptr-- = digits[num % base];
-
 
185
            size++;
96
        } while (num /= base);
186
        } while (num /= base);
97
    }
187
    }
-
 
188
 
-
 
189
    /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */
98
    if (flags & __PRINTF_FLAG_PREFIX) { /*FIXME*/
190
    if (flags & __PRINTF_FLAG_PREFIX) {
99
        switch(base) {
191
        switch(base) {
100
            case 2: /* Binary formating is not standard, but usefull */
192
            case 2: /* Binary formating is not standard, but usefull */
101
                *ptr = 'b';
193
                size += 2;
102
                if (flags & __PRINTF_FLAG_BIGCHARS) *ptr = 'B';
-
 
103
                ptr--;
-
 
104
                *ptr-- = '0';
-
 
105
                break;
194
                break;
106
            case 8:
195
            case 8:
107
                *ptr-- = 'o';
196
                size++;
108
                break;
197
                break;
109
            case 16:
198
            case 16:
110
                *ptr = 'x';
199
                size += 2;
111
                if (flags & __PRINTF_FLAG_BIGCHARS) *ptr = 'X';
-
 
112
                ptr--;
-
 
113
                *ptr-- = '0';
-
 
114
                break;
200
                break;
115
        }
201
        }
116
    }
202
    }
117
   
203
 
-
 
204
    sgn = 0;
118
    if (flags & __PRINTF_FLAG_SIGNED) {
205
    if (flags & __PRINTF_FLAG_SIGNED) {
119
        if (flags & __PRINTF_FLAG_NEGATIVE) {
206
        if (flags & __PRINTF_FLAG_NEGATIVE) {
120
            *ptr-- = '-';
207
            sgn = '-';
-
 
208
            size++;
121
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
209
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
122
                *ptr-- = '+';
210
                sgn = '+';
-
 
211
                size++;
123
            } else if (flags & __PRINTF_FLAG_SPACESIGN) {
212
            } else if (flags & __PRINTF_FLAG_SPACESIGN) {
124
                    *ptr-- = ' ';
213
                    sgn = ' ';
-
 
214
                    size++;
125
                }
215
                }
126
    }
216
    }
127
 
217
 
128
    /* Print leading zeroes */
-
 
129
 
-
 
130
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
218
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
131
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
219
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
132
    }
220
    }
-
 
221
 
-
 
222
    /* if number is leftaligned or precision is specified then zeropadding is ignored */
133
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
223
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
134
            while (ptr != d ) {
224
        if ((precision == 0) && (width > size)) {
135
            *ptr-- = '0';
225
            precision = width - size;
136
        }
226
        }
137
    }
227
    }
-
 
228
 
-
 
229
    /* print leading spaces */
-
 
230
    if (size > precision) /* We must print whole number not only a part */
-
 
231
        precision = size;
-
 
232
 
-
 
233
    width -= precision;
-
 
234
   
-
 
235
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
-
 
236
        while (width-- > 0) {  
-
 
237
            putchar(' ');  
-
 
238
            written++;
-
 
239
        }
-
 
240
    }
-
 
241
   
-
 
242
    /* print sign */
-
 
243
    if (sgn) {
-
 
244
        putchar(sgn);
-
 
245
        written++;
-
 
246
    }
-
 
247
   
-
 
248
    /* print prefix */
-
 
249
   
-
 
250
    if (flags & __PRINTF_FLAG_PREFIX) {
-
 
251
        switch(base) {
-
 
252
            case 2: /* Binary formating is not standard, but usefull */
-
 
253
                putchar('0');
-
 
254
                if (flags & __PRINTF_FLAG_BIGCHARS) {
-
 
255
                    putchar('B');
-
 
256
                } else {
-
 
257
                    putchar('b');
-
 
258
                }
-
 
259
                written == 2;
-
 
260
                break;
-
 
261
            case 8:
-
 
262
                putchar('o');
-
 
263
                written++;
-
 
264
                break;
-
 
265
            case 16:
-
 
266
                putchar('0');
-
 
267
                if (flags & __PRINTF_FLAG_BIGCHARS) {
-
 
268
                    putchar('X');
-
 
269
                } else {
-
 
270
                    putchar('x');
-
 
271
                }
-
 
272
                written += 2;
-
 
273
                break;
-
 
274
        }
-
 
275
    }
-
 
276
 
-
 
277
    /* print leading zeroes */
-
 
278
    precision -= size;
-
 
279
    while (precision-- > 0) {  
-
 
280
        putchar('0');  
-
 
281
        written++;
-
 
282
    }
-
 
283
 
-
 
284
   
-
 
285
    /* print number itself */
-
 
286
 
-
 
287
    written += putstr(++ptr);
138
   
288
   
-
 
289
    /* print ending spaces */
-
 
290
   
-
 
291
    while (width-- > 0) {  
-
 
292
        putchar(' ');  
-
 
293
        written++;
-
 
294
    }
-
 
295
 
139
    return putstr(++ptr);
296
    return written;
140
}
297
}
141
 
298
 
142
 
299
 
143
 
300
 
144
/** General formatted text print
301
/** General formatted text print
145
 *
302
 *
146
 * Print text formatted according the fmt parameter
303
 * Print text formatted according the fmt parameter
147
 * and variant arguments. Each formatting directive
304
 * and variant arguments. Each formatting directive
148
 * begins with \% (percentage) character and one of the
305
 * begins with \% (percentage) character and one of the
149
 * following character:
306
 * following character:
150
 *
307
 *
151
 * \%    Prints the percentage character.
308
 * \%    Prints the percentage character.
152
 *
309
 *
153
 * s    The next variant argument is treated as char*
310
 * s    The next variant argument is treated as char*
154
 *      and printed as a NULL terminated string.
311
 *      and printed as a NULL terminated string.
155
 *
312
 *
156
 * c    The next variant argument is treated as a single char.
313
 * c    The next variant argument is treated as a single char.
157
 *
314
 *
158
 * p    The next variant argument is treated as a maximum
315
 * p    The next variant argument is treated as a maximum
159
 *      bit-width integer with respect to architecture
316
 *      bit-width integer with respect to architecture
160
 *      and printed in full hexadecimal width.
317
 *      and printed in full hexadecimal width.
161
 *
318
 *
162
 * P    As with 'p', but '0x' is prefixed.
319
 * P    As with 'p', but '0x' is prefixed.
163
 *
320
 *
164
 * q    The next variant argument is treated as a 64b integer
321
 * q    The next variant argument is treated as a 64b integer
165
 *      and printed in full hexadecimal width.
322
 *      and printed in full hexadecimal width.
166
 *
323
 *
167
 * Q    As with 'q', but '0x' is prefixed.
324
 * Q    As with 'q', but '0x' is prefixed.
168
 *
325
 *
169
 * l    The next variant argument is treated as a 32b integer
326
 * l    The next variant argument is treated as a 32b integer
170
 *      and printed in full hexadecimal width.
327
 *      and printed in full hexadecimal width.
171
 *
328
 *
172
 * L    As with 'l', but '0x' is prefixed.
329
 * L    As with 'l', but '0x' is prefixed.
173
 *
330
 *
174
 * w    The next variant argument is treated as a 16b integer
331
 * w    The next variant argument is treated as a 16b integer
175
 *      and printed in full hexadecimal width.
332
 *      and printed in full hexadecimal width.
176
 *
333
 *
177
 * W    As with 'w', but '0x' is prefixed.
334
 * W    As with 'w', but '0x' is prefixed.
178
 *
335
 *
179
 * b    The next variant argument is treated as a 8b integer
336
 * b    The next variant argument is treated as a 8b integer
180
 *      and printed in full hexadecimal width.
337
 *      and printed in full hexadecimal width.
181
 *
338
 *
182
 * B    As with 'b', but '0x' is prefixed.
339
 * B    As with 'b', but '0x' is prefixed.
183
 *
340
 *
184
 * d    The next variant argument is treated as integer
341
 * d    The next variant argument is treated as integer
185
 *      and printed in standard decimal format (only significant
342
 *      and printed in standard decimal format (only significant
186
 *      digits).
343
 *      digits).
187
 *
344
 *
188
 * x    The next variant argument is treated as integer
345
 * x    The next variant argument is treated as integer
189
 *      and printed in standard hexadecimal format (only significant
346
 *      and printed in standard hexadecimal format (only significant
190
 *      digits).
347
 *      digits).
191
 *
348
 *
192
 * X    As with 'x', but '0x' is prefixed.
349
 * X    As with 'x', but '0x' is prefixed.
193
 *
350
 *
194
 * All other characters from fmt except the formatting directives
351
 * All other characters from fmt except the formatting directives
195
 * are printed in verbatim.
352
 * are printed in verbatim.
196
 *
353
 *
197
 * @param fmt Formatting NULL terminated string.
354
 * @param fmt Formatting NULL terminated string.
198
 */
355
 */
199
int printf(const char *fmt, ...)
356
int printf(const char *fmt, ...)
200
{
357
{
201
    int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */
358
    int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */
202
    int end;
359
    int end;
203
    int counter; /* counter of printed characters */
360
    int counter; /* counter of printed characters */
204
    int retval; /* used to store return values from called functions */
361
    int retval; /* used to store return values from called functions */
205
    va_list ap;
362
    va_list ap;
206
    char c;
363
    char c;
207
    qualifier_t qualifier;  /* type of argument */
364
    qualifier_t qualifier;  /* type of argument */
208
    int base;   /* base in which will be parameter (numbers only) printed */
365
    int base;   /* base in which will be parameter (numbers only) printed */
209
    uint64_t number; /* argument value */
366
    uint64_t number; /* argument value */
210
    size_t  size; /* byte size of integer parameter */
367
    size_t  size; /* byte size of integer parameter */
-
 
368
    int width, precision;
211
    uint64_t flags;
369
    uint64_t flags;
212
   
370
   
213
    counter = 0;
371
    counter = 0;
214
    va_start(ap, fmt);
372
    va_start(ap, fmt);
215
 
373
   
216
    while ((c = fmt[i])) {
374
    while ((c = fmt[i])) {
217
        /* control character */
375
        /* control character */
218
        if (c == '%' ) {
376
        if (c == '%' ) {
219
            /* print common characters if any processed */ 
377
            /* print common characters if any processed */ 
220
            if (i > j) {
378
            if (i > j) {
221
                if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
379
                if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
222
                    return -counter;
380
                    return -counter;
223
                }
381
                }
224
                counter += retval;
382
                counter += retval;
225
            }
383
            }
226
       
384
       
227
            j = i;
385
            j = i;
228
            /* parse modifiers */
386
            /* parse modifiers */
229
            flags = 0;
387
            flags = 0;
230
            end = 0;
388
            end = 0;
231
           
389
           
232
            do {
390
            do {
233
                ++i;
391
                ++i;
234
                switch (c = fmt[i]) {
392
                switch (c = fmt[i]) {
235
                    case '#': flags |= __PRINTF_FLAG_PREFIX; break;
393
                    case '#': flags |= __PRINTF_FLAG_PREFIX; break;
236
                    case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break;
394
                    case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break;
237
                    case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break;
395
                    case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break;
238
                    case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break;
396
                    case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break;
239
                    case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break;
397
                    case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break;
240
                    default: end = 1;
398
                    default: end = 1;
241
                }; 
399
                }; 
242
               
400
               
243
            } while (end == 0);
401
            } while (end == 0);
-
 
402
           
244
            /* TODO: width & '*' operator */
403
            /* width & '*' operator */
-
 
404
            width = 0;
-
 
405
            if (isdigit(fmt[i])) {
-
 
406
                while (isdigit(fmt[i])) {
-
 
407
                    width *= 10;
-
 
408
                    width += fmt[i++] - '0';
-
 
409
                }
-
 
410
            } else if (fmt[i] == '*') {
-
 
411
                /* get width value from argument list*/
-
 
412
                i++;
-
 
413
                width = (int)va_arg(ap, int);
-
 
414
                if (width < 0) {
-
 
415
                    /* negative width means to set '-' flag */
-
 
416
                    width *= -1;
-
 
417
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
-
 
418
                }
-
 
419
            }
-
 
420
           
245
            /* TODO: precision and '*' operator */ 
421
            /* precision and '*' operator */   
-
 
422
            precision = 0;
-
 
423
            if (fmt[i] == '.') {
-
 
424
                ++i;
-
 
425
                if (isdigit(fmt[i])) {
-
 
426
                    while (isdigit(fmt[i])) {
-
 
427
                        precision *= 10;
-
 
428
                        precision += fmt[i++] - '0';
-
 
429
                    }
-
 
430
                } else if (fmt[i] == '*') {
-
 
431
                    /* get precision value from argument list*/
-
 
432
                    i++;
-
 
433
                    precision = (int)va_arg(ap, int);
-
 
434
                    if (precision < 0) {
-
 
435
                        /* negative precision means to ignore it */
-
 
436
                        precision = 0;
-
 
437
                    }
-
 
438
                }
-
 
439
            }
246
 
440
 
247
            switch (fmt[i++]) {
441
            switch (fmt[i++]) {
-
 
442
                /** TODO: unimplemented qualifiers:
-
 
443
                 * t ptrdiff_t - ISO C 99
-
 
444
                 */
248
                case 'h':   /* char or short */
445
                case 'h':   /* char or short */
249
                    qualifier = PrintfQualifierShort;
446
                    qualifier = PrintfQualifierShort;
250
                    if (fmt[i] == 'h') {
447
                    if (fmt[i] == 'h') {
251
                        i++;
448
                        i++;
252
                        qualifier = PrintfQualifierByte;
449
                        qualifier = PrintfQualifierByte;
253
                    }
450
                    }
254
                    break;
451
                    break;
255
                case 'l':   /* long or long long*/
452
                case 'l':   /* long or long long*/
256
                    qualifier = PrintfQualifierLong;
453
                    qualifier = PrintfQualifierLong;
257
                    if (fmt[i] == 'l') {
454
                    if (fmt[i] == 'l') {
258
                        i++;
455
                        i++;
259
                        qualifier = PrintfQualifierLongLong;
456
                        qualifier = PrintfQualifierLongLong;
260
                    }
457
                    }
261
                    break;
458
                    break;
262
                case 'z':   /* size_t */
459
                case 'z':   /* size_t */
263
                    qualifier = PrintfQualifierSizeT;
460
                    qualifier = PrintfQualifierSizeT;
264
                    break;
461
                    break;
265
                default:
462
                default:
266
                    qualifier = PrintfQualifierInt; /* default type */
463
                    qualifier = PrintfQualifierInt; /* default type */
267
                    --i;
464
                    --i;
268
            }  
465
            }  
269
           
466
           
270
            base = 10;
467
            base = 10;
271
 
468
 
272
            switch (c = fmt[i]) {
469
            switch (c = fmt[i]) {
273
 
470
 
274
                /*
471
                /*
275
                * String and character conversions.
472
                * String and character conversions.
276
                */
473
                */
277
                case 's':
474
                case 's':
278
                    if ((retval = putstr(va_arg(ap, char*))) == EOF) {
475
                    if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) {
279
                        return -counter;
476
                        return -counter;
280
                    };
477
                    };
281
                   
478
                   
282
                    counter += retval;
479
                    counter += retval;
283
                    j = i + 1;
480
                    j = i + 1;
284
                    goto next_char;
481
                    goto next_char;
285
                case 'c':
482
                case 'c':
286
                    c = va_arg(ap, unsigned long);
483
                    c = va_arg(ap, unsigned int);
287
                    if ((retval = putnchars(&c, sizeof(char))) == EOF) {
484
                    if ((retval = print_char(c, width, flags )) == EOF) {
288
                        return -counter;
485
                        return -counter;
289
                    };
486
                    };
290
                   
487
                   
291
                    counter += retval;
488
                    counter += retval;
292
                    j = i + 1;
489
                    j = i + 1;
293
                    goto next_char;
490
                    goto next_char;
294
 
491
 
295
                /*
492
                /*
296
                 * Integer values
493
                 * Integer values
297
                */
494
                */
298
                case 'P': /* pointer */
495
                case 'P': /* pointer */
299
                        flags |= __PRINTF_FLAG_BIGCHARS;
496
                        flags |= __PRINTF_FLAG_BIGCHARS;
300
                case 'p':
497
                case 'p':
301
                    flags |= __PRINTF_FLAG_PREFIX;
498
                    flags |= __PRINTF_FLAG_PREFIX;
302
                    base = 16;
499
                    base = 16;
303
                    qualifier = PrintfQualifierPointer;
500
                    qualifier = PrintfQualifierPointer;
304
                    break; 
501
                    break; 
305
                case 'b':
502
                case 'b':
306
                    base = 2;
503
                    base = 2;
307
                    break;
504
                    break;
308
                case 'o':
505
                case 'o':
309
                    base = 8;
506
                    base = 8;
310
                    break;
507
                    break;
311
                case 'd':
508
                case 'd':
312
                case 'i':
509
                case 'i':
313
                    flags |= __PRINTF_FLAG_SIGNED;  
510
                    flags |= __PRINTF_FLAG_SIGNED;  
314
                case 'u':
511
                case 'u':
315
                    break;
512
                    break;
316
                case 'X':
513
                case 'X':
317
                    flags |= __PRINTF_FLAG_BIGCHARS;
514
                    flags |= __PRINTF_FLAG_BIGCHARS;
318
                case 'x':
515
                case 'x':
319
                    base = 16;
516
                    base = 16;
320
                    break;
517
                    break;
321
                /* percentile itself */
518
                /* percentile itself */
322
                case '%':
519
                case '%':
323
                    j = i;
520
                    j = i;
324
                    goto next_char;
521
                    goto next_char;
325
                /*
522
                /*
326
                * Bad formatting.
523
                * Bad formatting.
327
                */
524
                */
328
                default:
525
                default:
329
                    /* Unknown format
526
                    /* Unknown format
330
                     *  now, the j is index of '%' so we will
527
                     *  now, the j is index of '%' so we will
331
                     * print whole bad format sequence
528
                     * print whole bad format sequence
332
                     */
529
                     */
333
                    goto next_char;    
530
                    goto next_char;    
334
            }
531
            }
335
       
532
       
336
       
533
       
337
        /* Print integers */
534
        /* Print integers */
338
            /* print number */
535
            /* print number */
339
            switch (qualifier) {
536
            switch (qualifier) {
340
                case PrintfQualifierByte:
537
                case PrintfQualifierByte:
341
                    size = sizeof(unsigned char);
538
                    size = sizeof(unsigned char);
342
                    number = (uint64_t)va_arg(ap, unsigned int);
539
                    number = (uint64_t)va_arg(ap, unsigned int);
343
                    break;
540
                    break;
344
                case PrintfQualifierShort:
541
                case PrintfQualifierShort:
345
                    size = sizeof(unsigned short);
542
                    size = sizeof(unsigned short);
346
                    number = (uint64_t)va_arg(ap, unsigned int);
543
                    number = (uint64_t)va_arg(ap, unsigned int);
347
                    break;
544
                    break;
348
                case PrintfQualifierInt:
545
                case PrintfQualifierInt:
349
                    size = sizeof(unsigned int);
546
                    size = sizeof(unsigned int);
350
                    number = (uint64_t)va_arg(ap, unsigned int);
547
                    number = (uint64_t)va_arg(ap, unsigned int);
351
                    break;
548
                    break;
352
                case PrintfQualifierLong:
549
                case PrintfQualifierLong:
353
                    size = sizeof(unsigned long);
550
                    size = sizeof(unsigned long);
354
                    number = (uint64_t)va_arg(ap, unsigned long);
551
                    number = (uint64_t)va_arg(ap, unsigned long);
355
                    break;
552
                    break;
356
                case PrintfQualifierLongLong:
553
                case PrintfQualifierLongLong:
357
                    size = sizeof(unsigned long long);
554
                    size = sizeof(unsigned long long);
358
                    number = (uint64_t)va_arg(ap, unsigned long long);
555
                    number = (uint64_t)va_arg(ap, unsigned long long);
359
                    break;
556
                    break;
360
                case PrintfQualifierPointer:
557
                case PrintfQualifierPointer:
361
                    size = sizeof(void *);
558
                    size = sizeof(void *);
362
                    number = (uint64_t)(unsigned long)va_arg(ap, void *);
559
                    number = (uint64_t)(unsigned long)va_arg(ap, void *);
363
                    break;
560
                    break;
364
                case PrintfQualifierSizeT:
561
                case PrintfQualifierSizeT:
365
                    size = sizeof(size_t);
562
                    size = sizeof(size_t);
366
                    number = (uint64_t)va_arg(ap, size_t);
563
                    number = (uint64_t)va_arg(ap, size_t);
367
                    break;
564
                    break;
368
                default: /* Unknown qualifier */
565
                default: /* Unknown qualifier */
369
                    return -counter;
566
                    return -counter;
370
                   
567
                   
371
            }
568
            }
372
           
569
           
373
            if (flags & __PRINTF_FLAG_SIGNED) {
570
            if (flags & __PRINTF_FLAG_SIGNED) {
374
                if (number & (0x1 << (size*8 - 1))) {
571
                if (number & (0x1 << (size*8 - 1))) {
375
                    flags |= __PRINTF_FLAG_NEGATIVE;
572
                    flags |= __PRINTF_FLAG_NEGATIVE;
376
               
573
               
377
                    if (size == sizeof(uint64_t)) {
574
                    if (size == sizeof(uint64_t)) {
378
                        number = -((int64_t)number);
575
                        number = -((int64_t)number);
379
                    } else {
576
                    } else {
380
                        number = ~number;
577
                        number = ~number;
381
                        number &= (~((0xFFFFFFFFFFFFFFFFll) <<  (size * 8)));
578
                        number &= (~((0xFFFFFFFFFFFFFFFFll) <<  (size * 8)));
382
                        number++;
579
                        number++;
383
                    }
580
                    }
384
                }
581
                }
385
            }
582
            }
386
 
583
 
387
            if ((retval = print_number(number, size, base, flags)) == EOF ) {
584
            if ((retval = print_number(number, width, precision, base, flags)) == EOF ) {
388
                return -counter;
585
                return -counter;
389
            };
586
            };
390
 
587
 
391
            counter += retval;
588
            counter += retval;
392
            j = i + 1;
589
            j = i + 1;
393
        }  
590
        }  
394
next_char:
591
next_char:
395
           
592
           
396
        ++i;
593
        ++i;
397
    }
594
    }
398
   
595
   
399
    if (i > j) {
596
    if (i > j) {
400
        if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
597
        if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
401
            return -counter;
598
            return -counter;
402
        }
599
        }
403
        counter += retval;
600
        counter += retval;
404
    }
601
    }
405
   
602
   
406
    va_end(ap);
603
    va_end(ap);
407
    return counter;
604
    return counter;
408
}
605
}
409
 
606
 
410
 
607