1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Gary Czvitkovicz
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 * Minimal printf and snprintf formatting functions
23 * These support %c %s %d and %x
24 * Field width and zero-padding flag only
31 #include "file.h" /* for write(), used in fprintf() */
32 #include "sprintf.h" /* to allow the simulator magic */
34 static const char hexdigit
[] = "0123456789ABCDEF";
37 /* call 'push()' for each output letter */
38 int (*push
)(void *userp
, unsigned char data
),
45 int ch
, width
, val
, sign
;
51 tmpbuf
[sizeof tmpbuf
- 1] = '\0';
53 while ((ch
= *fmt
++) != '\0' && ok
)
63 while (ch
>= '0' && ch
<= '9')
65 width
= 10*width
+ ch
- '0';
69 str
= tmpbuf
+ sizeof tmpbuf
- 1;
73 *--str
= va_arg (ap
, int);
77 str
= va_arg (ap
, char*);
81 val
= sign
= va_arg (ap
, int);
86 *--str
= (val
% 10) + '0';
96 uval
= va_arg (ap
, int);
99 *--str
= hexdigit
[uval
& 0xf];
110 ulval
= va_arg (ap
, long);
113 *--str
= hexdigit
[ulval
& 0xf];
119 lval
= sign
= va_arg (ap
, long);
124 *--str
= (lval
% 10) + '0';
146 width
-= strlen (str
);
147 while (width
-- > 0 && ok
)
150 while (*str
!= '\0' && ok
)
151 ok
=push(userp
, *str
++);
156 return ok
; /* true means good */
159 struct for_snprintf
{
160 unsigned char *ptr
; /* where to store it */
161 int bytes
; /* amount already stored */
162 int max
; /* max amount to store */
165 static int sprfunc(void *ptr
, unsigned char letter
)
167 struct for_snprintf
*pr
= (struct for_snprintf
*)ptr
;
168 if(pr
->bytes
< pr
->max
) {
174 return false; /* filled buffer */
178 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
182 struct for_snprintf pr
;
189 ok
= format(sprfunc
, &pr
, fmt
, ap
);
192 /* make sure it ends with a trailing zero */
193 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
198 int vsnprintf(char *buf
, int size
, const char *fmt
, va_list ap
)
201 struct for_snprintf pr
;
207 ok
= format(sprfunc
, &pr
, fmt
, ap
);
209 /* make sure it ends with a trailing zero */
210 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
216 int fd
; /* where to store it */
217 int bytes
; /* amount stored */
220 static int fprfunc(void *pr
, unsigned char letter
)
222 struct for_fprintf
*fpr
= (struct for_fprintf
*)pr
;
223 int rc
= write(fpr
->fd
, &letter
, 1);
226 fpr
->bytes
++; /* count them */
227 return true; /* we are ok */
230 return false; /* failure */
234 int fprintf(int fd
, const char *fmt
, ...)
238 struct for_fprintf fpr
;
244 ok
= format(fprfunc
, &fpr
, fmt
, ap
);
247 return fpr
.bytes
; /* return 0 on error */