2 * Copyright (C) Paul Mackerras 1997.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
25 write(void *handle
, void *ptr
, int nb
)
37 args
.service
= "write";
40 args
.ihandle
= handle
;
49 read(void *handle
, void *ptr
, int nb
)
61 args
.service
= "read";
64 args
.ihandle
= handle
;
80 args
.service
= "exit";
92 args
.service
= "enter";
97 finddevice(const char *name
)
107 args
.service
= "finddevice";
111 args
.phandle
= (void *) -1;
117 claim(unsigned long virt
, unsigned long size
, unsigned long align
)
129 args
.service
= "claim";
140 getprop(void *phandle
, const char *name
, void *buf
, int buflen
)
153 args
.service
= "getprop";
156 args
.phandle
= phandle
;
159 args
.buflen
= buflen
;
172 return write(f
, &ch
, 1) == 1? c
: -1;
178 return putc(c
, stdout
);
182 fputs(char *str
, void *f
)
186 return write(f
, str
, n
) == n
? 0: -1;
189 size_t strnlen(const char * s
, size_t count
)
193 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
198 extern unsigned int __div64_32(unsigned long long *dividend
,
199 unsigned int divisor
);
201 /* The unnecessary pointer compare is there
202 * to check for type safety (n must be 64bit)
204 # define do_div(n,base) ({ \
205 unsigned int __base = (base); \
206 unsigned int __rem; \
207 (void)(((typeof((n)) *)0) == ((unsigned long long *)0)); \
208 if (((n) >> 32) == 0) { \
209 __rem = (unsigned int)(n) % __base; \
210 (n) = (unsigned int)(n) / __base; \
212 __rem = __div64_32(&(n), __base); \
216 static int skip_atoi(const char **s
)
220 for (i
= 0; '0' <= (c
= **s
) && c
<= '9'; ++*s
)
225 #define ZEROPAD 1 /* pad with zero */
226 #define SIGN 2 /* unsigned/signed long */
227 #define PLUS 4 /* show plus */
228 #define SPACE 8 /* space if plus */
229 #define LEFT 16 /* left justified */
230 #define SPECIAL 32 /* 0x */
231 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
233 static char * number(char * str
, unsigned long long num
, int base
, int size
, int precision
, int type
)
236 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
240 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
243 if (base
< 2 || base
> 36)
245 c
= (type
& ZEROPAD
) ? '0' : ' ';
248 if ((signed long long)num
< 0) {
250 num
= - (signed long long)num
;
252 } else if (type
& PLUS
) {
255 } else if (type
& SPACE
) {
260 if (type
& SPECIAL
) {
269 else while (num
!= 0) {
270 tmp
[i
++] = digits
[do_div(num
, base
)];
275 if (!(type
&(ZEROPAD
+LEFT
)))
280 if (type
& SPECIAL
) {
291 while (i
< precision
--)
300 int vsprintf(char *buf
, const char *fmt
, va_list args
)
303 unsigned long long num
;
308 int flags
; /* flags to number() */
310 int field_width
; /* width of output field */
311 int precision
; /* min. # of digits for integers; max
312 number of chars for from string */
313 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
314 /* 'z' support added 23/7/1999 S.H. */
315 /* 'z' changed to 'Z' --davidm 1/25/99 */
318 for (str
=buf
; *fmt
; ++fmt
) {
327 ++fmt
; /* this also skips first '%' */
329 case '-': flags
|= LEFT
; goto repeat
;
330 case '+': flags
|= PLUS
; goto repeat
;
331 case ' ': flags
|= SPACE
; goto repeat
;
332 case '#': flags
|= SPECIAL
; goto repeat
;
333 case '0': flags
|= ZEROPAD
; goto repeat
;
336 /* get field width */
338 if ('0' <= *fmt
&& *fmt
<= '9')
339 field_width
= skip_atoi(&fmt
);
340 else if (*fmt
== '*') {
342 /* it's the next argument */
343 field_width
= va_arg(args
, int);
344 if (field_width
< 0) {
345 field_width
= -field_width
;
350 /* get the precision */
354 if ('0' <= *fmt
&& *fmt
<= '9')
355 precision
= skip_atoi(&fmt
);
356 else if (*fmt
== '*') {
358 /* it's the next argument */
359 precision
= va_arg(args
, int);
365 /* get the conversion qualifier */
367 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
=='Z') {
378 while (--field_width
> 0)
380 *str
++ = (unsigned char) va_arg(args
, int);
381 while (--field_width
> 0)
386 s
= va_arg(args
, char *);
390 len
= strnlen(s
, precision
);
393 while (len
< field_width
--)
395 for (i
= 0; i
< len
; ++i
)
397 while (len
< field_width
--)
402 if (field_width
== -1) {
403 field_width
= 2*sizeof(void *);
407 (unsigned long) va_arg(args
, void *), 16,
408 field_width
, precision
, flags
);
413 if (qualifier
== 'l') {
414 long * ip
= va_arg(args
, long *);
416 } else if (qualifier
== 'Z') {
417 size_t * ip
= va_arg(args
, size_t *);
420 int * ip
= va_arg(args
, int *);
429 /* integer number formats - set up the flags and "break" */
454 if (qualifier
== 'l') {
455 num
= va_arg(args
, unsigned long);
457 num
= (signed long) num
;
458 } else if (qualifier
== 'Z') {
459 num
= va_arg(args
, size_t);
460 } else if (qualifier
== 'h') {
461 num
= (unsigned short) va_arg(args
, int);
463 num
= (signed short) num
;
465 num
= va_arg(args
, unsigned int);
467 num
= (signed int) num
;
469 str
= number(str
, num
, base
, field_width
, precision
, flags
);
475 int sprintf(char * buf
, const char *fmt
, ...)
481 i
=vsprintf(buf
,fmt
,args
);
486 static char sprint_buf
[1024];
489 printf(const char *fmt
, ...)
495 n
= vsprintf(sprint_buf
, fmt
, args
);
497 write(stdout
, sprint_buf
, n
);