Subversion Repositories HelenOS

Rev

Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
974 cejka 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
3
 * Copyright (c) 2006 Josef Cejka
974 cejka 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
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
16
 *   derived from this software without specific prior written permission.
17
 *
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
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1866 jermar 30
/** @addtogroup libc
1653 cejka 31
 * @{
32
 */
1272 cejka 33
/**
1653 cejka 34
 * @file
1272 cejka 35
 * @brief   Printing functions.
36
 */
37
 
1234 cejka 38
#include <unistd.h>
974 cejka 39
#include <stdio.h>
1234 cejka 40
#include <io/printf_core.h>
1173 cejka 41
#include <ctype.h>
42
#include <string.h>
974 cejka 43
 
1272 cejka 44
#define __PRINTF_FLAG_PREFIX        0x00000001  /**< show prefixes 0x or 0*/
45
#define __PRINTF_FLAG_SIGNED        0x00000002  /**< signed / unsigned number */
46
#define __PRINTF_FLAG_ZEROPADDED    0x00000004  /**< print leading zeroes */
47
#define __PRINTF_FLAG_LEFTALIGNED   0x00000010  /**< align to left */
48
#define __PRINTF_FLAG_SHOWPLUS      0x00000020  /**< always show + sign */
49
#define __PRINTF_FLAG_SPACESIGN     0x00000040  /**< print space instead of plus */
50
#define __PRINTF_FLAG_BIGCHARS      0x00000080  /**< show big characters */
51
#define __PRINTF_FLAG_NEGATIVE      0x00000100  /**< number has - sign */
995 cejka 52
 
1272 cejka 53
#define PRINT_NUMBER_BUFFER_SIZE    (64+5)      /**< Buffer big enought for 64 bit number
1073 cejka 54
                             * printed in base 2, sign, prefix and
55
                             * 0 to terminate string.. (last one is only for better testing
56
                             * end of buffer by zero-filling subroutine)
57
                             */
1272 cejka 58
/** Enumeration of possible arguments types.
59
 */
1073 cejka 60
typedef enum {
61
    PrintfQualifierByte = 0,
62
    PrintfQualifierShort,
63
    PrintfQualifierInt,
64
    PrintfQualifierLong,
65
    PrintfQualifierLongLong,
66
    PrintfQualifierSizeT,
67
    PrintfQualifierPointer
68
} qualifier_t;
974 cejka 69
 
1272 cejka 70
static char digits_small[] = "0123456789abcdef";    /**< Small hexadecimal characters */
71
static char digits_big[] = "0123456789ABCDEF";      /**< Big hexadecimal characters */
995 cejka 72
 
1234 cejka 73
/** Print count chars from buffer without adding newline
74
 * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed!
75
 * @param count
76
 * @param ps output method and its data
1617 cejka 77
 * @return number of printed characters
1234 cejka 78
 */
2570 jermar 79
static int printf_putnchars(const char * buf, size_t count,
80
    struct printf_spec *ps)
1234 cejka 81
{
1617 cejka 82
    return ps->write((void *)buf, count, ps->data);
1234 cejka 83
}
1173 cejka 84
 
1234 cejka 85
/** Print string without added newline
86
 * @param str string to print
87
 * @param ps write function specification and support data
1617 cejka 88
 * @return number of printed characters
1234 cejka 89
 */
90
static int printf_putstr(const char * str, struct printf_spec *ps)
91
{
92
    size_t count;
93
 
2209 decky 94
    if (str == NULL)
1234 cejka 95
        return printf_putnchars("(NULL)", 6, ps);
96
 
3397 rimsky 97
    count = strlen(str);
1234 cejka 98
 
3397 rimsky 99
    return ps->write((void *) str, count, ps->data);
1234 cejka 100
}
101
 
102
/** Print one character to output
103
 * @param c one character
104
 * @param ps output method
1617 cejka 105
 * @return number of printed characters
1234 cejka 106
 */
107
static int printf_putchar(int c, struct printf_spec *ps)
108
{
109
    unsigned char ch = c;
110
 
1617 cejka 111
    return ps->write((void *) &ch, 1, ps->data);
1234 cejka 112
}
113
 
1173 cejka 114
/** Print one formatted character
115
 * @param c character to print
116
 * @param width
117
 * @param flags
1617 cejka 118
 * @return number of printed characters
1173 cejka 119
 */
1234 cejka 120
static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps)
1173 cejka 121
{
122
    int counter = 0;
123
 
124
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
2570 jermar 125
        /*
126
         * One space is consumed by the character itself, hence the
127
         * predecrement.
128
         */
129
        while (--width > 0) {
1617 cejka 130
            if (printf_putchar(' ', ps) > 0)   
131
                ++counter;
1173 cejka 132
        }
133
    }
1617 cejka 134
 
135
    if (printf_putchar(c, ps) > 0)
136
        counter++;
2570 jermar 137
    /*
138
     * One space is consumed by the character itself, hence the
139
     * predecrement.
140
     */
141
    while (--width > 0) {
1617 cejka 142
        if (printf_putchar(' ', ps) > 0)
143
            ++counter;
1173 cejka 144
    }
145
 
146
    return ++counter;
147
}
148
 
149
/** Print one string
150
 * @param s string
151
 * @param width
152
 * @param precision
153
 * @param flags
1617 cejka 154
 * @return number of printed characters
1173 cejka 155
 */
2570 jermar 156
static int print_string(char *s, int width, int precision, uint64_t flags,
157
    struct printf_spec *ps)
1173 cejka 158
{
159
    int counter = 0;
160
    size_t size;
1617 cejka 161
    int retval;
1173 cejka 162
 
163
    if (s == NULL) {
1234 cejka 164
        return printf_putstr("(NULL)", ps);
1173 cejka 165
    }
166
 
167
    size = strlen(s);
168
 
169
    /* print leading spaces */
170
 
171
    if (precision == 0)
172
        precision = size;
173
 
174
    width -= precision;
175
 
176
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
177
        while (width-- > 0) {  
1617 cejka 178
            if (printf_putchar(' ', ps) == 1)  
179
                counter++;
1173 cejka 180
        }
181
    }
182
 
2570 jermar 183
    if ((retval = printf_putnchars(s, size < precision ? size : precision,
184
        ps)) < 0) {
1617 cejka 185
        return -counter;
1173 cejka 186
    }
187
 
1617 cejka 188
    counter += retval; 
1173 cejka 189
 
190
    while (width-- > 0) {
1617 cejka 191
        if (printf_putchar(' ', ps) == 1)  
192
            ++counter;
1173 cejka 193
    }
194
 
1617 cejka 195
    return counter;
1173 cejka 196
}
197
 
198
 
974 cejka 199
/** Print number in given base
200
 *
201
 * Print significant digits of a number in given
202
 * base.
203
 *
204
 * @param num  Number to print.
1173 cejka 205
 * @param width
206
 * @param precision
974 cejka 207
 * @param base Base to print the number in (should
208
 *             be in range 2 .. 16).
1073 cejka 209
 * @param flags output modifiers
1617 cejka 210
 * @return number of printed characters
974 cejka 211
 *
212
 */
2570 jermar 213
static int print_number(uint64_t num, int width, int precision, int base,
214
    uint64_t flags, struct printf_spec *ps)
974 cejka 215
{
1073 cejka 216
    char *digits = digits_small;
2570 jermar 217
    char d[PRINT_NUMBER_BUFFER_SIZE];   /* this is good enough even for
218
                         * base == 2, prefix and sign */
1073 cejka 219
    char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
1234 cejka 220
    int size = 0; /* size of number with all prefixes and signs */
221
    int number_size; /* size of plain number */
1173 cejka 222
    char sgn;
1617 cejka 223
    int retval;
224
    int counter = 0;
974 cejka 225
 
1073 cejka 226
    if (flags & __PRINTF_FLAG_BIGCHARS)
227
        digits = digits_big;   
995 cejka 228
 
1073 cejka 229
    *ptr-- = 0; /* Put zero at end of string */
230
 
231
    if (num == 0) {
232
        *ptr-- = '0';
1173 cejka 233
        size++;
1073 cejka 234
    } else {
235
        do {
236
            *ptr-- = digits[num % base];
1173 cejka 237
            size++;
1073 cejka 238
        } while (num /= base);
239
    }
1234 cejka 240
 
241
    number_size = size;
1173 cejka 242
 
2570 jermar 243
    /*
244
     * Collect sum of all prefixes/signs/... to calculate padding and
245
     * leading zeroes
246
     */
1173 cejka 247
    if (flags & __PRINTF_FLAG_PREFIX) {
1073 cejka 248
        switch(base) {
2070 jermar 249
        case 2: /* Binary formating is not standard, but usefull */
250
            size += 2;
251
            break;
252
        case 8:
253
            size++;
254
            break;
255
        case 16:
256
            size += 2;
257
            break;
1073 cejka 258
        }
259
    }
1173 cejka 260
 
261
    sgn = 0;
1073 cejka 262
    if (flags & __PRINTF_FLAG_SIGNED) {
263
        if (flags & __PRINTF_FLAG_NEGATIVE) {
1173 cejka 264
            sgn = '-';
265
            size++;
1073 cejka 266
        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
2570 jermar 267
            sgn = '+';
268
            size++;
269
        } else if (flags & __PRINTF_FLAG_SPACESIGN) {
270
            sgn = ' ';
271
            size++;
272
        }
1073 cejka 273
    }
974 cejka 274
 
1073 cejka 275
    if (flags & __PRINTF_FLAG_LEFTALIGNED) {
276
        flags &= ~__PRINTF_FLAG_ZEROPADDED;
277
    }
1173 cejka 278
 
2570 jermar 279
    /*
280
     * If number is leftaligned or precision is specified then zeropadding
281
     * is ignored.
282
     */
1073 cejka 283
    if (flags & __PRINTF_FLAG_ZEROPADDED) {
1173 cejka 284
        if ((precision == 0) && (width > size)) {
1234 cejka 285
            precision = width - size + number_size;
1073 cejka 286
        }
287
    }
1173 cejka 288
 
289
    /* print leading spaces */
2570 jermar 290
 
291
    /* We must print whole number not only a part. */
292
    if (number_size > precision)
1234 cejka 293
        precision = number_size;
1173 cejka 294
 
1234 cejka 295
    width -= precision + size - number_size;
1073 cejka 296
 
1173 cejka 297
    if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
298
        while (width-- > 0) {  
1617 cejka 299
            if (printf_putchar(' ', ps) == 1)  
300
                counter++;
1173 cejka 301
        }
302
    }
303
 
304
    /* print sign */
305
    if (sgn) {
1617 cejka 306
        if (printf_putchar(sgn, ps) == 1)
307
            counter++;
1173 cejka 308
    }
309
 
310
    /* print prefix */
311
 
312
    if (flags & __PRINTF_FLAG_PREFIX) {
313
        switch(base) {
2070 jermar 314
        case 2: /* Binary formating is not standard, but usefull */
315
            if (printf_putchar('0', ps) == 1)
316
                counter++;
317
            if (flags & __PRINTF_FLAG_BIGCHARS) {
318
                if (printf_putchar('B', ps) == 1)
1617 cejka 319
                    counter++;
2070 jermar 320
            } else {
321
                if (printf_putchar('b', ps) == 1)
1617 cejka 322
                    counter++;
2070 jermar 323
            }
324
            break;
325
        case 8:
326
            if (printf_putchar('o', ps) == 1)
327
                counter++;
328
            break;
329
        case 16:
330
            if (printf_putchar('0', ps) == 1)
331
                counter++;
332
            if (flags & __PRINTF_FLAG_BIGCHARS) {
333
                if (printf_putchar('X', ps) == 1)
1617 cejka 334
                    counter++;
2070 jermar 335
            } else {
336
                if (printf_putchar('x', ps) == 1)
337
                    counter++;
338
            }
339
            break;
1173 cejka 340
        }
341
    }
342
 
343
    /* print leading zeroes */
1234 cejka 344
    precision -= number_size;
1173 cejka 345
    while (precision-- > 0) {  
1617 cejka 346
        if (printf_putchar('0', ps) == 1)
347
            counter++;
1173 cejka 348
    }
349
 
350
 
351
    /* print number itself */
352
 
1617 cejka 353
    if ((retval = printf_putstr(++ptr, ps)) > 0) {
354
        counter += retval;
355
    }
1173 cejka 356
 
357
    /* print ending spaces */
358
 
359
    while (width-- > 0) {  
1617 cejka 360
        if (printf_putchar(' ', ps) == 1)  
361
            counter++;
1173 cejka 362
    }
363
 
1617 cejka 364
    return counter;
974 cejka 365
}
366
 
367
 
1272 cejka 368
/** Print formatted string.
974 cejka 369
 *
2570 jermar 370
 * Print string formatted according to the fmt parameter and variadic arguments.
371
 * Each formatting directive must have the following form:
1272 cejka 372
 *
373
 *  \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
974 cejka 374
 *
1272 cejka 375
 * FLAGS:@n
376
 *  - "#" Force to print prefix.
2570 jermar 377
 *  For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and
378
 *  0X and for conversion \%b the prefix is 0b.
1272 cejka 379
 *
380
 *  - "-"   Align to left.
381
 *
382
 *  - "+"   Print positive sign just as negative.
383
 *
2570 jermar 384
 *  - " "   If the printed number is positive and "+" flag is not set,
385
 *      print space in place of sign.
1272 cejka 386
 *
2570 jermar 387
 *  - "0"   Print 0 as padding instead of spaces. Zeroes are placed between
388
 *      sign and the rest of the number. This flag is ignored if "-"
389
 *      flag is specified.
1199 cejka 390
 *
1272 cejka 391
 * WIDTH:@n
2570 jermar 392
 *  - Specify minimal width of printed argument. If it is bigger, width is
393
 *    ignored. If width is specified with a "*" character instead of number,
394
 *    width is taken from parameter list. And integer parameter is expected
395
 *    before parameter for processed conversion specification. If this value
396
 *    is negative its absolute value is taken and the "-" flag is set.
974 cejka 397
 *
1272 cejka 398
 * PRECISION:@n
399
 *  - Value precision. For numbers it specifies minimum valid numbers.
2570 jermar 400
 *    Smaller numbers are printed with leading zeroes. Bigger numbers are
401
 *    not affected. Strings with more than precision characters are cut off.
402
 *    Just as with width, an "*" can be used used instead of a number. An
403
 *    integer value is then expected in parameters. When both width and
404
 *    precision are specified using "*", the first parameter is used for
405
 *    width and the second one for precision.
1199 cejka 406
 *
1272 cejka 407
 * TYPE:@n
408
 *  - "hh"  Signed or unsigned char.@n
409
 *  - "h"   Signed or usigned short.@n
410
 *  - ""    Signed or usigned int (default value).@n
411
 *  - "l"   Signed or usigned long int.@n
412
 *  - "ll"  Signed or usigned long long int.@n
413
 *  - "z"   Type size_t.@n
1199 cejka 414
 *
415
 *
1272 cejka 416
 * CONVERSION:@n
417
 *  - % Print percentile character itself.
974 cejka 418
 *
1272 cejka 419
 *  - c Print single character.
974 cejka 420
 *
2570 jermar 421
 *  - s Print zero terminated string. If a NULL value is passed as
422
 *      value, "(NULL)" is printed instead.
1199 cejka 423
 *
2570 jermar 424
 *  - P, p  Print value of a pointer. Void * value is expected and it is
425
 *      printed in hexadecimal notation with prefix (as with '\%#X' or
426
 *      '\%#x' for 32bit or '\%#X' or '\%#x' for 64bit long pointers).
974 cejka 427
 *
2570 jermar 428
 *  - b Print value as unsigned binary number. Prefix is not printed by
429
 *      default. (Nonstandard extension.)
1199 cejka 430
 *
2570 jermar 431
 *  - o Print value as unsigned octal number. Prefix is not printed by
432
 *      default.
974 cejka 433
 *
2570 jermar 434
 *  - d, i  Print signed decimal number. There is no difference between d
435
 *      and i conversion.
974 cejka 436
 *
1272 cejka 437
 *  - u Print unsigned decimal number.
974 cejka 438
 *
2570 jermar 439
 *  - X, x  Print hexadecimal number with upper- or lower-case. Prefix is
440
 *      not printed by default.
1199 cejka 441
 *
2570 jermar 442
 * All other characters from fmt except the formatting directives are printed in
443
 * verbatim.
974 cejka 444
 *
445
 * @param fmt Formatting NULL terminated string.
1272 cejka 446
 * @return Number of printed characters or negative value on failure.
974 cejka 447
 */
1234 cejka 448
int printf_core(const char *fmt, struct printf_spec *ps, va_list ap)
974 cejka 449
{
2570 jermar 450
    /* i is the index of the currently processed char from fmt */
451
    int i = 0;
452
    /* j is the index to the first not printed nonformating character */
453
    int j = 0;
454
 
1073 cejka 455
    int end;
2570 jermar 456
    int counter;    /* counter of printed characters */
457
    int retval; /* used to store return values from called functions */
1073 cejka 458
    char c;
1444 cejka 459
    qualifier_t qualifier;  /* type of argument */
2570 jermar 460
    int base;   /* base in which will be a numeric parameter printed */
1444 cejka 461
    uint64_t number; /* argument value */
2570 jermar 462
    size_t  size;   /* byte size of integer parameter */
1173 cejka 463
    int width, precision;
995 cejka 464
    uint64_t flags;
974 cejka 465
 
466
    counter = 0;
1173 cejka 467
 
974 cejka 468
    while ((c = fmt[i])) {
992 jermar 469
        /* control character */
974 cejka 470
        if (c == '%' ) {
992 jermar 471
            /* print common characters if any processed */ 
974 cejka 472
            if (i > j) {
2570 jermar 473
                if ((retval = printf_putnchars(&fmt[j],
474
                    (size_t)(i - j), ps)) < 0) { /* error */
1616 palkovsky 475
                    goto minus_out;
974 cejka 476
                }
477
                counter += retval;
478
            }
1073 cejka 479
 
480
            j = i;
995 cejka 481
            /* parse modifiers */
482
            flags = 0;
1073 cejka 483
            end = 0;
484
 
485
            do {
486
                ++i;
487
                switch (c = fmt[i]) {
2570 jermar 488
                case '#':
489
                    flags |= __PRINTF_FLAG_PREFIX;
490
                    break;
491
                case '-':
492
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
493
                    break;
494
                case '+':
495
                    flags |= __PRINTF_FLAG_SHOWPLUS;
496
                    break;
497
                case ' ':
498
                    flags |= __PRINTF_FLAG_SPACESIGN;
499
                    break;
500
                case '0':
501
                    flags |= __PRINTF_FLAG_ZEROPADDED;
502
                    break;
503
                default:
504
                    end = 1;
1073 cejka 505
                }; 
506
 
507
            } while (end == 0);
1173 cejka 508
 
509
            /* width & '*' operator */
510
            width = 0;
511
            if (isdigit(fmt[i])) {
512
                while (isdigit(fmt[i])) {
513
                    width *= 10;
514
                    width += fmt[i++] - '0';
515
                }
516
            } else if (fmt[i] == '*') {
517
                /* get width value from argument list*/
518
                i++;
519
                width = (int)va_arg(ap, int);
520
                if (width < 0) {
2570 jermar 521
                    /* negative width sets '-' flag */
1173 cejka 522
                    width *= -1;
523
                    flags |= __PRINTF_FLAG_LEFTALIGNED;
524
                }
525
            }
526
 
527
            /* precision and '*' operator */   
528
            precision = 0;
529
            if (fmt[i] == '.') {
530
                ++i;
531
                if (isdigit(fmt[i])) {
532
                    while (isdigit(fmt[i])) {
533
                        precision *= 10;
534
                        precision += fmt[i++] - '0';
535
                    }
536
                } else if (fmt[i] == '*') {
2570 jermar 537
                    /* get precision value from argument */
1173 cejka 538
                    i++;
539
                    precision = (int)va_arg(ap, int);
540
                    if (precision < 0) {
2570 jermar 541
                        /* negative precision ignored */
1173 cejka 542
                        precision = 0;
543
                    }
544
                }
545
            }
1073 cejka 546
 
547
            switch (fmt[i++]) {
2570 jermar 548
            /** @todo unimplemented qualifiers:
2070 jermar 549
             * t ptrdiff_t - ISO C 99
550
             */
551
            case 'h':   /* char or short */
552
                qualifier = PrintfQualifierShort;
553
                if (fmt[i] == 'h') {
554
                    i++;
555
                    qualifier = PrintfQualifierByte;
556
                }
557
                break;
558
            case 'l':   /* long or long long*/
559
                qualifier = PrintfQualifierLong;
560
                if (fmt[i] == 'l') {
561
                    i++;
562
                    qualifier = PrintfQualifierLongLong;
563
                }
564
                break;
565
            case 'z':   /* size_t */
566
                qualifier = PrintfQualifierSizeT;
567
                break;
568
            default:
2570 jermar 569
                /* set default type */
570
                qualifier = PrintfQualifierInt;
2070 jermar 571
                --i;
995 cejka 572
            }  
1073 cejka 573
 
574
            base = 10;
575
 
974 cejka 576
            switch (c = fmt[i]) {
577
 
2570 jermar 578
            /*
579
             * String and character conversions.
580
             */
2070 jermar 581
            case 's':
2570 jermar 582
                if ((retval = print_string(va_arg(ap, char*),
583
                    width, precision, flags, ps)) < 0) {
2070 jermar 584
                    goto minus_out;
585
                }
974 cejka 586
 
2070 jermar 587
                counter += retval;
588
                j = i + 1;
589
                goto next_char;
590
            case 'c':
591
                c = va_arg(ap, unsigned int);
2570 jermar 592
                retval = print_char(c, width, flags, ps);
593
                if (retval < 0) {
2070 jermar 594
                    goto minus_out;
595
                }
974 cejka 596
 
2070 jermar 597
                counter += retval;
598
                j = i + 1;
599
                goto next_char;
600
 
601
            /*
602
             * Integer values
2570 jermar 603
             */
2070 jermar 604
            case 'P': /* pointer */
605
                    flags |= __PRINTF_FLAG_BIGCHARS;
606
            case 'p':
607
                flags |= __PRINTF_FLAG_PREFIX;
608
                base = 16;
609
                qualifier = PrintfQualifierPointer;
610
                break; 
611
            case 'b':
612
                base = 2;
613
                break;
614
            case 'o':
615
                base = 8;
616
                break;
617
            case 'd':
618
            case 'i':
619
                flags |= __PRINTF_FLAG_SIGNED;  
620
            case 'u':
621
                break;
622
            case 'X':
623
                flags |= __PRINTF_FLAG_BIGCHARS;
624
            case 'x':
625
                base = 16;
626
                break;
627
            /* percentile itself */
628
            case '%':
629
                j = i;
630
                goto next_char;
631
            /*
2570 jermar 632
             * Bad formatting.
633
             */
2070 jermar 634
            default:
2570 jermar 635
                /*
636
                 * Unknown format. Now, j is the index of '%',
637
                 * so we will print the whole bad format
638
                 * sequence.
2070 jermar 639
                 */
640
                goto next_char;    
1073 cejka 641
            }
642
 
643
 
2570 jermar 644
            /* Print integers */
1073 cejka 645
            switch (qualifier) {
2070 jermar 646
            case PrintfQualifierByte:
647
                size = sizeof(unsigned char);
648
                number = (uint64_t)va_arg(ap, unsigned int);
649
                break;
650
            case PrintfQualifierShort:
651
                size = sizeof(unsigned short);
652
                number = (uint64_t)va_arg(ap, unsigned int);
653
                break;
654
            case PrintfQualifierInt:
655
                size = sizeof(unsigned int);
656
                number = (uint64_t)va_arg(ap, unsigned int);
657
                break;
658
            case PrintfQualifierLong:
659
                size = sizeof(unsigned long);
660
                number = (uint64_t)va_arg(ap, unsigned long);
661
                break;
662
            case PrintfQualifierLongLong:
663
                size = sizeof(unsigned long long);
2570 jermar 664
                number = (uint64_t)va_arg(ap,
665
                    unsigned long long);
2070 jermar 666
                break;
667
            case PrintfQualifierPointer:
668
                size = sizeof(void *);
2570 jermar 669
                number = (uint64_t)(unsigned long)va_arg(ap,
670
                    void *);
2070 jermar 671
                break;
672
            case PrintfQualifierSizeT:
673
                size = sizeof(size_t);
674
                number = (uint64_t)va_arg(ap, size_t);
675
                break;
676
            default: /* Unknown qualifier */
677
                goto minus_out;
1073 cejka 678
 
974 cejka 679
            }
1073 cejka 680
 
681
            if (flags & __PRINTF_FLAG_SIGNED) {
2570 jermar 682
                if (number & (0x1 << (size * 8 - 1))) {
1073 cejka 683
                    flags |= __PRINTF_FLAG_NEGATIVE;
684
 
685
                    if (size == sizeof(uint64_t)) {
686
                        number = -((int64_t)number);
687
                    } else {
688
                        number = ~number;
2570 jermar 689
                        number &=
690
                            ~(0xFFFFFFFFFFFFFFFFll <<
691
                            (size * 8));
1073 cejka 692
                        number++;
693
                    }
694
                }
695
            }
696
 
2570 jermar 697
            if ((retval = print_number(number, width, precision,
698
                base, flags, ps)) < 0 ) {
1616 palkovsky 699
                goto minus_out;
2570 jermar 700
            }
1073 cejka 701
 
702
            counter += retval;
703
            j = i + 1;
974 cejka 704
        }  
1073 cejka 705
next_char:
706
 
974 cejka 707
        ++i;
708
    }
709
 
975 cejka 710
    if (i > j) {
2570 jermar 711
        retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps);
712
        if (retval < 0) { /* error */
1616 palkovsky 713
            goto minus_out;
975 cejka 714
        }
715
        counter += retval;
716
    }
717
 
974 cejka 718
    return counter;
1616 palkovsky 719
minus_out:
720
    return -counter;
974 cejka 721
}
722
 
1866 jermar 723
/** @}
1653 cejka 724
 */