]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/IA32_flat_GCC_Galileo_Gen_2/Support_Files/printf-stdarg.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / IA32_flat_GCC_Galileo_Gen_2 / Support_Files / printf-stdarg.c
1 /*\r
2         Copyright 2001, 2002 Georges Menie (www.menie.org)\r
3         stdarg version contributed by Christian Ettinger\r
4 \r
5     This program is free software; you can redistribute it and/or modify\r
6     it under the terms of the GNU Lesser General Public License as published by\r
7     the Free Software Foundation; either version 2 of the License, or\r
8     (at your option) any later version.\r
9 \r
10     This program is distributed in the hope that it will be useful,\r
11     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13     GNU Lesser General Public License for more details.\r
14 \r
15     You should have received a copy of the GNU Lesser General Public License\r
16     along with this program; if not, write to the Free Software\r
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
18 */\r
19 \r
20 #include <stdarg.h>\r
21 #include "galileo_support.h"\r
22 \r
23 static void printchar(char **str, int c)\r
24 {\r
25         if (str) {\r
26                 **str = (char)c;\r
27                 ++(*str);\r
28         }\r
29         else\r
30         {\r
31                 vGalileoPrintc( c );\r
32         }\r
33 }\r
34 \r
35 #define PAD_RIGHT 1\r
36 #define PAD_ZERO 2\r
37 \r
38 static int prints(char **out, const char *string, int width, int pad)\r
39 {\r
40         register int pc = 0, padchar = ' ';\r
41 \r
42         if (width > 0) {\r
43                 register int len = 0;\r
44                 register const char *ptr;\r
45                 for (ptr = string; *ptr; ++ptr) ++len;\r
46                 if (len >= width) width = 0;\r
47                 else width -= len;\r
48                 if (pad & PAD_ZERO) padchar = '0';\r
49         }\r
50         if (!(pad & PAD_RIGHT)) {\r
51                 for ( ; width > 0; --width) {\r
52                         printchar (out, padchar);\r
53                         ++pc;\r
54                 }\r
55         }\r
56         for ( ; *string ; ++string) {\r
57                 printchar (out, *string);\r
58                 ++pc;\r
59         }\r
60         for ( ; width > 0; --width) {\r
61                 printchar (out, padchar);\r
62                 ++pc;\r
63         }\r
64 \r
65         return pc;\r
66 }\r
67 \r
68 /* the following should be enough for 32 bit int */\r
69 #define PRINT_BUF_LEN 12\r
70 \r
71 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)\r
72 {\r
73         char print_buf[PRINT_BUF_LEN];\r
74         register char *s;\r
75         register int t, neg = 0, pc = 0;\r
76         register unsigned int u = (unsigned int)i;\r
77 \r
78         if (i == 0) {\r
79                 print_buf[0] = '0';\r
80                 print_buf[1] = '\0';\r
81                 return prints (out, print_buf, width, pad);\r
82         }\r
83 \r
84         if (sg && b == 10 && i < 0) {\r
85                 neg = 1;\r
86                 u = (unsigned int)-i;\r
87         }\r
88 \r
89         s = print_buf + PRINT_BUF_LEN-1;\r
90         *s = '\0';\r
91 \r
92         while (u) {\r
93                 t = (unsigned int)u % b;\r
94                 if( t >= 10 )\r
95                         t += letbase - '0' - 10;\r
96                 *--s = (char)(t + '0');\r
97                 u /= b;\r
98         }\r
99 \r
100         if (neg) {\r
101                 if( width && (pad & PAD_ZERO) ) {\r
102                         printchar (out, '-');\r
103                         ++pc;\r
104                         --width;\r
105                 }\r
106                 else {\r
107                         *--s = '-';\r
108                 }\r
109         }\r
110 \r
111         return pc + prints (out, s, width, pad);\r
112 }\r
113 \r
114 int print( char **out, const char *format, va_list args )\r
115 {\r
116         register int width, pad;\r
117         register int pc = 0;\r
118         char scr[2];\r
119 \r
120         for (; *format != 0; ++format) {\r
121                 if (*format == '%') {\r
122                         ++format;\r
123                         width = pad = 0;\r
124                         if (*format == '\0') break;\r
125                         if (*format == '%') goto out;\r
126                         if (*format == '-') {\r
127                                 ++format;\r
128                                 pad = PAD_RIGHT;\r
129                         }\r
130                         while (*format == '0') {\r
131                                 ++format;\r
132                                 pad |= PAD_ZERO;\r
133                         }\r
134                         for ( ; *format >= '0' && *format <= '9'; ++format) {\r
135                                 width *= 10;\r
136                                 width += *format - '0';\r
137                         }\r
138                         if( *format == 's' ) {\r
139                                 register char *s = (char *)va_arg( args, int );\r
140                                 pc += prints (out, s?s:"(null)", width, pad);\r
141                                 continue;\r
142                         }\r
143                         if( *format == 'd' ) {\r
144                                 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');\r
145                                 continue;\r
146                         }\r
147                         if( *format == 'x' ) {\r
148                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');\r
149                                 continue;\r
150                         }\r
151                         if( *format == 'X' ) {\r
152                                 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');\r
153                                 continue;\r
154                         }\r
155                         if( *format == 'u' ) {\r
156                                 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');\r
157                                 continue;\r
158                         }\r
159                         if( *format == 'c' ) {\r
160                                 /* char are converted to int then pushed on the stack */\r
161                                 scr[0] = (char)va_arg( args, int );\r
162                                 scr[1] = '\0';\r
163                                 pc += prints (out, scr, width, pad);\r
164                                 continue;\r
165                         }\r
166                 }\r
167                 else {\r
168                 out:\r
169                         printchar (out, *format);\r
170                         ++pc;\r
171                 }\r
172         }\r
173         if (out) **out = '\0';\r
174         va_end( args );\r
175         return pc;\r
176 }\r
177 \r
178 int printf(const char *format, ...)\r
179 {\r
180         va_list args;\r
181         \r
182         va_start( args, format );\r
183         return print( 0, format, args );\r
184 }\r
185 \r
186 int sprintf(char *out, const char *format, ...)\r
187 {\r
188         va_list args;\r
189         \r
190         va_start( args, format );\r
191         return print( &out, format, args );\r
192 }\r
193 \r
194 \r
195 int snprintf( char *buf, unsigned int count, const char *format, ... )\r
196 {\r
197         va_list args;\r
198         \r
199         ( void ) count;\r
200         \r
201         va_start( args, format );\r
202         return print( &buf, format, args );\r
203 }\r
204 \r
205 \r
206 #ifdef TEST_PRINTF\r
207 int main(void)\r
208 {\r
209         char *ptr = "Hello world!";\r
210         char *np = 0;\r
211         int i = 5;\r
212         unsigned int bs = sizeof(int)*8;\r
213         int mi;\r
214         char buf[80];\r
215 \r
216         mi = (1 << (bs-1)) + 1;\r
217         printf("%s\n", ptr);\r
218         printf("printf test\n");\r
219         printf("%s is null pointer\n", np);\r
220         printf("%d = 5\n", i);\r
221         printf("%d = - max int\n", mi);\r
222         printf("char %c = 'a'\n", 'a');\r
223         printf("hex %x = ff\n", 0xff);\r
224         printf("hex %02x = 00\n", 0);\r
225         printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);\r
226         printf("%d %s(s)%", 0, "message");\r
227         printf("\n");\r
228         printf("%d %s(s) with %%\n", 0, "message");\r
229         sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);\r
230         sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);\r
231         sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);\r
232         sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);\r
233         sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);\r
234         sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);\r
235         sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);\r
236         sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);\r
237 \r
238         return 0;\r
239 }\r
240 \r
241 /*\r
242  * if you compile this file with\r
243  *   gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c\r
244  * you will get a normal warning:\r
245  *   printf.c:214: warning: spurious trailing `%' in format\r
246  * this line is testing an invalid % at the end of the format string.\r
247  *\r
248  * this should display (on 32bit int machine) :\r
249  *\r
250  * Hello world!\r
251  * printf test\r
252  * (null) is null pointer\r
253  * 5 = 5\r
254  * -2147483647 = - max int\r
255  * char a = 'a'\r
256  * hex ff = ff\r
257  * hex 00 = 00\r
258  * signed -3 = unsigned 4294967293 = hex fffffffd\r
259  * 0 message(s)\r
260  * 0 message(s) with %\r
261  * justif: "left      "\r
262  * justif: "     right"\r
263  *  3: 0003 zero padded\r
264  *  3: 3    left justif.\r
265  *  3:    3 right justif.\r
266  * -3: -003 zero padded\r
267  * -3: -3   left justif.\r
268  * -3:   -3 right justif.\r
269  */\r
270 \r
271 #endif\r
272 \r
273 \r
274 /* To keep linker happy. */\r
275 int     write( int i, char* c, int n)\r
276 {\r
277         (void)i;\r
278         (void)n;\r
279         (void)c;\r
280         return 0;\r
281 }\r
282 \r