Rev 995 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 995 | Rev 1010 | ||
---|---|---|---|
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 | 34 | ||
35 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
35 | #define __PRINTF_FLAG_PREFIX 0x00000001 |
36 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
36 | #define __PRINTF_FLAG_SIGNED 0x00000002 |
37 | 37 | ||
38 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */ |
38 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */ |
39 | 39 | ||
40 | /** Print hexadecimal digits |
40 | /** Print hexadecimal digits |
41 | * |
41 | * |
42 | * Print fixed count of hexadecimal digits from |
42 | * Print fixed count of hexadecimal digits from |
43 | * the number num. The digits are printed in |
43 | * the number num. The digits are printed in |
44 | * natural left-to-right order starting with |
44 | * natural left-to-right order starting with |
45 | * the width-th digit. |
45 | * the width-th digit. |
46 | * |
46 | * |
47 | * @param num Number containing digits. |
47 | * @param num Number containing digits. |
48 | * @param width Count of digits to print. |
48 | * @param width Count of digits to print. |
49 | * |
49 | * |
50 | */ |
50 | */ |
51 | static int print_fixed_hex(const uint64_t num, const int width, uint64_t flags) |
51 | static int print_fixed_hex(const uint64_t num, const int width, uint64_t flags) |
52 | { |
52 | { |
53 | int i; |
53 | int i; |
54 | char buf[18]; /* 16 bytes for number + 2 for optionaly prefix */ |
54 | char buf[18]; /* 16 bytes for number + 2 for optionaly prefix */ |
55 | char *bptr; |
55 | char *bptr; |
56 | 56 | ||
57 | bptr = buf; |
57 | bptr = buf; |
58 | 58 | ||
59 | if (flags & __PRINTF_FLAG_PREFIX) { |
59 | if (flags & __PRINTF_FLAG_PREFIX) { |
60 | buf[0] = '0'; |
60 | buf[0] = '0'; |
61 | buf[1] = 'x'; |
61 | buf[1] = 'x'; |
62 | bptr += 2; |
62 | bptr += 2; |
63 | } |
63 | } |
64 | 64 | ||
65 | for (i = width*8 - 4; i >= 0; i -= 4) |
65 | for (i = width*8 - 4; i >= 0; i -= 4) |
66 | *bptr++ = digits[(num>>i) & 0xf]; |
66 | *bptr++ = digits[(num>>i) & 0xf]; |
67 | *bptr = '\0'; |
67 | *bptr = '\0'; |
68 | 68 | ||
69 | return putstr(buf); |
69 | return putstr(buf); |
70 | } |
70 | } |
71 | 71 | ||
72 | 72 | ||
73 | /** Print number in given base |
73 | /** Print number in given base |
74 | * |
74 | * |
75 | * Print significant digits of a number in given |
75 | * Print significant digits of a number in given |
76 | * base. |
76 | * base. |
77 | * |
77 | * |
78 | * @param num Number to print. |
78 | * @param num Number to print. |
79 | * @param base Base to print the number in (should |
79 | * @param base Base to print the number in (should |
80 | * be in range 2 .. 16). |
80 | * be in range 2 .. 16). |
81 | * |
81 | * |
82 | */ |
82 | */ |
83 | static int print_number(const unsigned long num, const unsigned int base, uint64_t flags) |
83 | static int print_number(const unsigned long num, const unsigned int base, uint64_t flags) |
84 | { |
84 | { |
85 | int val = num; |
85 | int val = num; |
86 | char d[sizeof(unsigned long)*8+1]; /* this is good enough even for base == 2 */ |
86 | char d[sizeof(unsigned long)*8+1]; /* this is good enough even for base == 2 */ |
87 | int i = sizeof(unsigned long)*8-1; |
87 | int i = sizeof(unsigned long)*8-1; |
88 | 88 | ||
89 | /* FIXME: if signed, print sign */ |
89 | /* FIXME: if signed, print sign */ |
90 | 90 | ||
91 | do { |
91 | do { |
92 | d[i--] = digits[val % base]; |
92 | d[i--] = digits[val % base]; |
93 | } while (val /= base); |
93 | } while (val /= base); |
94 | 94 | ||
95 | d[sizeof(unsigned long)*8] = 0; |
95 | d[sizeof(unsigned long)*8] = 0; |
96 | 96 | ||
97 | return putstr(&d[i + 1]); |
97 | return putstr(&d[i + 1]); |
98 | } |
98 | } |
99 | 99 | ||
100 | 100 | ||
101 | 101 | ||
102 | /** General formatted text print |
102 | /** General formatted text print |
103 | * |
103 | * |
104 | * Print text formatted according the fmt parameter |
104 | * Print text formatted according the fmt parameter |
105 | * and variant arguments. Each formatting directive |
105 | * and variant arguments. Each formatting directive |
106 | * begins with \% (percentage) character and one of the |
106 | * begins with \% (percentage) character and one of the |
107 | * following character: |
107 | * following character: |
108 | * |
108 | * |
109 | * \% Prints the percentage character. |
109 | * \% Prints the percentage character. |
110 | * |
110 | * |
111 | * s The next variant argument is treated as char* |
111 | * s The next variant argument is treated as char* |
112 | * and printed as a NULL terminated string. |
112 | * and printed as a NULL terminated string. |
113 | * |
113 | * |
114 | * c The next variant argument is treated as a single char. |
114 | * c The next variant argument is treated as a single char. |
115 | * |
115 | * |
116 | * p The next variant argument is treated as a maximum |
116 | * p The next variant argument is treated as a maximum |
117 | * bit-width integer with respect to architecture |
117 | * bit-width integer with respect to architecture |
118 | * and printed in full hexadecimal width. |
118 | * and printed in full hexadecimal width. |
119 | * |
119 | * |
120 | * P As with 'p', but '0x' is prefixed. |
120 | * P As with 'p', but '0x' is prefixed. |
121 | * |
121 | * |
122 | * q The next variant argument is treated as a 64b integer |
122 | * q The next variant argument is treated as a 64b integer |
123 | * and printed in full hexadecimal width. |
123 | * and printed in full hexadecimal width. |
124 | * |
124 | * |
125 | * Q As with 'q', but '0x' is prefixed. |
125 | * Q As with 'q', but '0x' is prefixed. |
126 | * |
126 | * |
127 | * l The next variant argument is treated as a 32b integer |
127 | * l The next variant argument is treated as a 32b integer |
128 | * and printed in full hexadecimal width. |
128 | * and printed in full hexadecimal width. |
129 | * |
129 | * |
130 | * L As with 'l', but '0x' is prefixed. |
130 | * L As with 'l', but '0x' is prefixed. |
131 | * |
131 | * |
132 | * w The next variant argument is treated as a 16b integer |
132 | * w The next variant argument is treated as a 16b integer |
133 | * and printed in full hexadecimal width. |
133 | * and printed in full hexadecimal width. |
134 | * |
134 | * |
135 | * W As with 'w', but '0x' is prefixed. |
135 | * W As with 'w', but '0x' is prefixed. |
136 | * |
136 | * |
137 | * b The next variant argument is treated as a 8b integer |
137 | * b The next variant argument is treated as a 8b integer |
138 | * and printed in full hexadecimal width. |
138 | * and printed in full hexadecimal width. |
139 | * |
139 | * |
140 | * B As with 'b', but '0x' is prefixed. |
140 | * B As with 'b', but '0x' is prefixed. |
141 | * |
141 | * |
142 | * d The next variant argument is treated as integer |
142 | * d The next variant argument is treated as integer |
143 | * and printed in standard decimal format (only significant |
143 | * and printed in standard decimal format (only significant |
144 | * digits). |
144 | * digits). |
145 | * |
145 | * |
146 | * x The next variant argument is treated as integer |
146 | * x The next variant argument is treated as integer |
147 | * and printed in standard hexadecimal format (only significant |
147 | * and printed in standard hexadecimal format (only significant |
148 | * digits). |
148 | * digits). |
149 | * |
149 | * |
150 | * X As with 'x', but '0x' is prefixed. |
150 | * X As with 'x', but '0x' is prefixed. |
151 | * |
151 | * |
152 | * All other characters from fmt except the formatting directives |
152 | * All other characters from fmt except the formatting directives |
153 | * are printed in verbatim. |
153 | * are printed in verbatim. |
154 | * |
154 | * |
155 | * @param fmt Formatting NULL terminated string. |
155 | * @param fmt Formatting NULL terminated string. |
156 | */ |
156 | */ |
157 | int printf(const char *fmt, ...) |
157 | int printf(const char *fmt, ...) |
158 | { |
158 | { |
159 | int i = 0, j = 0; |
159 | int i = 0, j = 0; |
160 | int counter, retval; |
160 | int counter, retval; |
161 | va_list ap; |
161 | va_list ap; |
162 | char c; |
162 | char c; |
163 | 163 | ||
164 | uint64_t flags; |
164 | uint64_t flags; |
165 | 165 | ||
166 | counter = 0; |
166 | counter = 0; |
167 | va_start(ap, fmt); |
167 | va_start(ap, fmt); |
168 | 168 | ||
169 | while ((c = fmt[i])) { |
169 | while ((c = fmt[i])) { |
170 | /* control character */ |
170 | /* control character */ |
171 | if (c == '%' ) { |
171 | if (c == '%' ) { |
172 | /* print common characters if any processed */ |
172 | /* print common characters if any processed */ |
173 | if (i > j) { |
173 | if (i > j) { |
174 | if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */ |
174 | if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */ |
175 | return -counter; |
175 | return -counter; |
176 | } |
176 | } |
177 | counter += retval; |
177 | counter += retval; |
178 | } |
178 | } |
179 | 179 | ||
180 | j = ++i; |
180 | j = ++i; |
181 | 181 | ||
182 | /* parse modifiers */ |
182 | /* parse modifiers */ |
183 | flags = 0; |
183 | flags = 0; |
184 | /*switch (c = fmt[i]) { |
184 | /*switch (c = fmt[i]) { |
185 | case '-': |
185 | case '-': |
186 | } |
186 | } |
187 | */ |
187 | */ |
188 | switch (c = fmt[i]) { |
188 | switch (c = fmt[i]) { |
189 | 189 | ||
190 | /* percentile itself */ |
190 | /* percentile itself */ |
191 | case '%': |
191 | case '%': |
192 | --j; /* soon will be incremented back */ |
192 | --j; /* soon will be incremented back */ |
193 | break; |
193 | break; |
194 | 194 | ||
195 | /* |
195 | /* |
196 | * String and character conversions. |
196 | * String and character conversions. |
197 | */ |
197 | */ |
198 | case 's': |
198 | case 's': |
199 | if ((retval = putstr(va_arg(ap, char*))) == EOF) { |
199 | if ((retval = putstr(va_arg(ap, char*))) == EOF) { |
200 | return -counter; |
200 | return -counter; |
201 | }; |
201 | }; |
202 | 202 | ||
203 | counter += retval; |
203 | counter += retval; |
204 | break; |
204 | break; |
205 | case 'c': |
205 | case 'c': |
- | 206 | c = va_arg(ap, unsigned long); |
|
206 | if ((retval = putnchars((char *)&va_arg(ap, unsigned long), sizeof(char))) == EOF) { |
207 | if ((retval = putnchars(&c, sizeof(char))) == EOF) { |
207 | return -counter; |
208 | return -counter; |
208 | }; |
209 | }; |
209 | 210 | ||
210 | counter += retval; |
211 | counter += retval; |
211 | break; |
212 | break; |
212 | 213 | ||
213 | /* |
214 | /* |
214 | * Hexadecimal conversions with fixed width. |
215 | * Hexadecimal conversions with fixed width. |
215 | */ |
216 | */ |
216 | case 'P': |
217 | case 'P': |
217 | flags |= __PRINTF_FLAG_PREFIX; |
218 | flags |= __PRINTF_FLAG_PREFIX; |
218 | case 'p': |
219 | case 'p': |
219 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(unsigned long), flags)) == EOF ) { |
220 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(unsigned long), flags)) == EOF ) { |
220 | return -counter; |
221 | return -counter; |
221 | }; |
222 | }; |
222 | 223 | ||
223 | counter += retval; |
224 | counter += retval; |
224 | break; |
225 | break; |
225 | case 'Q': |
226 | case 'Q': |
226 | flags |= __PRINTF_FLAG_PREFIX; |
227 | flags |= __PRINTF_FLAG_PREFIX; |
227 | case 'q': |
228 | case 'q': |
228 | if ((retval = print_fixed_hex(va_arg(ap, uint64_t), sizeof(uint64_t), flags)) == EOF ) { |
229 | if ((retval = print_fixed_hex(va_arg(ap, uint64_t), sizeof(uint64_t), flags)) == EOF ) { |
229 | return -counter; |
230 | return -counter; |
230 | }; |
231 | }; |
231 | 232 | ||
232 | counter += retval; |
233 | counter += retval; |
233 | break; |
234 | break; |
234 | case 'L': |
235 | case 'L': |
235 | flags |= __PRINTF_FLAG_PREFIX; |
236 | flags |= __PRINTF_FLAG_PREFIX; |
236 | case 'l': |
237 | case 'l': |
237 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint32_t), flags)) == EOF ) { |
238 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint32_t), flags)) == EOF ) { |
238 | return -counter; |
239 | return -counter; |
239 | }; |
240 | }; |
240 | 241 | ||
241 | counter += retval; |
242 | counter += retval; |
242 | break; |
243 | break; |
243 | case 'W': |
244 | case 'W': |
244 | flags |= __PRINTF_FLAG_PREFIX; |
245 | flags |= __PRINTF_FLAG_PREFIX; |
245 | case 'w': |
246 | case 'w': |
246 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint16_t), flags)) == EOF ) { |
247 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint16_t), flags)) == EOF ) { |
247 | return -counter; |
248 | return -counter; |
248 | }; |
249 | }; |
249 | 250 | ||
250 | counter += retval; |
251 | counter += retval; |
251 | break; |
252 | break; |
252 | case 'B': |
253 | case 'B': |
253 | flags |= __PRINTF_FLAG_PREFIX; |
254 | flags |= __PRINTF_FLAG_PREFIX; |
254 | case 'b': |
255 | case 'b': |
255 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint8_t), flags)) == EOF ) { |
256 | if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint8_t), flags)) == EOF ) { |
256 | return -counter; |
257 | return -counter; |
257 | }; |
258 | }; |
258 | 259 | ||
259 | counter += retval; |
260 | counter += retval; |
260 | break; |
261 | break; |
261 | /* |
262 | /* |
262 | * Decimal and hexadecimal conversions. |
263 | * Decimal and hexadecimal conversions. |
263 | */ |
264 | */ |
264 | case 'd': |
265 | case 'd': |
265 | case 'i': |
266 | case 'i': |
266 | flags |= __PRINTF_FLAG_SIGNED; |
267 | flags |= __PRINTF_FLAG_SIGNED; |
267 | if ((retval = print_number(va_arg(ap,unsigned long), 10, flags)) == EOF ) { |
268 | if ((retval = print_number(va_arg(ap,unsigned long), 10, flags)) == EOF ) { |
268 | return -counter; |
269 | return -counter; |
269 | }; |
270 | }; |
270 | 271 | ||
271 | counter += retval; |
272 | counter += retval; |
272 | break; |
273 | break; |
273 | case 'X': |
274 | case 'X': |
274 | flags |= __PRINTF_FLAG_PREFIX; |
275 | flags |= __PRINTF_FLAG_PREFIX; |
275 | case 'x': |
276 | case 'x': |
276 | if ((retval = print_number(va_arg(ap, unsigned long), 16, flags)) == EOF ) { |
277 | if ((retval = print_number(va_arg(ap, unsigned long), 16, flags)) == EOF ) { |
277 | return -counter; |
278 | return -counter; |
278 | }; |
279 | }; |
279 | 280 | ||
280 | counter += retval; |
281 | counter += retval; |
281 | break; |
282 | break; |
282 | /* |
283 | /* |
283 | * Bad formatting. |
284 | * Bad formatting. |
284 | */ |
285 | */ |
285 | default: |
286 | default: |
286 | return -counter; |
287 | return -counter; |
287 | } |
288 | } |
288 | ++j; |
289 | ++j; |
289 | } |
290 | } |
290 | ++i; |
291 | ++i; |
291 | } |
292 | } |
292 | 293 | ||
293 | if (i > j) { |
294 | if (i > j) { |
294 | if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */ |
295 | if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */ |
295 | return -counter; |
296 | return -counter; |
296 | } |
297 | } |
297 | counter += retval; |
298 | counter += retval; |
298 | } |
299 | } |
299 | 300 | ||
300 | va_end(ap); |
301 | va_end(ap); |
301 | return counter; |
302 | return counter; |
302 | } |
303 | } |
303 | 304 | ||
304 | 305 |