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