summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <franklin@rockbox.org>2020-07-26 15:41:32 -0400
committerFranklin Wei <franklin@rockbox.org>2020-07-26 15:41:32 -0400
commit2eb7ce475a8338171b6d3df5f2789d527b87f746 (patch)
treef645ea845c5b0b5989464b561d96835a711d7c8b
parent73b02a4a2c04dafafdaa3f4f1fb388c3b8e26f0d (diff)
downloadrockbox-2eb7ce475a8338171b6d3df5f2789d527b87f746.tar.gz
rockbox-2eb7ce475a8338171b6d3df5f2789d527b87f746.zip
Refactor 73b02a4.
Moves basename to a separate function, and documents some of the pointer arithmetic it's doing. Change-Id: I6f65ad99f163c2b223929f2ce7805b8935df71c0
-rw-r--r--apps/plugins/md5sum.c49
1 files changed, 36 insertions, 13 deletions
diff --git a/apps/plugins/md5sum.c b/apps/plugins/md5sum.c
index f69c71a49f..b929d8f061 100644
--- a/apps/plugins/md5sum.c
+++ b/apps/plugins/md5sum.c
@@ -181,14 +181,46 @@ static void hash_check( int out, const char *path )
181 rb->close( list ); 181 rb->close( list );
182} 182}
183 183
184/*
185 * Return the last name from a pathname (ignoring a trailing slash if
186 * it exists). The returned pointer points to a statically allocated
187 * buffer.
188 */
189static char *get_basename(const char *path) {
190 static char temp[MAX_PATH];
191 char *p;
192 int len, isdir = 0;
193
194 rb->strcpy(temp, path);
195
196 len = rb->strlen(temp);
197
198 if (temp[len - 1] == '/')
199 {
200 /* strip trailing slash, and update length accordingly */
201 temp[--len] = '\0';
202 isdir = 1;
203 }
204
205 /* find the last slash, if there is one */
206 p = rb->strrchr(temp, '/');
207
208 /*
209 * re-append trailing slash if we previously removed it (the
210 * original NUL is still present)
211 */
212 if(isdir)
213 temp[len++] = '/';
214
215 return p ? (p + 1) : temp;
216}
217
184enum plugin_status plugin_start(const void* parameter) 218enum plugin_status plugin_start(const void* parameter)
185{ 219{
186 const char *arg = (const char *)parameter; /* input file path, if any */ 220 const char *arg = (const char *)parameter; /* input file path, if any */
187 char temp[MAX_PATH]; /* input file name */ 221 char *basename;
188 char *basename=temp;
189 int out = -1; /* output file descriptor */ 222 int out = -1; /* output file descriptor */
190 char filename[MAX_PATH]; /* output file name */ 223 char filename[MAX_PATH]; /* output file name */
191 int isdir=0; /*flag if input file is a directory */
192 224
193 void (*action)( int, const char * ) = NULL; 225 void (*action)( int, const char * ) = NULL;
194 226
@@ -243,17 +275,8 @@ enum plugin_status plugin_start(const void* parameter)
243 action = hash_dir; 275 action = hash_dir;
244 arg = "/"; 276 arg = "/";
245 } 277 }
246 rb->strcpy(temp, arg);
247 if (temp[(rb->strlen(temp) - 1)] == '/')
248 {
249 temp[(rb->strlen(temp) - 1)] = '\0';
250 isdir=1;
251 }
252 if(rb->strrchr(temp, '/'))
253 basename =(rb->strrchr(temp, '/')+1);
254 278
255 if(isdir) 279 basename = get_basename(arg);
256 temp[(rb->strlen(temp))] = '/';
257 280
258 rb->lcd_putsf( 0, 1, "Hashing %s", basename ); 281 rb->lcd_putsf( 0, 1, "Hashing %s", basename );
259 rb->lcd_puts( 0, 2, rb->str(LANG_ACTION_STD_CANCEL) ); 282 rb->lcd_puts( 0, 2, rb->str(LANG_ACTION_STD_CANCEL) );