From 2eb7ce475a8338171b6d3df5f2789d527b87f746 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Sun, 26 Jul 2020 15:41:32 -0400 Subject: Refactor 73b02a4. Moves basename to a separate function, and documents some of the pointer arithmetic it's doing. Change-Id: I6f65ad99f163c2b223929f2ce7805b8935df71c0 --- apps/plugins/md5sum.c | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file 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 ) rb->close( list ); } +/* + * Return the last name from a pathname (ignoring a trailing slash if + * it exists). The returned pointer points to a statically allocated + * buffer. + */ +static char *get_basename(const char *path) { + static char temp[MAX_PATH]; + char *p; + int len, isdir = 0; + + rb->strcpy(temp, path); + + len = rb->strlen(temp); + + if (temp[len - 1] == '/') + { + /* strip trailing slash, and update length accordingly */ + temp[--len] = '\0'; + isdir = 1; + } + + /* find the last slash, if there is one */ + p = rb->strrchr(temp, '/'); + + /* + * re-append trailing slash if we previously removed it (the + * original NUL is still present) + */ + if(isdir) + temp[len++] = '/'; + + return p ? (p + 1) : temp; +} + enum plugin_status plugin_start(const void* parameter) { const char *arg = (const char *)parameter; /* input file path, if any */ - char temp[MAX_PATH]; /* input file name */ - char *basename=temp; + char *basename; int out = -1; /* output file descriptor */ char filename[MAX_PATH]; /* output file name */ - int isdir=0; /*flag if input file is a directory */ void (*action)( int, const char * ) = NULL; @@ -243,17 +275,8 @@ enum plugin_status plugin_start(const void* parameter) action = hash_dir; arg = "/"; } - rb->strcpy(temp, arg); - if (temp[(rb->strlen(temp) - 1)] == '/') - { - temp[(rb->strlen(temp) - 1)] = '\0'; - isdir=1; - } - if(rb->strrchr(temp, '/')) - basename =(rb->strrchr(temp, '/')+1); - if(isdir) - temp[(rb->strlen(temp))] = '/'; + basename = get_basename(arg); rb->lcd_putsf( 0, 1, "Hashing %s", basename ); rb->lcd_puts( 0, 2, rb->str(LANG_ACTION_STD_CANCEL) ); -- cgit v1.2.3