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