Rev 3386 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3386 | Rev 4263 | ||
---|---|---|---|
Line 1... | Line 1... | ||
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 | * Copyright (c) 2009 Martin Decky |
|
4 | * All rights reserved. |
5 | * All rights reserved. |
5 | * |
6 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * are met: |
Line 25... | Line 26... | ||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * 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 | * (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 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | */ |
29 | 30 | ||
30 | /** @addtogroup libc |
31 | /** @addtogroup generic |
31 | * @{ |
32 | * @{ |
32 | */ |
33 | */ |
33 | /** |
34 | /** |
34 | * @file |
35 | * @file |
35 | * @brief Printing functions. |
36 | * @brief Printing functions. |
36 | */ |
37 | */ |
37 | 38 | ||
38 | #include <unistd.h> |
39 | #include <unistd.h> |
39 | #include <stdio.h> |
40 | #include <stdio.h> |
40 | #include <io/printf_core.h> |
41 | #include <io/printf_core.h> |
41 | #include <ctype.h> |
42 | #include <ctype.h> |
42 | #include <string.h> |
43 | #include <string.h> |
43 | 44 | ||
- | 45 | /** show prefixes 0x or 0 */ |
|
44 | #define __PRINTF_FLAG_PREFIX 0x00000001 /**< show prefixes 0x or 0*/ |
46 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
- | 47 | /** signed / unsigned number */ |
|
45 | #define __PRINTF_FLAG_SIGNED 0x00000002 /**< signed / unsigned number */ |
48 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
- | 49 | /** print leading zeroes */ |
|
46 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 /**< print leading zeroes */ |
50 | #define __PRINTF_FLAG_ZEROPADDED 0x00000004 |
- | 51 | /** align to left */ |
|
47 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /**< align to left */ |
52 | #define __PRINTF_FLAG_LEFTALIGNED 0x00000010 |
- | 53 | /** always show + sign */ |
|
48 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 /**< always show + sign */ |
54 | #define __PRINTF_FLAG_SHOWPLUS 0x00000020 |
- | 55 | /** print space instead of plus */ |
|
49 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 /**< print space instead of plus */ |
56 | #define __PRINTF_FLAG_SPACESIGN 0x00000040 |
- | 57 | /** show big characters */ |
|
50 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 /**< show big characters */ |
58 | #define __PRINTF_FLAG_BIGCHARS 0x00000080 |
- | 59 | /** number has - sign */ |
|
51 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 /**< number has - sign */ |
60 | #define __PRINTF_FLAG_NEGATIVE 0x00000100 |
52 | 61 | ||
53 | #define PRINT_NUMBER_BUFFER_SIZE (64+5) /**< Buffer big enought for 64 bit number |
- | |
- | 62 | /** |
|
54 | * printed in base 2, sign, prefix and |
63 | * Buffer big enough for 64-bit number printed in base 2, sign, prefix and 0 |
55 | * 0 to terminate string.. (last one is only for better testing |
64 | * to terminate string... (last one is only for better testing end of buffer by |
56 | * end of buffer by zero-filling subroutine) |
65 | * zero-filling subroutine) |
57 | */ |
66 | */ |
- | 67 | #define PRINT_NUMBER_BUFFER_SIZE (64 + 5) |
|
- | 68 | ||
58 | /** Enumeration of possible arguments types. |
69 | /** Enumeration of possible arguments types. |
59 | */ |
70 | */ |
60 | typedef enum { |
71 | typedef enum { |
61 | PrintfQualifierByte = 0, |
72 | PrintfQualifierByte = 0, |
62 | PrintfQualifierShort, |
73 | PrintfQualifierShort, |
63 | PrintfQualifierInt, |
74 | PrintfQualifierInt, |
64 | PrintfQualifierLong, |
75 | PrintfQualifierLong, |
65 | PrintfQualifierLongLong, |
76 | PrintfQualifierLongLong, |
66 | PrintfQualifierSizeT, |
- | |
67 | PrintfQualifierPointer |
77 | PrintfQualifierPointer |
68 | } qualifier_t; |
78 | } qualifier_t; |
69 | 79 | ||
- | 80 | static char nullstr[] = "(NULL)"; |
|
70 | static char digits_small[] = "0123456789abcdef"; /**< Small hexadecimal characters */ |
81 | static char digits_small[] = "0123456789abcdef"; |
71 | static char digits_big[] = "0123456789ABCDEF"; /**< Big hexadecimal characters */ |
82 | static char digits_big[] = "0123456789ABCDEF"; |
- | 83 | static char invalch = U_SPECIAL; |
|
72 | 84 | ||
73 | /** Print count chars from buffer without adding newline |
85 | /** Print one or more characters without adding newline. |
- | 86 | * |
|
- | 87 | * @param buf Buffer holding characters with size of |
|
74 | * @param buf Buffer with size at least count bytes - NULL pointer NOT allowed! |
88 | * at least size bytes. NULL is not allowed! |
75 | * @param count |
89 | * @param size Size of the buffer in bytes. |
76 | * @param ps output method and its data |
90 | * @param ps Output method and its data. |
- | 91 | * |
|
77 | * @return number of printed characters |
92 | * @return Number of characters printed. |
- | 93 | * |
|
78 | */ |
94 | */ |
79 | static int printf_putnchars(const char * buf, size_t count, |
95 | static int printf_putnchars(const char *buf, size_t size, |
80 | struct printf_spec *ps) |
96 | printf_spec_t *ps) |
81 | { |
97 | { |
82 | return ps->write((void *)buf, count, ps->data); |
98 | return ps->str_write((void *) buf, size, ps->data); |
83 | } |
99 | } |
84 | 100 | ||
85 | /** Print string without added newline |
101 | /** Print one or more wide characters without adding newline. |
- | 102 | * |
|
- | 103 | * @param buf Buffer holding wide characters with size of |
|
- | 104 | * at least size bytes. NULL is not allowed! |
|
86 | * @param str string to print |
105 | * @param size Size of the buffer in bytes. |
87 | * @param ps write function specification and support data |
106 | * @param ps Output method and its data. |
- | 107 | * |
|
88 | * @return number of printed characters |
108 | * @return Number of wide characters printed. |
- | 109 | * |
|
89 | */ |
110 | */ |
90 | static int printf_putstr(const char * str, struct printf_spec *ps) |
111 | static int printf_wputnchars(const wchar_t *buf, size_t size, |
- | 112 | printf_spec_t *ps) |
|
91 | { |
113 | { |
92 | size_t count; |
- | |
93 | - | ||
94 | if (str == NULL) |
- | |
95 | return printf_putnchars("(NULL)", 6, ps); |
114 | return ps->wstr_write((void *) buf, size, ps->data); |
96 | 115 | } |
|
97 | count = strlen(str); |
- | |
98 | 116 | ||
- | 117 | /** Print string without adding a newline. |
|
- | 118 | * |
|
- | 119 | * @param str String to print. |
|
- | 120 | * @param ps Write function specification and support data. |
|
- | 121 | * |
|
- | 122 | * @return Number of characters printed. |
|
- | 123 | * |
|
- | 124 | */ |
|
- | 125 | static int printf_putstr(const char *str, printf_spec_t *ps) |
|
- | 126 | { |
|
- | 127 | if (str == NULL) |
|
- | 128 | return printf_putnchars(nullstr, str_size(nullstr), ps); |
|
- | 129 | ||
99 | return ps->write((void *) str, count, ps->data); |
130 | return ps->str_write((void *) str, str_size(str), ps->data); |
100 | } |
131 | } |
101 | 132 | ||
102 | /** Print one character to output |
133 | /** Print one ASCII character. |
- | 134 | * |
|
103 | * @param c one character |
135 | * @param c ASCII character to be printed. |
104 | * @param ps output method |
136 | * @param ps Output method. |
- | 137 | * |
|
105 | * @return number of printed characters |
138 | * @return Number of characters printed. |
- | 139 | * |
|
106 | */ |
140 | */ |
107 | static int printf_putchar(int c, struct printf_spec *ps) |
141 | static int printf_putchar(const char ch, printf_spec_t *ps) |
108 | { |
142 | { |
109 | unsigned char ch = c; |
143 | if (!ascii_check(ch)) |
- | 144 | return ps->str_write((void *) &invalch, 1, ps->data); |
|
110 | 145 | ||
111 | return ps->write((void *) &ch, 1, ps->data); |
146 | return ps->str_write(&ch, 1, ps->data); |
112 | } |
147 | } |
113 | 148 | ||
114 | /** Print one formatted character |
149 | /** Print one wide character. |
- | 150 | * |
|
115 | * @param c character to print |
151 | * @param c Wide character to be printed. |
116 | * @param width |
152 | * @param ps Output method. |
117 | * @param flags |
153 | * |
118 | * @return number of printed characters |
154 | * @return Number of characters printed. |
- | 155 | * |
|
119 | */ |
156 | */ |
120 | static int print_char(char c, int width, uint64_t flags, struct printf_spec *ps) |
157 | static int printf_putwchar(const wchar_t ch, printf_spec_t *ps) |
121 | { |
158 | { |
122 | int counter = 0; |
159 | if (!chr_check(ch)) |
- | 160 | return ps->str_write((void *) &invalch, 1, ps->data); |
|
123 | 161 | ||
- | 162 | return ps->wstr_write(&ch, sizeof(wchar_t), ps->data); |
|
- | 163 | } |
|
- | 164 | ||
- | 165 | /** Print one formatted ASCII character. |
|
- | 166 | * |
|
- | 167 | * @param ch Character to print. |
|
- | 168 | * @param width Width modifier. |
|
- | 169 | * @param flags Flags that change the way the character is printed. |
|
- | 170 | * |
|
- | 171 | * @return Number of characters printed, negative value on failure. |
|
- | 172 | * |
|
- | 173 | */ |
|
- | 174 | static int print_char(const char ch, int width, uint32_t flags, printf_spec_t *ps) |
|
- | 175 | { |
|
- | 176 | count_t counter = 0; |
|
124 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
177 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
125 | /* |
- | |
126 | * One space is consumed by the character itself, hence the |
- | |
127 | * predecrement. |
- | |
128 | */ |
- | |
129 | while (--width > 0) { |
178 | while (--width > 0) { |
- | 179 | /* |
|
- | 180 | * One space is consumed by the character itself, hence |
|
- | 181 | * the predecrement. |
|
- | 182 | */ |
|
130 | if (printf_putchar(' ', ps) > 0) |
183 | if (printf_putchar(' ', ps) > 0) |
131 | ++counter; |
184 | counter++; |
132 | } |
185 | } |
133 | } |
186 | } |
134 | 187 | ||
135 | if (printf_putchar(c, ps) > 0) |
188 | if (printf_putchar(ch, ps) > 0) |
136 | counter++; |
189 | counter++; |
137 | /* |
190 | |
138 | * One space is consumed by the character itself, hence the |
- | |
139 | * predecrement. |
- | |
140 | */ |
- | |
141 | while (--width > 0) { |
191 | while (--width > 0) { |
- | 192 | /* |
|
- | 193 | * One space is consumed by the character itself, hence |
|
- | 194 | * the predecrement. |
|
- | 195 | */ |
|
142 | if (printf_putchar(' ', ps) > 0) |
196 | if (printf_putchar(' ', ps) > 0) |
143 | ++counter; |
197 | counter++; |
144 | } |
198 | } |
145 | 199 | ||
146 | return ++counter; |
200 | return (int) (counter + 1); |
147 | } |
201 | } |
148 | 202 | ||
149 | /** Print one string |
203 | /** Print one formatted wide character. |
- | 204 | * |
|
150 | * @param s string |
205 | * @param ch Character to print. |
151 | * @param width |
206 | * @param width Width modifier. |
152 | * @param precision |
207 | * @param flags Flags that change the way the character is printed. |
153 | * @param flags |
208 | * |
154 | * @return number of printed characters |
209 | * @return Number of characters printed, negative value on failure. |
- | 210 | * |
|
155 | */ |
211 | */ |
156 | static int print_string(char *s, int width, int precision, uint64_t flags, |
212 | static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps) |
157 | struct printf_spec *ps) |
- | |
158 | { |
213 | { |
159 | int counter = 0; |
214 | count_t counter = 0; |
- | 215 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
|
- | 216 | while (--width > 0) { |
|
- | 217 | /* |
|
- | 218 | * One space is consumed by the character itself, hence |
|
- | 219 | * the predecrement. |
|
- | 220 | */ |
|
- | 221 | if (printf_putchar(' ', ps) > 0) |
|
160 | size_t size; |
222 | counter++; |
- | 223 | } |
|
- | 224 | } |
|
- | 225 | ||
- | 226 | if (printf_putwchar(ch, ps) > 0) |
|
161 | int retval; |
227 | counter++; |
162 | 228 | ||
163 | if (s == NULL) { |
229 | while (--width > 0) { |
- | 230 | /* |
|
- | 231 | * One space is consumed by the character itself, hence |
|
- | 232 | * the predecrement. |
|
- | 233 | */ |
|
164 | return printf_putstr("(NULL)", ps); |
234 | if (printf_putchar(' ', ps) > 0) |
- | 235 | counter++; |
|
165 | } |
236 | } |
166 | 237 | ||
167 | size = strlen(s); |
238 | return (int) (counter + 1); |
- | 239 | } |
|
168 | 240 | ||
169 | /* print leading spaces */ |
241 | /** Print string. |
- | 242 | * |
|
- | 243 | * @param str String to be printed. |
|
- | 244 | * @param width Width modifier. |
|
- | 245 | * @param precision Precision modifier. |
|
- | 246 | * @param flags Flags that modify the way the string is printed. |
|
- | 247 | * |
|
- | 248 | * @return Number of characters printed, negative value on failure. |
|
- | 249 | */ |
|
- | 250 | static int print_str(char *str, int width, unsigned int precision, |
|
- | 251 | uint32_t flags, printf_spec_t *ps) |
|
- | 252 | { |
|
- | 253 | if (str == NULL) |
|
- | 254 | return printf_putstr(nullstr, ps); |
|
170 | 255 | ||
- | 256 | /* Print leading spaces. */ |
|
- | 257 | count_t strw = str_length(str); |
|
171 | if (precision == 0) |
258 | if (precision == 0) |
172 | precision = size; |
259 | precision = strw; |
173 | 260 | ||
- | 261 | /* Left padding */ |
|
- | 262 | count_t counter = 0; |
|
174 | width -= precision; |
263 | width -= precision; |
175 | - | ||
176 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
264 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
177 | while (width-- > 0) { |
265 | while (width-- > 0) { |
178 | if (printf_putchar(' ', ps) == 1) |
266 | if (printf_putchar(' ', ps) == 1) |
179 | counter++; |
267 | counter++; |
180 | } |
268 | } |
181 | } |
269 | } |
182 | 270 | ||
183 | if ((retval = printf_putnchars(s, size < precision ? size : precision, |
271 | /* Part of @a str fitting into the alloted space. */ |
184 | ps)) < 0) { |
272 | int retval; |
- | 273 | size_t size = str_lsize(str, precision); |
|
- | 274 | if ((retval = printf_putnchars(str, size, ps)) < 0) |
|
185 | return -counter; |
275 | return -counter; |
186 | } |
- | |
187 | 276 | ||
188 | counter += retval; |
277 | counter += retval; |
189 | 278 | ||
- | 279 | /* Right padding */ |
|
190 | while (width-- > 0) { |
280 | while (width-- > 0) { |
191 | if (printf_putchar(' ', ps) == 1) |
281 | if (printf_putchar(' ', ps) == 1) |
192 | ++counter; |
282 | counter++; |
193 | } |
283 | } |
194 | 284 | ||
195 | return counter; |
285 | return ((int) counter); |
- | 286 | ||
196 | } |
287 | } |
197 | 288 | ||
- | 289 | /** Print wide string. |
|
- | 290 | * |
|
- | 291 | * @param str Wide string to be printed. |
|
- | 292 | * @param width Width modifier. |
|
- | 293 | * @param precision Precision modifier. |
|
- | 294 | * @param flags Flags that modify the way the string is printed. |
|
- | 295 | * |
|
- | 296 | * @return Number of wide characters printed, negative value on failure. |
|
- | 297 | */ |
|
- | 298 | static int print_wstr(wchar_t *str, int width, unsigned int precision, |
|
- | 299 | uint32_t flags, printf_spec_t *ps) |
|
- | 300 | { |
|
- | 301 | if (str == NULL) |
|
- | 302 | return printf_putstr(nullstr, ps); |
|
- | 303 | ||
- | 304 | if (*str == U_BOM) |
|
- | 305 | str++; |
|
- | 306 | ||
- | 307 | /* Print leading spaces. */ |
|
- | 308 | size_t strw = wstr_length(str); |
|
- | 309 | if (precision == 0) |
|
- | 310 | precision = strw; |
|
- | 311 | ||
- | 312 | /* Left padding */ |
|
- | 313 | count_t counter = 0; |
|
- | 314 | width -= precision; |
|
- | 315 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
|
- | 316 | while (width-- > 0) { |
|
- | 317 | if (printf_putchar(' ', ps) == 1) |
|
- | 318 | counter++; |
|
- | 319 | } |
|
- | 320 | } |
|
- | 321 | ||
- | 322 | /* Part of @a wstr fitting into the alloted space. */ |
|
- | 323 | int retval; |
|
- | 324 | size_t size = wstr_lsize(str, precision); |
|
- | 325 | if ((retval = printf_wputnchars(str, size, ps)) < 0) |
|
- | 326 | return -counter; |
|
- | 327 | ||
- | 328 | counter += retval; |
|
- | 329 | ||
- | 330 | /* Right padding */ |
|
- | 331 | while (width-- > 0) { |
|
- | 332 | if (printf_putchar(' ', ps) == 1) |
|
- | 333 | counter++; |
|
- | 334 | } |
|
198 | 335 | ||
- | 336 | return ((int) counter); |
|
- | 337 | } |
|
- | 338 | ||
199 | /** Print number in given base |
339 | /** Print a number in a given base. |
- | 340 | * |
|
- | 341 | * Print significant digits of a number in given base. |
|
200 | * |
342 | * |
201 | * Print significant digits of a number in given |
343 | * @param num Number to print. |
202 | * base. |
344 | * @param width Width modifier. |
- | 345 | * @param precision Precision modifier. |
|
- | 346 | * @param base Base to print the number in (must be between 2 and 16). |
|
- | 347 | * @param flags Flags that modify the way the number is printed. |
|
203 | * |
348 | * |
204 | * @param num Number to print. |
- | |
205 | * @param width |
- | |
206 | * @param precision |
- | |
207 | * @param base Base to print the number in (should |
- | |
208 | * be in range 2 .. 16). |
- | |
209 | * @param flags output modifiers |
- | |
210 | * @return number of printed characters |
349 | * @return Number of characters printed. |
211 | * |
350 | * |
212 | */ |
351 | */ |
213 | static int print_number(uint64_t num, int width, int precision, int base, |
352 | static int print_number(uint64_t num, int width, int precision, int base, |
214 | uint64_t flags, struct printf_spec *ps) |
353 | uint32_t flags, printf_spec_t *ps) |
215 | { |
354 | { |
216 | char *digits = digits_small; |
355 | char *digits; |
217 | char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for |
- | |
218 | * base == 2, prefix and sign */ |
- | |
219 | char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1]; |
356 | if (flags & __PRINTF_FLAG_BIGCHARS) |
220 | int size = 0; /* size of number with all prefixes and signs */ |
- | |
221 | int number_size; /* size of plain number */ |
357 | digits = digits_big; |
222 | char sgn; |
358 | else |
223 | int retval; |
- | |
224 | int counter = 0; |
359 | digits = digits_small; |
225 | 360 | ||
226 | if (flags & __PRINTF_FLAG_BIGCHARS) |
361 | char data[PRINT_NUMBER_BUFFER_SIZE]; |
- | 362 | char *ptr = &data[PRINT_NUMBER_BUFFER_SIZE - 1]; |
|
- | 363 | ||
- | 364 | /* Size of number with all prefixes and signs */ |
|
- | 365 | int size = 0; |
|
- | 366 | ||
227 | digits = digits_big; |
367 | /* Put zero at end of string */ |
- | 368 | *ptr-- = 0; |
|
228 | 369 | ||
229 | *ptr-- = 0; /* Put zero at end of string */ |
- | |
230 | - | ||
231 | if (num == 0) { |
370 | if (num == 0) { |
232 | *ptr-- = '0'; |
371 | *ptr-- = '0'; |
233 | size++; |
372 | size++; |
234 | } else { |
373 | } else { |
235 | do { |
374 | do { |
236 | *ptr-- = digits[num % base]; |
375 | *ptr-- = digits[num % base]; |
237 | size++; |
376 | size++; |
238 | } while (num /= base); |
377 | } while (num /= base); |
239 | } |
378 | } |
240 | 379 | ||
- | 380 | /* Size of plain number */ |
|
241 | number_size = size; |
381 | int number_size = size; |
242 | 382 | ||
243 | /* |
383 | /* |
244 | * Collect sum of all prefixes/signs/... to calculate padding and |
384 | * Collect the sum of all prefixes/signs/etc. to calculate padding and |
245 | * leading zeroes |
385 | * leading zeroes. |
246 | */ |
386 | */ |
247 | if (flags & __PRINTF_FLAG_PREFIX) { |
387 | if (flags & __PRINTF_FLAG_PREFIX) { |
248 | switch(base) { |
388 | switch(base) { |
- | 389 | case 2: |
|
249 | case 2: /* Binary formating is not standard, but usefull */ |
390 | /* Binary formating is not standard, but usefull */ |
250 | size += 2; |
391 | size += 2; |
251 | break; |
392 | break; |
252 | case 8: |
393 | case 8: |
253 | size++; |
394 | size++; |
254 | break; |
395 | break; |
255 | case 16: |
396 | case 16: |
256 | size += 2; |
397 | size += 2; |
257 | break; |
398 | break; |
258 | } |
399 | } |
259 | } |
400 | } |
260 | 401 | ||
261 | sgn = 0; |
402 | char sgn = 0; |
262 | if (flags & __PRINTF_FLAG_SIGNED) { |
403 | if (flags & __PRINTF_FLAG_SIGNED) { |
263 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
404 | if (flags & __PRINTF_FLAG_NEGATIVE) { |
264 | sgn = '-'; |
405 | sgn = '-'; |
265 | size++; |
406 | size++; |
266 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
407 | } else if (flags & __PRINTF_FLAG_SHOWPLUS) { |
Line 269... | Line 410... | ||
269 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
410 | } else if (flags & __PRINTF_FLAG_SPACESIGN) { |
270 | sgn = ' '; |
411 | sgn = ' '; |
271 | size++; |
412 | size++; |
272 | } |
413 | } |
273 | } |
414 | } |
274 | 415 | ||
275 | if (flags & __PRINTF_FLAG_LEFTALIGNED) { |
416 | if (flags & __PRINTF_FLAG_LEFTALIGNED) |
276 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
417 | flags &= ~__PRINTF_FLAG_ZEROPADDED; |
277 | } |
418 | |
278 | - | ||
279 | /* |
419 | /* |
280 | * If number is leftaligned or precision is specified then zeropadding |
420 | * If the number is left-aligned or precision is specified then |
281 | * is ignored. |
421 | * padding with zeros is ignored. |
282 | */ |
422 | */ |
283 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
423 | if (flags & __PRINTF_FLAG_ZEROPADDED) { |
284 | if ((precision == 0) && (width > size)) { |
424 | if ((precision == 0) && (width > size)) |
285 | precision = width - size + number_size; |
425 | precision = width - size + number_size; |
286 | } |
- | |
287 | } |
426 | } |
288 | 427 | ||
289 | /* print leading spaces */ |
428 | /* Print leading spaces */ |
290 | - | ||
291 | /* We must print whole number not only a part. */ |
- | |
292 | if (number_size > precision) |
429 | if (number_size > precision) { |
- | 430 | /* Print the whole number, not only a part */ |
|
293 | precision = number_size; |
431 | precision = number_size; |
- | 432 | } |
|
294 | 433 | ||
295 | width -= precision + size - number_size; |
434 | width -= precision + size - number_size; |
- | 435 | count_t counter = 0; |
|
296 | 436 | ||
297 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
437 | if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) { |
298 | while (width-- > 0) { |
438 | while (width-- > 0) { |
299 | if (printf_putchar(' ', ps) == 1) |
439 | if (printf_putchar(' ', ps) == 1) |
300 | counter++; |
440 | counter++; |
301 | } |
441 | } |
302 | } |
442 | } |
303 | 443 | ||
304 | /* print sign */ |
444 | /* Print sign */ |
305 | if (sgn) { |
445 | if (sgn) { |
306 | if (printf_putchar(sgn, ps) == 1) |
446 | if (printf_putchar(sgn, ps) == 1) |
307 | counter++; |
447 | counter++; |
308 | } |
448 | } |
309 | 449 | ||
310 | /* print prefix */ |
450 | /* Print prefix */ |
311 | - | ||
312 | if (flags & __PRINTF_FLAG_PREFIX) { |
451 | if (flags & __PRINTF_FLAG_PREFIX) { |
313 | switch(base) { |
452 | switch(base) { |
- | 453 | case 2: |
|
314 | case 2: /* Binary formating is not standard, but usefull */ |
454 | /* Binary formating is not standard, but usefull */ |
315 | if (printf_putchar('0', ps) == 1) |
455 | if (printf_putchar('0', ps) == 1) |
316 | counter++; |
456 | counter++; |
317 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
457 | if (flags & __PRINTF_FLAG_BIGCHARS) { |
318 | if (printf_putchar('B', ps) == 1) |
458 | if (printf_putchar('B', ps) == 1) |
319 | counter++; |
459 | counter++; |
Line 337... | Line 477... | ||
337 | counter++; |
477 | counter++; |
338 | } |
478 | } |
339 | break; |
479 | break; |
340 | } |
480 | } |
341 | } |
481 | } |
342 | 482 | ||
343 | /* print leading zeroes */ |
483 | /* Print leading zeroes */ |
344 | precision -= number_size; |
484 | precision -= number_size; |
345 | while (precision-- > 0) { |
485 | while (precision-- > 0) { |
346 | if (printf_putchar('0', ps) == 1) |
486 | if (printf_putchar('0', ps) == 1) |
347 | counter++; |
487 | counter++; |
348 | } |
488 | } |
349 | - | ||
350 | 489 | ||
351 | /* print number itself */ |
490 | /* Print the number itself */ |
352 | 491 | int retval; |
|
353 | if ((retval = printf_putstr(++ptr, ps)) > 0) { |
492 | if ((retval = printf_putstr(++ptr, ps)) > 0) |
354 | counter += retval; |
493 | counter += retval; |
355 | } |
- | |
356 | 494 | ||
357 | /* print ending spaces */ |
495 | /* Print tailing spaces */ |
358 | 496 | ||
359 | while (width-- > 0) { |
497 | while (width-- > 0) { |
360 | if (printf_putchar(' ', ps) == 1) |
498 | if (printf_putchar(' ', ps) == 1) |
361 | counter++; |
499 | counter++; |
362 | } |
500 | } |
363 | 501 | ||
364 | return counter; |
502 | return ((int) counter); |
365 | } |
503 | } |
366 | 504 | ||
367 | - | ||
368 | /** Print formatted string. |
505 | /** Print formatted string. |
369 | * |
506 | * |
370 | * Print string formatted according to the fmt parameter and variadic arguments. |
507 | * Print string formatted according to the fmt parameter and variadic arguments. |
371 | * Each formatting directive must have the following form: |
508 | * Each formatting directive must have the following form: |
372 | * |
509 | * |
373 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
510 | * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION |
374 | * |
511 | * |
375 | * FLAGS:@n |
512 | * FLAGS:@n |
376 | * - "#" Force to print prefix. |
513 | * - "#" Force to print prefix. For \%o conversion, the prefix is 0, for |
377 | * For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and |
514 | * \%x and \%X prefixes are 0x and 0X and for conversion \%b the |
378 | * 0X and for conversion \%b the prefix is 0b. |
515 | * prefix is 0b. |
- | 516 | * |
|
- | 517 | * - "-" Align to left. |
|
379 | * |
518 | * |
380 | * - "-" Align to left. |
519 | * - "+" Print positive sign just as negative. |
381 | * |
520 | * |
- | 521 | * - " " If the printed number is positive and "+" flag is not set, |
|
382 | * - "+" Print positive sign just as negative. |
522 | * print space in place of sign. |
383 | * |
523 | * |
384 | * - " " If the printed number is positive and "+" flag is not set, |
524 | * - "0" Print 0 as padding instead of spaces. Zeroes are placed between |
- | 525 | * sign and the rest of the number. This flag is ignored if "-" |
|
385 | * print space in place of sign. |
526 | * flag is specified. |
386 | * |
527 | * |
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. |
- | |
390 | * |
- | |
391 | * WIDTH:@n |
528 | * WIDTH:@n |
392 | * - Specify minimal width of printed argument. If it is bigger, width is |
529 | * - Specify the minimal width of a printed argument. If it is bigger, |
393 | * ignored. If width is specified with a "*" character instead of number, |
530 | * width is ignored. If width is specified with a "*" character instead of |
394 | * width is taken from parameter list. And integer parameter is expected |
531 | * number, width is taken from parameter list. And integer parameter is |
395 | * before parameter for processed conversion specification. If this value |
532 | * expected before parameter for processed conversion specification. If |
396 | * is negative its absolute value is taken and the "-" flag is set. |
533 | * this value is negative its absolute value is taken and the "-" flag is |
- | 534 | * set. |
|
397 | * |
535 | * |
398 | * PRECISION:@n |
536 | * PRECISION:@n |
399 | * - Value precision. For numbers it specifies minimum valid numbers. |
537 | * - Value precision. For numbers it specifies minimum valid numbers. |
400 | * Smaller numbers are printed with leading zeroes. Bigger numbers are |
538 | * Smaller numbers are printed with leading zeroes. Bigger numbers are not |
401 | * not affected. Strings with more than precision characters are cut off. |
539 | * affected. Strings with more than precision characters are cut off. Just |
402 | * Just as with width, an "*" can be used used instead of a number. An |
540 | * as with width, an "*" can be used used instead of a number. An integer |
403 | * integer value is then expected in parameters. When both width and |
541 | * value is then expected in parameters. When both width and precision are |
404 | * precision are specified using "*", the first parameter is used for |
542 | * specified using "*", the first parameter is used for width and the |
405 | * width and the second one for precision. |
543 | * second one for precision. |
406 | * |
544 | * |
407 | * TYPE:@n |
545 | * TYPE:@n |
408 | * - "hh" Signed or unsigned char.@n |
546 | * - "hh" Signed or unsigned char.@n |
409 | * - "h" Signed or usigned short.@n |
547 | * - "h" Signed or unsigned short.@n |
410 | * - "" Signed or usigned int (default value).@n |
548 | * - "" Signed or unsigned int (default value).@n |
411 | * - "l" Signed or usigned long int.@n |
549 | * - "l" Signed or unsigned long int.@n |
- | 550 | * If conversion is "c", the character is wchar_t (wide character).@n |
|
- | 551 | * If conversion is "s", the string is wchar_t * (wide string).@n |
|
412 | * - "ll" Signed or usigned long long int.@n |
552 | * - "ll" Signed or unsigned long long int.@n |
413 | * - "z" Type size_t.@n |
- | |
414 | * |
- | |
415 | * |
553 | * |
416 | * CONVERSION:@n |
554 | * CONVERSION:@n |
417 | * - % Print percentile character itself. |
555 | * - % Print percentile character itself. |
- | 556 | * |
|
- | 557 | * - c Print single character. The character is expected to be plain |
|
- | 558 | * ASCII (e.g. only values 0 .. 127 are valid).@n |
|
- | 559 | * If type is "l", then the character is expected to be wide character |
|
- | 560 | * (e.g. values 0 .. 0x10ffff are valid). |
|
- | 561 | * |
|
- | 562 | * - s Print zero terminated string. If a NULL value is passed as |
|
- | 563 | * value, "(NULL)" is printed instead.@n |
|
- | 564 | * If type is "l", then the string is expected to be wide string. |
|
- | 565 | * |
|
- | 566 | * - P, p Print value of a pointer. Void * value is expected and it is |
|
- | 567 | * printed in hexadecimal notation with prefix (as with \%#X / \%#x |
|
- | 568 | * for 32-bit or \%#X / \%#x for 64-bit long pointers). |
|
- | 569 | * |
|
- | 570 | * - b Print value as unsigned binary number. Prefix is not printed by |
|
- | 571 | * default. (Nonstandard extension.) |
|
- | 572 | * |
|
- | 573 | * - o Print value as unsigned octal number. Prefix is not printed by |
|
- | 574 | * default. |
|
- | 575 | * |
|
- | 576 | * - d, i Print signed decimal number. There is no difference between d |
|
- | 577 | * and i conversion. |
|
- | 578 | * |
|
- | 579 | * - u Print unsigned decimal number. |
|
418 | * |
580 | * |
- | 581 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
|
419 | * - c Print single character. |
582 | * not printed by default. |
420 | * |
583 | * |
421 | * - s Print zero terminated string. If a NULL value is passed as |
- | |
422 | * value, "(NULL)" is printed instead. |
- | |
423 | * |
- | |
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). |
- | |
427 | * |
- | |
428 | * - b Print value as unsigned binary number. Prefix is not printed by |
- | |
429 | * default. (Nonstandard extension.) |
- | |
430 | * |
- | |
431 | * - o Print value as unsigned octal number. Prefix is not printed by |
- | |
432 | * default. |
- | |
433 | * |
- | |
434 | * - d, i Print signed decimal number. There is no difference between d |
- | |
435 | * and i conversion. |
- | |
436 | * |
- | |
437 | * - u Print unsigned decimal number. |
- | |
438 | * |
- | |
439 | * - X, x Print hexadecimal number with upper- or lower-case. Prefix is |
- | |
440 | * not printed by default. |
- | |
441 | * |
- | |
442 | * All other characters from fmt except the formatting directives are printed in |
584 | * All other characters from fmt except the formatting directives are printed |
443 | * verbatim. |
585 | * verbatim. |
444 | * |
586 | * |
445 | * @param fmt Formatting NULL terminated string. |
587 | * @param fmt Format NULL-terminated string. |
- | 588 | * |
|
446 | * @return Number of printed characters or negative value on failure. |
589 | * @return Number of characters printed, negative value on failure. |
- | 590 | * |
|
447 | */ |
591 | */ |
448 | int printf_core(const char *fmt, struct printf_spec *ps, va_list ap) |
592 | int printf_core(const char *fmt, printf_spec_t *ps, va_list ap) |
449 | { |
593 | { |
450 | /* i is the index of the currently processed char from fmt */ |
594 | size_t i; /* Index of the currently processed character from fmt */ |
451 | int i = 0; |
595 | size_t nxt = 0; /* Index of the next character from fmt */ |
452 | /* j is the index to the first not printed nonformating character */ |
596 | size_t j = 0; /* Index to the first not printed nonformating character */ |
453 | int j = 0; |
- | |
454 | 597 | ||
455 | int end; |
- | |
456 | int counter; /* counter of printed characters */ |
598 | count_t counter = 0; /* Number of characters printed */ |
457 | int retval; /* used to store return values from called functions */ |
599 | int retval; /* Return values from nested functions */ |
458 | char c; |
600 | |
459 | qualifier_t qualifier; /* type of argument */ |
601 | while (true) { |
460 | int base; /* base in which will be a numeric parameter printed */ |
- | |
461 | uint64_t number; /* argument value */ |
602 | i = nxt; |
462 | size_t size; /* byte size of integer parameter */ |
603 | wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
463 | int width, precision; |
- | |
464 | uint64_t flags; |
- | |
465 | 604 | ||
466 | counter = 0; |
605 | if (uc == 0) |
- | 606 | break; |
|
467 | 607 | ||
468 | while ((c = fmt[i])) { |
- | |
469 | /* control character */ |
608 | /* Control character */ |
470 | if (c == '%' ) { |
609 | if (uc == '%') { |
471 | /* print common characters if any processed */ |
610 | /* Print common characters if any processed */ |
472 | if (i > j) { |
611 | if (i > j) { |
473 | if ((retval = printf_putnchars(&fmt[j], |
612 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
474 | (size_t)(i - j), ps)) < 0) { /* error */ |
613 | /* Error */ |
- | 614 | counter = -counter; |
|
475 | goto minus_out; |
615 | goto out; |
476 | } |
616 | } |
477 | counter += retval; |
617 | counter += retval; |
478 | } |
618 | } |
479 | 619 | ||
480 | j = i; |
620 | j = i; |
- | 621 | ||
481 | /* parse modifiers */ |
622 | /* Parse modifiers */ |
482 | flags = 0; |
623 | uint32_t flags = 0; |
483 | end = 0; |
624 | bool end = false; |
484 | 625 | ||
485 | do { |
626 | do { |
486 | ++i; |
627 | i = nxt; |
- | 628 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
487 | switch (c = fmt[i]) { |
629 | switch (uc) { |
488 | case '#': |
630 | case '#': |
489 | flags |= __PRINTF_FLAG_PREFIX; |
631 | flags |= __PRINTF_FLAG_PREFIX; |
490 | break; |
632 | break; |
491 | case '-': |
633 | case '-': |
492 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
634 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
Line 499... | Line 641... | ||
499 | break; |
641 | break; |
500 | case '0': |
642 | case '0': |
501 | flags |= __PRINTF_FLAG_ZEROPADDED; |
643 | flags |= __PRINTF_FLAG_ZEROPADDED; |
502 | break; |
644 | break; |
503 | default: |
645 | default: |
504 | end = 1; |
646 | end = true; |
505 | }; |
647 | }; |
506 | - | ||
507 | } while (end == 0); |
648 | } while (!end); |
508 | 649 | ||
509 | /* width & '*' operator */ |
650 | /* Width & '*' operator */ |
510 | width = 0; |
651 | int width = 0; |
511 | if (isdigit(fmt[i])) { |
652 | if (isdigit(uc)) { |
512 | while (isdigit(fmt[i])) { |
653 | while (true) { |
513 | width *= 10; |
654 | width *= 10; |
514 | width += fmt[i++] - '0'; |
655 | width += uc - '0'; |
- | 656 | ||
- | 657 | i = nxt; |
|
- | 658 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
- | 659 | if (uc == 0) |
|
- | 660 | break; |
|
- | 661 | if (!isdigit(uc)) |
|
- | 662 | break; |
|
515 | } |
663 | } |
516 | } else if (fmt[i] == '*') { |
664 | } else if (uc == '*') { |
517 | /* get width value from argument list*/ |
665 | /* Get width value from argument list */ |
518 | i++; |
666 | i = nxt; |
- | 667 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
519 | width = (int)va_arg(ap, int); |
668 | width = (int) va_arg(ap, int); |
520 | if (width < 0) { |
669 | if (width < 0) { |
521 | /* negative width sets '-' flag */ |
670 | /* Negative width sets '-' flag */ |
522 | width *= -1; |
671 | width *= -1; |
523 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
672 | flags |= __PRINTF_FLAG_LEFTALIGNED; |
524 | } |
673 | } |
525 | } |
674 | } |
526 | 675 | ||
527 | /* precision and '*' operator */ |
676 | /* Precision and '*' operator */ |
528 | precision = 0; |
677 | int precision = 0; |
529 | if (fmt[i] == '.') { |
678 | if (uc == '.') { |
530 | ++i; |
679 | i = nxt; |
- | 680 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
531 | if (isdigit(fmt[i])) { |
681 | if (isdigit(uc)) { |
532 | while (isdigit(fmt[i])) { |
682 | while (true) { |
533 | precision *= 10; |
683 | precision *= 10; |
534 | precision += fmt[i++] - '0'; |
684 | precision += uc - '0'; |
- | 685 | ||
- | 686 | i = nxt; |
|
- | 687 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
- | 688 | if (uc == 0) |
|
- | 689 | break; |
|
- | 690 | if (!isdigit(uc)) |
|
- | 691 | break; |
|
535 | } |
692 | } |
536 | } else if (fmt[i] == '*') { |
693 | } else if (uc == '*') { |
537 | /* get precision value from argument */ |
694 | /* Get precision value from the argument list */ |
538 | i++; |
695 | i = nxt; |
- | 696 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
539 | precision = (int)va_arg(ap, int); |
697 | precision = (int) va_arg(ap, int); |
540 | if (precision < 0) { |
698 | if (precision < 0) { |
541 | /* negative precision ignored */ |
699 | /* Ignore negative precision */ |
542 | precision = 0; |
700 | precision = 0; |
543 | } |
701 | } |
544 | } |
702 | } |
545 | } |
703 | } |
- | 704 | ||
- | 705 | qualifier_t qualifier; |
|
546 | 706 | ||
547 | switch (fmt[i++]) { |
707 | switch (uc) { |
548 | /** @todo unimplemented qualifiers: |
708 | /** @todo Unimplemented qualifiers: |
549 | * t ptrdiff_t - ISO C 99 |
709 | * t ptrdiff_t - ISO C 99 |
550 | */ |
710 | */ |
- | 711 | case 'h': |
|
551 | case 'h': /* char or short */ |
712 | /* Char or short */ |
552 | qualifier = PrintfQualifierShort; |
713 | qualifier = PrintfQualifierShort; |
- | 714 | i = nxt; |
|
- | 715 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
553 | if (fmt[i] == 'h') { |
716 | if (uc == 'h') { |
554 | i++; |
717 | i = nxt; |
- | 718 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
555 | qualifier = PrintfQualifierByte; |
719 | qualifier = PrintfQualifierByte; |
556 | } |
720 | } |
557 | break; |
721 | break; |
- | 722 | case 'l': |
|
558 | case 'l': /* long or long long*/ |
723 | /* Long or long long */ |
559 | qualifier = PrintfQualifierLong; |
724 | qualifier = PrintfQualifierLong; |
- | 725 | i = nxt; |
|
- | 726 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
560 | if (fmt[i] == 'l') { |
727 | if (uc == 'l') { |
561 | i++; |
728 | i = nxt; |
- | 729 | uc = str_decode(fmt, &nxt, STR_NO_LIMIT); |
|
562 | qualifier = PrintfQualifierLongLong; |
730 | qualifier = PrintfQualifierLongLong; |
563 | } |
731 | } |
564 | break; |
732 | break; |
565 | case 'z': /* size_t */ |
- | |
566 | qualifier = PrintfQualifierSizeT; |
- | |
567 | break; |
- | |
568 | default: |
733 | default: |
569 | /* set default type */ |
734 | /* Default type */ |
570 | qualifier = PrintfQualifierInt; |
735 | qualifier = PrintfQualifierInt; |
571 | --i; |
- | |
572 | } |
736 | } |
573 | 737 | ||
574 | base = 10; |
738 | unsigned int base = 10; |
575 | 739 | ||
576 | switch (c = fmt[i]) { |
740 | switch (uc) { |
577 | - | ||
578 | /* |
741 | /* |
579 | * String and character conversions. |
742 | * String and character conversions. |
580 | */ |
743 | */ |
581 | case 's': |
744 | case 's': |
- | 745 | if (qualifier == PrintfQualifierLong) |
|
582 | if ((retval = print_string(va_arg(ap, char*), |
746 | retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps); |
- | 747 | else |
|
583 | width, precision, flags, ps)) < 0) { |
748 | retval = print_str(va_arg(ap, char *), width, precision, flags, ps); |
- | 749 | ||
- | 750 | if (retval < 0) { |
|
- | 751 | counter = -counter; |
|
584 | goto minus_out; |
752 | goto out; |
585 | } |
753 | } |
586 | 754 | ||
587 | counter += retval; |
755 | counter += retval; |
588 | j = i + 1; |
756 | j = nxt; |
589 | goto next_char; |
757 | goto next_char; |
590 | case 'c': |
758 | case 'c': |
591 | c = va_arg(ap, unsigned int); |
759 | if (qualifier == PrintfQualifierLong) |
592 | retval = print_char(c, width, flags, ps); |
760 | retval = print_wchar(va_arg(ap, wchar_t), width, flags, ps); |
- | 761 | else |
|
- | 762 | retval = print_char(va_arg(ap, unsigned int), width, flags, ps); |
|
- | 763 | ||
593 | if (retval < 0) { |
764 | if (retval < 0) { |
- | 765 | counter = -counter; |
|
594 | goto minus_out; |
766 | goto out; |
595 | } |
767 | }; |
596 | 768 | ||
597 | counter += retval; |
769 | counter += retval; |
598 | j = i + 1; |
770 | j = nxt; |
599 | goto next_char; |
771 | goto next_char; |
600 | 772 | ||
601 | /* |
773 | /* |
602 | * Integer values |
774 | * Integer values |
603 | */ |
775 | */ |
- | 776 | case 'P': |
|
604 | case 'P': /* pointer */ |
777 | /* Pointer */ |
605 | flags |= __PRINTF_FLAG_BIGCHARS; |
778 | flags |= __PRINTF_FLAG_BIGCHARS; |
606 | case 'p': |
779 | case 'p': |
607 | flags |= __PRINTF_FLAG_PREFIX; |
780 | flags |= __PRINTF_FLAG_PREFIX; |
608 | base = 16; |
781 | base = 16; |
609 | qualifier = PrintfQualifierPointer; |
782 | qualifier = PrintfQualifierPointer; |
610 | break; |
783 | break; |
611 | case 'b': |
784 | case 'b': |
612 | base = 2; |
785 | base = 2; |
613 | break; |
786 | break; |
614 | case 'o': |
787 | case 'o': |
615 | base = 8; |
788 | base = 8; |
616 | break; |
789 | break; |
617 | case 'd': |
790 | case 'd': |
618 | case 'i': |
791 | case 'i': |
619 | flags |= __PRINTF_FLAG_SIGNED; |
792 | flags |= __PRINTF_FLAG_SIGNED; |
620 | case 'u': |
793 | case 'u': |
621 | break; |
794 | break; |
622 | case 'X': |
795 | case 'X': |
623 | flags |= __PRINTF_FLAG_BIGCHARS; |
796 | flags |= __PRINTF_FLAG_BIGCHARS; |
624 | case 'x': |
797 | case 'x': |
625 | base = 16; |
798 | base = 16; |
626 | break; |
799 | break; |
- | 800 | ||
627 | /* percentile itself */ |
801 | /* Percentile itself */ |
628 | case '%': |
802 | case '%': |
629 | j = i; |
803 | j = i; |
630 | goto next_char; |
804 | goto next_char; |
- | 805 | ||
631 | /* |
806 | /* |
632 | * Bad formatting. |
807 | * Bad formatting. |
633 | */ |
808 | */ |
634 | default: |
809 | default: |
635 | /* |
810 | /* |
636 | * Unknown format. Now, j is the index of '%', |
811 | * Unknown format. Now, j is the index of '%' |
637 | * so we will print the whole bad format |
812 | * so we will print whole bad format sequence. |
638 | * sequence. |
- | |
639 | */ |
813 | */ |
640 | goto next_char; |
814 | goto next_char; |
641 | } |
815 | } |
642 | - | ||
643 | 816 | ||
644 | /* Print integers */ |
817 | /* Print integers */ |
- | 818 | size_t size; |
|
- | 819 | uint64_t number; |
|
645 | switch (qualifier) { |
820 | switch (qualifier) { |
646 | case PrintfQualifierByte: |
821 | case PrintfQualifierByte: |
647 | size = sizeof(unsigned char); |
822 | size = sizeof(unsigned char); |
648 | number = (uint64_t)va_arg(ap, unsigned int); |
823 | number = (uint64_t) va_arg(ap, unsigned int); |
649 | break; |
824 | break; |
650 | case PrintfQualifierShort: |
825 | case PrintfQualifierShort: |
651 | size = sizeof(unsigned short); |
826 | size = sizeof(unsigned short); |
652 | number = (uint64_t)va_arg(ap, unsigned int); |
827 | number = (uint64_t) va_arg(ap, unsigned int); |
653 | break; |
828 | break; |
654 | case PrintfQualifierInt: |
829 | case PrintfQualifierInt: |
655 | size = sizeof(unsigned int); |
830 | size = sizeof(unsigned int); |
656 | number = (uint64_t)va_arg(ap, unsigned int); |
831 | number = (uint64_t) va_arg(ap, unsigned int); |
657 | break; |
832 | break; |
658 | case PrintfQualifierLong: |
833 | case PrintfQualifierLong: |
659 | size = sizeof(unsigned long); |
834 | size = sizeof(unsigned long); |
660 | number = (uint64_t)va_arg(ap, unsigned long); |
835 | number = (uint64_t) va_arg(ap, unsigned long); |
661 | break; |
836 | break; |
662 | case PrintfQualifierLongLong: |
837 | case PrintfQualifierLongLong: |
663 | size = sizeof(unsigned long long); |
838 | size = sizeof(unsigned long long); |
664 | number = (uint64_t)va_arg(ap, |
839 | number = (uint64_t) va_arg(ap, unsigned long long); |
665 | unsigned long long); |
- | |
666 | break; |
840 | break; |
667 | case PrintfQualifierPointer: |
841 | case PrintfQualifierPointer: |
668 | size = sizeof(void *); |
842 | size = sizeof(void *); |
669 | number = (uint64_t)(unsigned long)va_arg(ap, |
843 | number = (uint64_t) (unsigned long) va_arg(ap, void *); |
670 | void *); |
- | |
671 | break; |
- | |
672 | case PrintfQualifierSizeT: |
- | |
673 | size = sizeof(size_t); |
- | |
674 | number = (uint64_t)va_arg(ap, size_t); |
- | |
675 | break; |
844 | break; |
- | 845 | default: |
|
676 | default: /* Unknown qualifier */ |
846 | /* Unknown qualifier */ |
677 | goto minus_out; |
847 | counter = -counter; |
678 | 848 | goto out; |
|
679 | } |
849 | } |
680 | 850 | ||
681 | if (flags & __PRINTF_FLAG_SIGNED) { |
851 | if (flags & __PRINTF_FLAG_SIGNED) { |
682 | if (number & (0x1 << (size * 8 - 1))) { |
852 | if (number & (0x1 << (size * 8 - 1))) { |
683 | flags |= __PRINTF_FLAG_NEGATIVE; |
853 | flags |= __PRINTF_FLAG_NEGATIVE; |
684 | 854 | ||
685 | if (size == sizeof(uint64_t)) { |
855 | if (size == sizeof(uint64_t)) { |
686 | number = -((int64_t)number); |
856 | number = -((int64_t) number); |
687 | } else { |
857 | } else { |
688 | number = ~number; |
858 | number = ~number; |
689 | number &= |
859 | number &= |
690 | ~(0xFFFFFFFFFFFFFFFFll << |
860 | ~(0xFFFFFFFFFFFFFFFFll << |
691 | (size * 8)); |
861 | (size * 8)); |
692 | number++; |
862 | number++; |
693 | } |
863 | } |
694 | } |
864 | } |
695 | } |
865 | } |
696 | 866 | ||
697 | if ((retval = print_number(number, width, precision, |
867 | if ((retval = print_number(number, width, precision, |
698 | base, flags, ps)) < 0 ) { |
868 | base, flags, ps)) < 0) { |
- | 869 | counter = -counter; |
|
699 | goto minus_out; |
870 | goto out; |
700 | } |
871 | } |
701 | 872 | ||
702 | counter += retval; |
873 | counter += retval; |
703 | j = i + 1; |
874 | j = nxt; |
704 | } |
875 | } |
705 | next_char: |
876 | next_char: |
706 | 877 | ; |
|
707 | ++i; |
- | |
708 | } |
878 | } |
709 | 879 | ||
710 | if (i > j) { |
880 | if (i > j) { |
711 | retval = printf_putnchars(&fmt[j], (size_t)(i - j), ps); |
881 | if ((retval = printf_putnchars(&fmt[j], i - j, ps)) < 0) { |
712 | if (retval < 0) { /* error */ |
882 | /* Error */ |
- | 883 | counter = -counter; |
|
713 | goto minus_out; |
884 | goto out; |
714 | } |
885 | } |
715 | counter += retval; |
886 | counter += retval; |
716 | } |
887 | } |
717 | 888 | ||
718 | return counter; |
- | |
719 | minus_out: |
889 | out: |
720 | return -counter; |
890 | return ((int) counter); |
721 | } |
891 | } |
722 | 892 | ||
723 | /** @} |
893 | /** @} |
724 | */ |
894 | */ |