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
;
49 tmpbuf
[sizeof tmpbuf
- 1] = '\0';
51 while ((ch
= *fmt
++) != '\0' && ok
)
61 while (ch
>= '0' && ch
<= '9')
63 width
= 10*width
+ ch
- '0';
67 str
= tmpbuf
+ sizeof tmpbuf
- 1;
71 *--str
= va_arg (ap
, int);
75 str
= va_arg (ap
, char*);
79 val
= sign
= va_arg (ap
, int);
84 *--str
= (val
% 10) + '0';
94 uval
= va_arg (ap
, int);
97 *--str
= hexdigit
[uval
& 0xf];
110 width
-= strlen (str
);
111 while (width
-- > 0 && ok
)
114 while (*str
!= '\0' && ok
)
115 ok
=push(userp
, *str
++);
120 return ok
; /* true means good */
123 struct for_snprintf
{
124 unsigned char *ptr
; /* where to store it */
125 int bytes
; /* amount already stored */
126 int max
; /* max amount to store */
129 static int sprfunc(void *ptr
, unsigned char letter
)
131 struct for_snprintf
*pr
= (struct for_snprintf
*)ptr
;
132 if(pr
->bytes
< pr
->max
) {
138 return false; /* filled buffer */
142 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
146 struct for_snprintf pr
;
153 ok
= format(sprfunc
, &pr
, fmt
, ap
);
156 /* make sure it ends with a trailing zero */
157 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
162 int vsnprintf(char *buf
, int size
, const char *fmt
, va_list ap
)
165 struct for_snprintf pr
;
171 ok
= format(sprfunc
, &pr
, fmt
, ap
);
173 /* make sure it ends with a trailing zero */
174 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
180 int fd
; /* where to store it */
181 int bytes
; /* amount stored */
184 static int fprfunc(void *pr
, unsigned char letter
)
186 struct for_fprintf
*fpr
= (struct for_fprintf
*)pr
;
187 int rc
= write(fpr
->fd
, &letter
, 1);
190 fpr
->bytes
++; /* count them */
191 return true; /* we are ok */
194 return false; /* failure */
198 int fprintf(int fd
, const char *fmt
, ...)
202 struct for_fprintf fpr
;
208 ok
= format(fprfunc
, &fpr
, fmt
, ap
);
211 return fpr
.bytes
; /* return 0 on error */