From e7e20fab1b2b90ea80e118959386017ed1d6f703 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sat, 19 Nov 2022 22:39:14 -0500 Subject: create function open_pathfmt() to allow printf formatting on open() save some space by allowing printf formatting directly rather than having a buffer and using sprintf Change-Id: I049c8f898fb4a68a26ad0f0646250c242647ba12 --- apps/debug_menu.c | 15 +++++---------- apps/filetypes.c | 11 +++++------ apps/gui/icon.c | 6 ++---- apps/logfdisp.c | 14 ++++++-------- apps/misc.c | 10 ++++++++++ apps/misc.h | 1 + apps/tagcache.c | 33 ++++++++++++--------------------- apps/talk.c | 5 +---- 8 files changed, 42 insertions(+), 53 deletions(-) diff --git a/apps/debug_menu.c b/apps/debug_menu.c index 5ebaa3a3f4..a4bfe65b1c 100644 --- a/apps/debug_menu.c +++ b/apps/debug_menu.c @@ -2297,11 +2297,6 @@ static bool cpu_boost_log(void) static bool cpu_boost_log_dump(void) { int fd; -#if CONFIG_RTC - struct tm *nowtm; - char fname[MAX_PATH]; -#endif - int count = cpu_boost_log_getcount(); char *str = cpu_boost_log_getlog_first(); @@ -2312,11 +2307,11 @@ static bool cpu_boost_log_dump(void) return false; #if CONFIG_RTC - nowtm = get_time(); - snprintf(fname, MAX_PATH, "%s/boostlog_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, - nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, - nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec); - fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC); + struct tm *nowtm = get_time(); + fd = open_pathfmt(O_CREAT|O_WRONLY|O_TRUNC, + "%s/boostlog_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, + nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, + nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec); #else fd = open(ROCKBOX_DIR "/boostlog.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666); #endif diff --git a/apps/filetypes.c b/apps/filetypes.c index 24dc0f2e4d..74607eac1c 100644 --- a/apps/filetypes.c +++ b/apps/filetypes.c @@ -262,9 +262,9 @@ void read_color_theme_file(void) { unknown_file.color = -1; if (!global_settings.colors_file[0] || global_settings.colors_file[0] == '-') return; - snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours", - global_settings.colors_file); - fd = open(buffer, O_RDONLY); + + fd = open_pathfmt(O_RDONLY, THEME_DIR "/%s.colours", + global_settings.colors_file); if (fd < 0) return; while (read_line(fd, buffer, MAX_PATH) > 0) @@ -303,9 +303,8 @@ void read_viewer_theme_file(void) custom_filetype_icons[i] = filetypes[i].icon; } - snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR, - global_settings.viewers_icon_file); - fd = open(buffer, O_RDONLY); + fd = open_pathfmt(O_RDONLY, "%s/%s.icons", ICON_DIR, + global_settings.viewers_icon_file); if (fd < 0) return; while (read_line(fd, buffer, MAX_PATH) > 0) diff --git a/apps/gui/icon.c b/apps/gui/icon.c index e78aa6841c..2c09f88852 100644 --- a/apps/gui/icon.c +++ b/apps/gui/icon.c @@ -32,6 +32,7 @@ #include "bmp.h" #include "filetypes.h" #include "language.h" +#include "misc.h" #include "bitmaps/default_icons.h" #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1) @@ -181,10 +182,7 @@ static void load_icons(const char* filename, enum Iconset iconset, ic->handle = 0; if (filename[0] && filename[0] != '-') { - char path[MAX_PATH]; - - snprintf(path, sizeof(path), ICON_DIR "/%s.bmp", filename); - fd = open(path, O_RDONLY); + fd = open_pathfmt(O_RDONLY, ICON_DIR "/%s.bmp", filename); if (fd < 0) return; buf_size = read_bmp_fd(fd, &ic->bmp, 0, diff --git a/apps/logfdisp.c b/apps/logfdisp.c index b139f30ac7..aebc9ffb33 100644 --- a/apps/logfdisp.c +++ b/apps/logfdisp.c @@ -35,7 +35,9 @@ #include "logfdisp.h" #include "action.h" #include "splash.h" - +#if CONFIG_RTC +#include "misc.h" +#endif /*CONFIG_RTC*/ int compute_nb_lines(int w, struct font* font) { int i, nb_lines; @@ -212,10 +214,6 @@ bool logfdisplay(void) bool logfdump(void) { int fd; -#if CONFIG_RTC - struct tm *nowtm; - char fname[MAX_PATH]; -#endif splashf(HZ, "Log File Dumped"); @@ -227,11 +225,11 @@ bool logfdump(void) logfenabled = false; #if CONFIG_RTC - nowtm = get_time(); - snprintf(fname, MAX_PATH, "%s/logf_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, + struct tm *nowtm = get_time(); + fd = open_pathfmt(O_CREAT|O_WRONLY|O_TRUNC, + "%s/logf_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec); - fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC); #else fd = open(ROCKBOX_DIR "/logf.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666); #endif diff --git a/apps/misc.c b/apps/misc.c index 0bac986a6f..66d1c272ae 100644 --- a/apps/misc.c +++ b/apps/misc.c @@ -1418,6 +1418,16 @@ int string_option(const char *option, const char *const oplist[], bool ignore_ca return -1; } +/* open but with a builtin printf for assembling the path */ +int open_pathfmt(int oflag, const char *pathfmt, ...) +{ + static char buf[MAX_PATH]; + va_list ap; + vsnprintf(buf, sizeof(buf), pathfmt, ap); + va_end(ap); + return open(buf, oflag, 0666); +} + /** Open a UTF-8 file and set file descriptor to first byte after BOM. * If no BOM is present this behaves like open(). * If the file is opened for writing and O_TRUNC is set, write a BOM to diff --git a/apps/misc.h b/apps/misc.h index ed40de3e1f..50191f0e95 100644 --- a/apps/misc.h +++ b/apps/misc.h @@ -122,6 +122,7 @@ extern int show_logo(void); #define BOM_UTF_16_SIZE 2 int split_string(char *str, const char needle, char *vector[], int vector_length); +int open_pathfmt(int oflag, const char *pathfmt, ...); int open_utf8(const char* pathname, int flags); int string_option(const char *option, const char *const oplist[], bool ignore_case); diff --git a/apps/tagcache.c b/apps/tagcache.c index b91195813e..a297bc7353 100644 --- a/apps/tagcache.c +++ b/apps/tagcache.c @@ -422,19 +422,17 @@ static ssize_t ecwrite_index_entry(int fd, struct index_entry *buf) static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write) { int fd; - char buf[MAX_PATH]; - const int bufsz = sizeof(buf); int rc; if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT) return -1; - snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag); + fd = open_pathfmt(write ? O_RDWR : O_RDONLY, TAGCACHE_FILE_INDEX, tag); - fd = open(buf, write ? O_RDWR : O_RDONLY); if (fd < 0) { - logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf); + logf("tag file open failed: tag=%d write=%d file= " TAGCACHE_FILE_INDEX, + tag, write, tag); tc_stat.ready = false; return fd; } @@ -805,10 +803,7 @@ static bool open_files(struct tagcache_search *tcs, int tag) { if (tcs->idxfd[tag] < 0) { - char fn[MAX_PATH]; - - snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag); - tcs->idxfd[tag] = open(fn, O_RDONLY); + tcs->idxfd[tag] = open_pathfmt(O_RDONLY, TAGCACHE_FILE_INDEX, tag); } if (tcs->idxfd[tag] < 0) @@ -1588,12 +1583,9 @@ bool tagcache_search_add_clause(struct tagcache_search *tcs, } if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0) - { - char buf[MAX_PATH]; - const int bufsz = sizeof(buf); - - snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, clause->tag); - tcs->idxfd[clause->tag] = open(buf, O_RDONLY); + { + tcs->idxfd[clause->tag] = open_pathfmt(O_RDONLY, + TAGCACHE_FILE_INDEX, clause->tag); } } @@ -2751,11 +2743,11 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd) * Creating new index file to store the tags. No need to preload * anything whether the index type is sorted or not. */ - snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, index_type); - fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0666); + fd = open_pathfmt(O_WRONLY | O_CREAT | O_TRUNC, + TAGCACHE_FILE_INDEX, index_type); if (fd < 0) { - logf("%s open fail", buf); + logf(TAGCACHE_FILE_INDEX " open fail", index_type); return -2; } @@ -4408,12 +4400,11 @@ static bool check_deleted_files(void) struct tagfile_entry tfe; logf("reverse scan..."); - snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag_filename); - fd = open(buf, O_RDONLY); + fd = open_pathfmt(O_RDONLY, TAGCACHE_FILE_INDEX, tag_filename); if (fd < 0) { - logf("%s open fail", buf); + logf(TAGCACHE_FILE_INDEX " open fail", tag_filename); return false; } diff --git a/apps/talk.c b/apps/talk.c index 8507a89541..89319ae9a2 100644 --- a/apps/talk.c +++ b/apps/talk.c @@ -247,7 +247,6 @@ static struct buflib_callbacks talk_ops = { static int open_voicefile(void) { - char buf[64]; char* p_lang = DEFAULT_VOICE_LANG; /* default */ if ( global_settings.lang_file[0] && @@ -256,9 +255,7 @@ static int open_voicefile(void) p_lang = (char *)global_settings.lang_file; } - snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang); - - return open(buf, O_RDONLY); + return open_pathfmt(O_RDONLY, LANG_DIR "/%s.voice", p_lang); } -- cgit