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 ****************************************************************************/
23 * Minimal printf and snprintf formatting functions
25 * These support %c %s %d and %x
26 * Field width and zero-padding flag only
34 #include "file.h" /* for write(), used in fprintf() */
35 #include "sprintf.h" /* to allow the simulator magic */
37 static const char hexdigit
[] = "0123456789ABCDEF";
40 /* call 'push()' for each output letter */
41 int (*push
)(void *userp
, unsigned char data
),
48 int ch
, width
, val
, sign
, precision
;
54 tmpbuf
[sizeof tmpbuf
- 1] = '\0';
56 while ((ch
= *fmt
++) != '\0' && ok
)
66 while (ch
>= '0' && ch
<= '9')
68 width
= 10*width
+ ch
- '0';
76 while (ch
>= '0' && ch
<= '9')
78 precision
= 10*precision
+ ch
- '0';
85 str
= tmpbuf
+ sizeof tmpbuf
- 1;
89 *--str
= va_arg (ap
, int);
93 str
= va_arg (ap
, char*);
97 val
= sign
= va_arg (ap
, int);
102 *--str
= (val
% 10) + '0';
111 uval
= va_arg(ap
, unsigned int);
114 *--str
= (uval
% 10) + '0';
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';
184 width
-= strlen (str
);
185 while (width
-- > 0 && ok
)
188 while (*str
!= '\0' && ok
&& precision
--)
189 ok
=push(userp
, *str
++);
194 return ok
; /* true means good */
197 #if !defined(SIMULATOR) || !defined(linux)
198 /* ALSA library requires a more advanced snprintf, so let's not
199 override it in simulator for Linux. Note that Cygwin requires
200 our snprintf or it produces garbled output after a while. */
202 struct for_snprintf
{
203 unsigned char *ptr
; /* where to store it */
204 int bytes
; /* amount already stored */
205 int max
; /* max amount to store */
208 static int sprfunc(void *ptr
, unsigned char letter
)
210 struct for_snprintf
*pr
= (struct for_snprintf
*)ptr
;
211 if(pr
->bytes
< pr
->max
) {
217 return false; /* filled buffer */
221 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
225 struct for_snprintf pr
;
227 pr
.ptr
= (unsigned char *)buf
;
232 ok
= format(sprfunc
, &pr
, fmt
, ap
);
235 /* make sure it ends with a trailing zero */
236 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
241 int vsnprintf(char *buf
, int size
, const char *fmt
, va_list ap
)
244 struct for_snprintf pr
;
246 pr
.ptr
= (unsigned char *)buf
;
250 ok
= format(sprfunc
, &pr
, fmt
, ap
);
252 /* make sure it ends with a trailing zero */
253 pr
.ptr
[(pr
.bytes
< pr
.max
) ? 0 : -1] = '\0';
258 #endif /* Linux SIMULATOR */
261 int fd
; /* where to store it */
262 int bytes
; /* amount stored */
265 static int fprfunc(void *pr
, unsigned char letter
)
267 struct for_fprintf
*fpr
= (struct for_fprintf
*)pr
;
268 int rc
= write(fpr
->fd
, &letter
, 1);
271 fpr
->bytes
++; /* count them */
272 return true; /* we are ok */
275 return false; /* failure */
279 int fdprintf(int fd
, const char *fmt
, ...)
283 struct for_fprintf fpr
;
289 ok
= format(fprfunc
, &fpr
, fmt
, ap
);
292 return fpr
.bytes
; /* return 0 on error */