summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-11-19 22:39:14 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2022-11-19 23:13:42 -0500
commite7e20fab1b2b90ea80e118959386017ed1d6f703 (patch)
treedf435e8bc4a4d9069a365061454443ae9e33ded3
parent8fe42c43c6c62a8f593ee337902f8919ed2152ad (diff)
downloadrockbox-e7e20fab1b2b90ea80e118959386017ed1d6f703.tar.gz
rockbox-e7e20fab1b2b90ea80e118959386017ed1d6f703.zip
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
-rw-r--r--apps/debug_menu.c15
-rw-r--r--apps/filetypes.c11
-rw-r--r--apps/gui/icon.c6
-rw-r--r--apps/logfdisp.c14
-rw-r--r--apps/misc.c10
-rw-r--r--apps/misc.h1
-rw-r--r--apps/tagcache.c33
-rw-r--r--apps/talk.c5
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)
2297static bool cpu_boost_log_dump(void) 2297static bool cpu_boost_log_dump(void)
2298{ 2298{
2299 int fd; 2299 int fd;
2300#if CONFIG_RTC
2301 struct tm *nowtm;
2302 char fname[MAX_PATH];
2303#endif
2304
2305 int count = cpu_boost_log_getcount(); 2300 int count = cpu_boost_log_getcount();
2306 char *str = cpu_boost_log_getlog_first(); 2301 char *str = cpu_boost_log_getlog_first();
2307 2302
@@ -2312,11 +2307,11 @@ static bool cpu_boost_log_dump(void)
2312 return false; 2307 return false;
2313 2308
2314#if CONFIG_RTC 2309#if CONFIG_RTC
2315 nowtm = get_time(); 2310 struct tm *nowtm = get_time();
2316 snprintf(fname, MAX_PATH, "%s/boostlog_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, 2311 fd = open_pathfmt(O_CREAT|O_WRONLY|O_TRUNC,
2317 nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, 2312 "%s/boostlog_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR,
2318 nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec); 2313 nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday,
2319 fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC); 2314 nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec);
2320#else 2315#else
2321 fd = open(ROCKBOX_DIR "/boostlog.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666); 2316 fd = open(ROCKBOX_DIR "/boostlog.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666);
2322#endif 2317#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) {
262 unknown_file.color = -1; 262 unknown_file.color = -1;
263 if (!global_settings.colors_file[0] || global_settings.colors_file[0] == '-') 263 if (!global_settings.colors_file[0] || global_settings.colors_file[0] == '-')
264 return; 264 return;
265 snprintf(buffer, MAX_PATH, THEME_DIR "/%s.colours", 265
266 global_settings.colors_file); 266 fd = open_pathfmt(O_RDONLY, THEME_DIR "/%s.colours",
267 fd = open(buffer, O_RDONLY); 267 global_settings.colors_file);
268 if (fd < 0) 268 if (fd < 0)
269 return; 269 return;
270 while (read_line(fd, buffer, MAX_PATH) > 0) 270 while (read_line(fd, buffer, MAX_PATH) > 0)
@@ -303,9 +303,8 @@ void read_viewer_theme_file(void)
303 custom_filetype_icons[i] = filetypes[i].icon; 303 custom_filetype_icons[i] = filetypes[i].icon;
304 } 304 }
305 305
306 snprintf(buffer, MAX_PATH, "%s/%s.icons", ICON_DIR, 306 fd = open_pathfmt(O_RDONLY, "%s/%s.icons", ICON_DIR,
307 global_settings.viewers_icon_file); 307 global_settings.viewers_icon_file);
308 fd = open(buffer, O_RDONLY);
309 if (fd < 0) 308 if (fd < 0)
310 return; 309 return;
311 while (read_line(fd, buffer, MAX_PATH) > 0) 310 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 @@
32#include "bmp.h" 32#include "bmp.h"
33#include "filetypes.h" 33#include "filetypes.h"
34#include "language.h" 34#include "language.h"
35#include "misc.h"
35 36
36#include "bitmaps/default_icons.h" 37#include "bitmaps/default_icons.h"
37#if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1) 38#if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
@@ -181,10 +182,7 @@ static void load_icons(const char* filename, enum Iconset iconset,
181 ic->handle = 0; 182 ic->handle = 0;
182 if (filename[0] && filename[0] != '-') 183 if (filename[0] && filename[0] != '-')
183 { 184 {
184 char path[MAX_PATH]; 185 fd = open_pathfmt(O_RDONLY, ICON_DIR "/%s.bmp", filename);
185
186 snprintf(path, sizeof(path), ICON_DIR "/%s.bmp", filename);
187 fd = open(path, O_RDONLY);
188 if (fd < 0) 186 if (fd < 0)
189 return; 187 return;
190 buf_size = read_bmp_fd(fd, &ic->bmp, 0, 188 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 @@
35#include "logfdisp.h" 35#include "logfdisp.h"
36#include "action.h" 36#include "action.h"
37#include "splash.h" 37#include "splash.h"
38 38#if CONFIG_RTC
39#include "misc.h"
40#endif /*CONFIG_RTC*/
39int compute_nb_lines(int w, struct font* font) 41int compute_nb_lines(int w, struct font* font)
40{ 42{
41 int i, nb_lines; 43 int i, nb_lines;
@@ -212,10 +214,6 @@ bool logfdisplay(void)
212bool logfdump(void) 214bool logfdump(void)
213{ 215{
214 int fd; 216 int fd;
215#if CONFIG_RTC
216 struct tm *nowtm;
217 char fname[MAX_PATH];
218#endif
219 217
220 splashf(HZ, "Log File Dumped"); 218 splashf(HZ, "Log File Dumped");
221 219
@@ -227,11 +225,11 @@ bool logfdump(void)
227 logfenabled = false; 225 logfenabled = false;
228 226
229#if CONFIG_RTC 227#if CONFIG_RTC
230 nowtm = get_time(); 228 struct tm *nowtm = get_time();
231 snprintf(fname, MAX_PATH, "%s/logf_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR, 229 fd = open_pathfmt(O_CREAT|O_WRONLY|O_TRUNC,
230 "%s/logf_%04d%02d%02d%02d%02d%02d.txt", ROCKBOX_DIR,
232 nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, 231 nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday,
233 nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec); 232 nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec);
234 fd = open(fname, O_CREAT|O_WRONLY|O_TRUNC);
235#else 233#else
236 fd = open(ROCKBOX_DIR "/logf.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666); 234 fd = open(ROCKBOX_DIR "/logf.txt", O_CREAT|O_WRONLY|O_TRUNC, 0666);
237#endif 235#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
1418 return -1; 1418 return -1;
1419} 1419}
1420 1420
1421/* open but with a builtin printf for assembling the path */
1422int open_pathfmt(int oflag, const char *pathfmt, ...)
1423{
1424 static char buf[MAX_PATH];
1425 va_list ap;
1426 vsnprintf(buf, sizeof(buf), pathfmt, ap);
1427 va_end(ap);
1428 return open(buf, oflag, 0666);
1429}
1430
1421/** Open a UTF-8 file and set file descriptor to first byte after BOM. 1431/** Open a UTF-8 file and set file descriptor to first byte after BOM.
1422 * If no BOM is present this behaves like open(). 1432 * If no BOM is present this behaves like open().
1423 * If the file is opened for writing and O_TRUNC is set, write a BOM to 1433 * 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);
122#define BOM_UTF_16_SIZE 2 122#define BOM_UTF_16_SIZE 2
123 123
124int split_string(char *str, const char needle, char *vector[], int vector_length); 124int split_string(char *str, const char needle, char *vector[], int vector_length);
125int open_pathfmt(int oflag, const char *pathfmt, ...);
125int open_utf8(const char* pathname, int flags); 126int open_utf8(const char* pathname, int flags);
126int string_option(const char *option, const char *const oplist[], bool ignore_case); 127int string_option(const char *option, const char *const oplist[], bool ignore_case);
127 128
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)
422static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write) 422static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
423{ 423{
424 int fd; 424 int fd;
425 char buf[MAX_PATH];
426 const int bufsz = sizeof(buf);
427 int rc; 425 int rc;
428 426
429 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT) 427 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT)
430 return -1; 428 return -1;
431 429
432 snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag); 430 fd = open_pathfmt(write ? O_RDWR : O_RDONLY, TAGCACHE_FILE_INDEX, tag);
433 431
434 fd = open(buf, write ? O_RDWR : O_RDONLY);
435 if (fd < 0) 432 if (fd < 0)
436 { 433 {
437 logf("tag file open failed: tag=%d write=%d file=%s", tag, write, buf); 434 logf("tag file open failed: tag=%d write=%d file= " TAGCACHE_FILE_INDEX,
435 tag, write, tag);
438 tc_stat.ready = false; 436 tc_stat.ready = false;
439 return fd; 437 return fd;
440 } 438 }
@@ -805,10 +803,7 @@ static bool open_files(struct tagcache_search *tcs, int tag)
805{ 803{
806 if (tcs->idxfd[tag] < 0) 804 if (tcs->idxfd[tag] < 0)
807 { 805 {
808 char fn[MAX_PATH]; 806 tcs->idxfd[tag] = open_pathfmt(O_RDONLY, TAGCACHE_FILE_INDEX, tag);
809
810 snprintf(fn, sizeof fn, TAGCACHE_FILE_INDEX, tag);
811 tcs->idxfd[tag] = open(fn, O_RDONLY);
812 } 807 }
813 808
814 if (tcs->idxfd[tag] < 0) 809 if (tcs->idxfd[tag] < 0)
@@ -1588,12 +1583,9 @@ bool tagcache_search_add_clause(struct tagcache_search *tcs,
1588 } 1583 }
1589 1584
1590 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0) 1585 if (!TAGCACHE_IS_NUMERIC(clause->tag) && tcs->idxfd[clause->tag] < 0)
1591 { 1586 {
1592 char buf[MAX_PATH]; 1587 tcs->idxfd[clause->tag] = open_pathfmt(O_RDONLY,
1593 const int bufsz = sizeof(buf); 1588 TAGCACHE_FILE_INDEX, clause->tag);
1594
1595 snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, clause->tag);
1596 tcs->idxfd[clause->tag] = open(buf, O_RDONLY);
1597 } 1589 }
1598 } 1590 }
1599 1591
@@ -2751,11 +2743,11 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2751 * Creating new index file to store the tags. No need to preload 2743 * Creating new index file to store the tags. No need to preload
2752 * anything whether the index type is sorted or not. 2744 * anything whether the index type is sorted or not.
2753 */ 2745 */
2754 snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, index_type); 2746 fd = open_pathfmt(O_WRONLY | O_CREAT | O_TRUNC,
2755 fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0666); 2747 TAGCACHE_FILE_INDEX, index_type);
2756 if (fd < 0) 2748 if (fd < 0)
2757 { 2749 {
2758 logf("%s open fail", buf); 2750 logf(TAGCACHE_FILE_INDEX " open fail", index_type);
2759 return -2; 2751 return -2;
2760 } 2752 }
2761 2753
@@ -4408,12 +4400,11 @@ static bool check_deleted_files(void)
4408 struct tagfile_entry tfe; 4400 struct tagfile_entry tfe;
4409 4401
4410 logf("reverse scan..."); 4402 logf("reverse scan...");
4411 snprintf(buf, bufsz, TAGCACHE_FILE_INDEX, tag_filename);
4412 fd = open(buf, O_RDONLY);
4413 4403
4404 fd = open_pathfmt(O_RDONLY, TAGCACHE_FILE_INDEX, tag_filename);
4414 if (fd < 0) 4405 if (fd < 0)
4415 { 4406 {
4416 logf("%s open fail", buf); 4407 logf(TAGCACHE_FILE_INDEX " open fail", tag_filename);
4417 return false; 4408 return false;
4418 } 4409 }
4419 4410
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 = {
247 247
248static int open_voicefile(void) 248static int open_voicefile(void)
249{ 249{
250 char buf[64];
251 char* p_lang = DEFAULT_VOICE_LANG; /* default */ 250 char* p_lang = DEFAULT_VOICE_LANG; /* default */
252 251
253 if ( global_settings.lang_file[0] && 252 if ( global_settings.lang_file[0] &&
@@ -256,9 +255,7 @@ static int open_voicefile(void)
256 p_lang = (char *)global_settings.lang_file; 255 p_lang = (char *)global_settings.lang_file;
257 } 256 }
258 257
259 snprintf(buf, sizeof(buf), LANG_DIR "/%s.voice", p_lang); 258 return open_pathfmt(O_RDONLY, LANG_DIR "/%s.voice", p_lang);
260
261 return open(buf, O_RDONLY);
262} 259}
263 260
264 261