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