1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
29 static const char hexdigit
[] = "0123456789ABCDEF";
32 /* call 'push()' for each output letter */
33 int (*push
)(void *userp
, unsigned char data
),
40 int ch
, width
, val
, sign
, precision
;
46 tmpbuf
[sizeof tmpbuf
- 1] = '\0';
48 while ((ch
= *fmt
++) != '\0' && ok
)
58 while (ch
>= '0' && ch
<= '9')
60 width
= 10*width
+ ch
- '0';
68 while (ch
>= '0' && ch
<= '9')
70 precision
= 10*precision
+ ch
- '0';
77 str
= tmpbuf
+ sizeof tmpbuf
- 1;
81 *--str
= va_arg (ap
, int);
85 str
= va_arg (ap
, char*);
89 val
= sign
= va_arg (ap
, int);
94 *--str
= (val
% 10) + '0';
103 uval
= va_arg(ap
, unsigned int);
106 *--str
= (uval
% 10) + '0';
115 uval
= va_arg (ap
, int);
118 *--str
= hexdigit
[uval
& 0xf];
130 ulval
= va_arg (ap
, long);
133 *--str
= hexdigit
[ulval
& 0xf];
139 lval
= lsign
= va_arg (ap
, long);
144 *--str
= (lval
% 10) + '0';
153 ulval
= va_arg(ap
, unsigned long);
156 *--str
= (ulval
% 10) + '0';
176 width
-= strlen (str
);
177 while (width
-- > 0 && ok
)
180 while (*str
!= '\0' && ok
&& precision
--)
181 ok
=push(userp
, *str
++);
186 return ok
; /* true means good */
190 int fd
; /* where to store it */
191 int bytes
; /* amount stored */
194 static int fprfunc(void *pr
, unsigned char letter
)
196 struct for_fprintf
*fpr
= (struct for_fprintf
*)pr
;
197 int rc
= write(fpr
->fd
, &letter
, 1);
200 fpr
->bytes
++; /* count them */
201 return true; /* we are ok */
204 return false; /* failure */
208 int fdprintf(int fd
, const char *fmt
, ...)
212 struct for_fprintf fpr
;
218 ok
= format(fprfunc
, &fpr
, fmt
, ap
);
221 return fpr
.bytes
; /* return 0 on error */
224 int vuprintf(int (*push
)(void *userp
, unsigned char data
), void *userp
, const char *fmt
, va_list ap
)
226 return format(push
, userp
, fmt
, ap
);