From 96f2a9dad198eee48a7bbb0614e1e668eb21a6d3 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Wed, 8 Jul 2009 23:29:41 +0000 Subject: [PATCH] Numbered filename creation: Make the search for pre-existing files independent of the requested number length. This way it will also increment properly if the requested number length changes or overflows (more than 99 files for numberlen == 2). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21732 a1c6a512-1295-4272-9138-f99709370657 --- firmware/general.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/firmware/general.c b/firmware/general.c index 1ff3340d27..14b2b55bf9 100644 --- a/firmware/general.c +++ b/firmware/general.c @@ -106,6 +106,7 @@ char *create_numbered_filename(char *buffer, const char *path, int max_num; int pathlen; int prefixlen = strlen(prefix); + int suffixlen = strlen(suffix); char fmtstring[12]; if (buffer != path) @@ -131,10 +132,14 @@ char *create_numbered_filename(char *buffer, const char *path, while ((entry = readdir(dir))) { - int curr_num; + int curr_num, namelen; - if (strncasecmp((char *)entry->d_name, prefix, prefixlen) - || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix)) + if (strncasecmp((char *)entry->d_name, prefix, prefixlen)) + continue; + + namelen = strlen((char *)entry->d_name); + if ((namelen <= prefixlen + suffixlen) + || strcasecmp((char *)entry->d_name + namelen - suffixlen, suffix)) continue; curr_num = atoi((char *)entry->d_name + prefixlen);