Subversion Repositories HelenOS

Rev

Rev 1272 | Rev 1616 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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