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