imxtools/hwemul: allow for toolchain prefix override
[maemo-rb.git] / utils / imxtools / hwemul / dev / format.c
blobf5783159c0290b14ee7fb2bfc0f3e979a8e5aa77
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Gary Czvitkovicz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 #include <stdarg.h>
24 #include <limits.h>
25 #include "stddef.h"
26 #include "string.h"
27 #include "format.h"
29 static const char hexdigit[] = "0123456789ABCDEF";
31 void vuprintf(
32 /* call 'push()' for each output letter */
33 int (*push)(void *userp, unsigned char data),
34 void *userp,
35 const char *fmt,
36 va_list ap)
38 char *str;
39 char tmpbuf[12], pad;
40 int ch, width, val, sign, precision;
41 long lval, lsign;
42 unsigned int uval;
43 unsigned long ulval;
44 size_t uszval;
45 ssize_t szval, szsign;
46 bool ok = true;
48 tmpbuf[sizeof tmpbuf - 1] = '\0';
50 while ((ch = *fmt++) != '\0' && ok)
52 if (ch == '%')
54 ch = *fmt++;
55 pad = ' ';
56 if (ch == '0')
57 pad = '0';
59 width = 0;
60 while (ch >= '0' && ch <= '9')
62 width = 10*width + ch - '0';
63 ch = *fmt++;
66 precision = 0;
67 if(ch == '.')
69 ch = *fmt++;
70 while (ch >= '0' && ch <= '9')
72 precision = 10*precision + ch - '0';
73 ch = *fmt++;
75 } else {
76 precision = INT_MAX;
79 str = tmpbuf + sizeof tmpbuf - 1;
80 switch (ch)
82 case 'c':
83 *--str = va_arg (ap, int);
84 break;
86 case 's':
87 str = va_arg (ap, char*);
88 break;
90 case 'd':
91 val = sign = va_arg (ap, int);
92 if (val < 0)
93 val = -val;
96 *--str = (val % 10) + '0';
97 val /= 10;
99 while (val > 0);
100 if (sign < 0)
101 *--str = '-';
102 break;
104 case 'u':
105 uval = va_arg(ap, unsigned int);
108 *--str = (uval % 10) + '0';
109 uval /= 10;
111 while (uval > 0);
112 break;
114 case 'x':
115 case 'X':
116 pad='0';
117 uval = va_arg (ap, int);
120 *--str = hexdigit[uval & 0xf];
121 uval >>= 4;
123 while (uval);
124 break;
126 case 'l':
127 ch = *fmt++;
128 switch(ch) {
129 case 'x':
130 case 'X':
131 pad='0';
132 ulval = va_arg (ap, long);
135 *--str = hexdigit[ulval & 0xf];
136 ulval >>= 4;
138 while (ulval);
139 break;
140 case 'd':
141 lval = lsign = va_arg (ap, long);
142 if (lval < 0)
143 lval = -lval;
146 *--str = (lval % 10) + '0';
147 lval /= 10;
149 while (lval > 0);
150 if (lsign < 0)
151 *--str = '-';
152 break;
154 case 'u':
155 ulval = va_arg(ap, unsigned long);
158 *--str = (ulval % 10) + '0';
159 ulval /= 10;
161 while (ulval > 0);
162 break;
164 default:
165 *--str = 'l';
166 *--str = ch;
169 break;
171 case 'z':
172 ch = *fmt++;
173 switch(ch) {
174 case 'd':
175 szval = szsign = va_arg (ap, ssize_t);
176 if (szval < 0)
177 szval = -szval;
180 *--str = (szval % 10) + '0';
181 szval /= 10;
183 while (szval > 0);
184 if (szsign < 0)
185 *--str = '-';
186 break;
188 case 'u':
189 uszval = va_arg(ap, size_t);
192 *--str = (uszval % 10) + '0';
193 uszval /= 10;
195 while (uszval > 0);
196 break;
198 default:
199 *--str = 'z';
200 *--str = ch;
203 break;
205 default:
206 *--str = ch;
207 break;
210 if (width > 0)
212 width -= strlen (str);
213 while (width-- > 0 && ok)
214 ok=push(userp, pad);
216 while (*str != '\0' && ok && precision--)
217 ok=push(userp, *str++);
219 else
220 ok=push(userp, ch);