diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-05-02 11:44:15 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-05-02 11:44:15 +0000 |
commit | 824a003052c51fdb9433bfd0dd0e4a525da8b2e7 (patch) | |
tree | b3c8677982cd6bbf0336f7b1ca810cef0533d072 /firmware | |
parent | a98b20e22a63094cd590298f7f6f3c6c1a42fe83 (diff) |
Added Gary's snprintf() and changed all places to use it.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@379 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/Makefile | 2 | ||||
-rw-r--r-- | firmware/common/sprintf.c | 127 | ||||
-rw-r--r-- | firmware/debug.c | 6 | ||||
-rw-r--r-- | firmware/id3.c | 5 | ||||
-rw-r--r-- | firmware/panic.c | 2 | ||||
-rw-r--r-- | firmware/playlist.c | 13 |
6 files changed, 140 insertions, 15 deletions
diff --git a/firmware/Makefile b/firmware/Makefile index 8f19551760..90ce900cc9 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -33,7 +33,7 @@ OBJS := $(SRC:%.c=%.o) all : archos.mod # archos.asm archos.elf : $(OBJS) app.lds - $(CC) -nostdlib -o archos.elf $(OBJS) -lgcc -Tapp.lds -Wl,-Map,archos.map + $(CC) -nostdlib -o archos.elf $(OBJS) -lgcc -lc -Tapp.lds -Wl,-Map,archos.map archos.bin : archos.elf $(OC) -O binary archos.elf archos.bin diff --git a/firmware/common/sprintf.c b/firmware/common/sprintf.c new file mode 100644 index 0000000000..80bdda6336 --- /dev/null +++ b/firmware/common/sprintf.c @@ -0,0 +1,127 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Gary Czvitkovicz + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +/* + * Minimal printf and snprintf formatting functions + * + * These support %c %s %d and %x + * Field width and zero-padding flag only + */ + +#include <stdarg.h> +#include <string.h> + +static const char hexdigit[] = "0123456789ABCDEF"; + +int vsnprintf (char *buf, int size, const char *fmt, va_list ap) +{ + char *bp = buf; + char *end = buf + size - 1; + + char *str; + char tmpbuf[12], pad; + int ch, width, val, sign; + + tmpbuf[sizeof tmpbuf - 1] = '\0'; + + while ((ch = *fmt++) != '\0' && bp < end) + { + if (ch == '%') + { + ch = *fmt++; + pad = ' '; + if (ch == '0') + pad = '0'; + + width = 0; + while (ch >= '0' && ch <= '9') + { + width = 10*width + ch - '0'; + ch = *fmt++; + } + + str = tmpbuf + sizeof tmpbuf - 1; + switch (ch) + { + case 'c': + *--str = va_arg (ap, int); + break; + + case 's': + str = va_arg (ap, char*); + break; + + case 'd': + val = sign = va_arg (ap, int); + if (val < 0) + val = -val; + do + { + *--str = (val % 10) + '0'; + val /= 10; + } + while (val > 0); + if (sign < 0) + *--str = '-'; + break; + + case 'x': + case 'X': + val = va_arg (ap, int); + do + { + *--str = hexdigit[val & 0xf]; + val >>= 4; + } + while (val > 0); + break; + + default: + *--str = ch; + break; + } + + if (width > 0) + { + width -= strlen (str); + while (width-- > 0 && buf < end) + *bp++ = pad; + } + while (*str != '\0' && buf < end) + *bp++ = *str++; + } + else + *bp++ = ch; + } + + *bp++ = '\0'; + return bp - buf - 1; +} + +int snprintf (char *buf, int size, const char *fmt, ...) +{ + int n; + va_list ap; + + va_start (ap, fmt); + n = vsnprintf (buf, size, fmt, ap); + va_end (ap); + + return n; +} diff --git a/firmware/debug.c b/firmware/debug.c index d2186049aa..9d2a6997a0 100644 --- a/firmware/debug.c +++ b/firmware/debug.c @@ -29,7 +29,7 @@ char debugbuf[200]; static int debug_tx_ready(void) { - return (SSR1 & SCI_TDRE); + return (SSR1 & SCI_TDRE); } static void debug_tx_char(char ch) @@ -180,7 +180,7 @@ void debugf(char *fmt, ...) va_list ap; va_start(ap, fmt); - vsprintf(debugmembuf, fmt, ap); + vsnprintf(debugmembuf, sizeof(debugmembuf), fmt, ap); va_end(ap); debug(debugmembuf); } @@ -197,7 +197,7 @@ void debugf(char *fmt, ...) va_list ap; va_start( ap, fmt ); - vsprintf( debugmembuf, fmt, ap ); + vsnprintf( debugmembuf, sizeof(debugmembuf), fmt, ap ); va_end( ap ); printf( debugmembuf ); } diff --git a/firmware/id3.c b/firmware/id3.c index b510195cb1..5761380e60 100644 --- a/firmware/id3.c +++ b/firmware/id3.c @@ -27,10 +27,7 @@ #include <unistd.h> #include <string.h> #include <errno.h> - -#ifdef SIMULATOR #include <fcntl.h> -#endif struct mp3entry { char *path; @@ -530,7 +527,7 @@ char *secs2str(int ms) static char buffer[32]; int secs = ms/1000; ms %= 1000; - sprintf(buffer, "%d:%02d.%d", secs/60, secs%60, ms/100); + snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100); return buffer; } diff --git a/firmware/panic.c b/firmware/panic.c index 2f8912631d..7f8a08889e 100644 --- a/firmware/panic.c +++ b/firmware/panic.c @@ -45,7 +45,7 @@ void panicf( char *fmt, ...) va_list ap; va_start( ap, fmt ); - vsprintf( panic_buf, fmt, ap ); + vsnprintf( panic_buf, sizeof(panic_buf), fmt, ap ); va_end( ap ); panic( panic_buf ); diff --git a/firmware/playlist.c b/firmware/playlist.c index 904e5d4452..67b7834449 100644 --- a/firmware/playlist.c +++ b/firmware/playlist.c @@ -37,7 +37,7 @@ int reload_playlist_info( playlist_info_t *playlist ) /* return a dummy playlist entry */ - sprintf( playlist->filename, "\\playlists\\1.m3u" ); + strncpy( playlist->filename, "\\playlists\\1.m3u", sizeof(playlist->filename) ); playlist->indices_count = 4; @@ -62,7 +62,8 @@ void load_playlist( playlist_info_t *playlist, const char *filename ) { char *m3u_buf = NULL; char debug_message[128]; - sprintf( debug_message, "load_playlist( %s )\n", filename ); + snprintf( debug_message, sizeof(debug_message), + "load_playlist( %s )\n", filename ); debug( debug_message ); /* read file */ @@ -71,7 +72,7 @@ void load_playlist( playlist_info_t *playlist, const char *filename ) { /* store playlist filename */ - sprintf( playlist->filename, filename ); + strncpy( playlist->filename, filename, sizeof(playlist->filename) ); /* add track indices to playlist data structure */ @@ -159,7 +160,7 @@ void extend_indices( playlist_info_t *playlist, int new_index ) track_t next_playlist_track( playlist_info_t *playlist ) { track_t track; - sprintf( track.filename, "boogie" ); + strncpy( track.filename, "boogie", sizeof(track.filename) ); return track; } @@ -320,13 +321,13 @@ void get_indices_as_string( char *string, playlist_info_t *playlist ) { /* first iteration - no comma */ - sprintf( tmp, "%d", p[count] ); + snprintf( tmp, sizeof(tmp), "%d", p[count] ); } else { /* normal iteration - insert comma */ - sprintf( tmp, ",%d", p[count] ); + snprintf( tmp, sizeof(tmp), ",%d", p[count] ); } strcat( string, tmp ); |