revert between 56095 -> 55830 in arch
[AROS.git] / arch / ppc-chrp / boot / openfirmware / src / support.c
blobce4b164cc41b811d4bce848a85dbd0f1be64faf4
1 /*
2 Copyright © 2008-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <of1275.h>
7 #include <support.h>
8 #include <stdarg.h>
10 char *remove_path(const char *in)
12 char *p = (char *)&in[strlen(in)-1];
14 while (p > in && p[-1] != '/' && p[-1] != ':') p--;
15 return p;
18 int32_t strlen(const char *c)
20 int32_t result = 0;
21 while (*c++)
22 result++;
24 return result;
27 int isblank(char c)
29 return (c == ' ' || c == '\t');
32 int isspace(char c)
34 return (c == ' ' || c == '\t' || c == '\f' || c == '\n' || c == '\r' || c == '\v');
37 int isdigit(char c)
39 return (c >= '0' && c <= '9');
42 int isxdigit(char c)
44 return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
47 int tolower(char c)
49 if (c >= 'A' && c <= 'Z')
50 c -= ('A'-'a');
52 return c;
55 int strncasecmp(const char *s1, const char *s2, int max)
57 int diff = 0;
58 for (; max && !(diff = tolower(*s2)-tolower(*s1)) && *s1; max--, s1++, s2++);
59 return diff;
62 int strcasecmp(const char *s1, const char *s2)
64 int diff = 0;
65 for (; !(diff = tolower(*s2)-tolower(*s1)) && *s1; s1++, s2++);
66 return diff;
69 int strncmp(const char *s1, const char *s2, int max)
71 int diff = 0;
72 for (; max && !(diff = *s2-*s1) && *s1; max--, s1++, s2++);
73 return diff;
76 void bzero(void *dest, int length)
78 char *d = dest;
80 while (length--)
81 *d++ = 0;
84 void memcpy(void *dest, const void *src, int length)
86 const char *s = src;
87 char *d = dest;
89 while (length--)
90 *d++ = *s++;
94 void __itoa(char *buf, intptr_t val, char code)
96 static const char convtbl[] = "0123456789ABCDEF";
97 int i;
98 if (code == 'p')
100 for (i=0; i < 8; i++, val <<= 4)
101 *buf++ = convtbl[(val >> 28) & 0xf];
103 *buf = 0;
105 return;
107 else
109 uint32_t ud = val;
110 int divisor = 10;
111 char *p, *p1, *p2;
113 if (code == 'd' && val < 0)
115 val = -val;
116 *buf++ = '-';
118 if (code == 'x')
119 divisor = 16;
121 p = buf;
123 do {
124 *p++ = convtbl[ud % divisor];
125 } while(ud /= divisor);
127 *p = 0;
129 p1 = buf;
130 p2 = p - 1;
132 while(p1 < p2)
134 char temp = *p1;
135 *p1 = *p2;
136 *p2 = temp;
138 p1++;
139 p2--;
144 void sprintf(char *dest, char *str, ...)
146 va_list a;
147 char c;
148 char buf[20];
150 va_start(a, str);
152 while ((c = *str++) != 0)
154 if (c != '%')
155 *dest++ = c;
157 else
159 char *p, tmp;
161 c = *str++;
163 switch (c)
165 case 'c':
166 tmp = va_arg(a, int);
167 *dest++ = tmp;
168 break;
170 case 'p':
171 case 'x':
172 case 'd':
173 case 'u':
174 p = buf;
175 intptr_t val = va_arg(a, intptr_t);
176 __itoa(p, val, c);
177 goto string;
179 case 's':
180 p = va_arg(a, char *);
181 if (!p)
182 p = "(null)";
183 string:
184 while (*p)
185 *dest++ = *p++;
186 break;
188 default:
189 *dest++ = c;
190 break;
195 *dest = 0;
197 va_end(a);
200 void printf(char *str, ...)
202 va_list a;
203 char c;
204 char buf[20];
206 va_start(a, str);
208 while ((c = *str++) != 0)
210 if (c != '%')
211 ofw_write(stdout, &c, 1);
213 else
215 char *p, tmp;
217 c = *str++;
219 switch (c)
221 case 'c':
222 tmp = va_arg(a, int);
223 ofw_write(stdout, &tmp, 1);
224 break;
226 case 'p':
227 case 'x':
228 case 'd':
229 case 'u':
230 p = buf;
231 intptr_t val = va_arg(a, intptr_t);
232 __itoa(p, val, c);
233 goto string;
235 case 's':
236 p = va_arg(a, char *);
237 if (!p)
238 p = "(null)";
239 string:
240 ofw_write(stdout, p, strlen(p));
241 break;
243 default:
244 ofw_write(stdout, &c, 1);
245 break;
250 va_end(a);
253 int atoi(const char *str)
255 int val = 0;
257 if ((str[0] == '0') && (tolower(str[1]) == 'x'))
259 str += 2;
261 while (*str && isxdigit(*str))
263 char c = tolower(*str++);
265 val <<= 4;
266 if (c > '9')
267 val += c - 'a' + 10;
268 else
269 val += c - '0';
273 while(*str && isdigit(*str))
275 val *= 10;
276 val += *str - '0';
277 str++;
280 return val;