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 ****************************************************************************/
30 static const char hexdigit
[] = "0123456789ABCDEF";
33 /* call 'push()' for each output letter */
34 int (*push
)(void *userp
, unsigned char data
),
41 int ch
, width
, val
, sign
, precision
;
46 ssize_t szval
, szsign
;
49 tmpbuf
[sizeof tmpbuf
- 1] = '\0';
51 while ((ch
= *fmt
++) != '\0' && ok
)
61 while (ch
>= '0' && ch
<= '9')
63 width
= 10*width
+ ch
- '0';
71 while (ch
>= '0' && ch
<= '9')
73 precision
= 10*precision
+ ch
- '0';
80 str
= tmpbuf
+ sizeof tmpbuf
- 1;
84 *--str
= va_arg (ap
, int);
88 str
= va_arg (ap
, char*);
92 val
= sign
= va_arg (ap
, int);
97 *--str
= (val
% 10) + '0';
106 uval
= va_arg(ap
, unsigned int);
109 *--str
= (uval
% 10) + '0';
116 /* for pointers prepend 0x and act like 'X' */
123 uval
= va_arg (ap
, int);
126 *--str
= hexdigit
[uval
& 0xf];
138 ulval
= va_arg (ap
, long);
141 *--str
= hexdigit
[ulval
& 0xf];
147 lval
= lsign
= va_arg (ap
, long);
152 *--str
= (lval
% 10) + '0';
161 ulval
= va_arg(ap
, unsigned long);
164 *--str
= (ulval
% 10) + '0';
181 szval
= szsign
= va_arg (ap
, ssize_t
);
186 *--str
= (szval
% 10) + '0';
195 uszval
= va_arg(ap
, size_t);
198 *--str
= (uszval
% 10) + '0';
218 width
-= strlen (str
);
219 while (width
-- > 0 && ok
)
222 while (*str
!= '\0' && ok
&& precision
--)
223 ok
=push(userp
, *str
++);
231 int fd
; /* where to store it */
232 int bytes
; /* amount stored */
235 static int fprfunc(void *pr
, unsigned char letter
)
237 struct for_fprintf
*fpr
= (struct for_fprintf
*)pr
;
238 int rc
= write(fpr
->fd
, &letter
, 1);
241 fpr
->bytes
++; /* count them */
242 return true; /* we are ok */
245 return false; /* failure */
249 int fdprintf(int fd
, const char *fmt
, ...)
252 struct for_fprintf fpr
;
258 format(fprfunc
, &fpr
, fmt
, ap
);
261 return fpr
.bytes
; /* return 0 on error */
264 void vuprintf(int (*push
)(void *userp
, unsigned char data
), void *userp
, const char *fmt
, va_list ap
)
266 format(push
, userp
, fmt
, ap
);