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
This commit is contained in:
William Wilgus 2022-11-19 22:39:14 -05:00 committed by William Wilgus
parent 8fe42c43c6
commit e7e20fab1b
8 changed files with 42 additions and 53 deletions

View file

@ -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

View file

@ -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)

View file

@ -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,

View file

@ -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

View file

@ -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

View file

@ -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);

View file

@ -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;
}

View file

@ -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);
}