From 51fc232cdb2f569f31b73996207c2a7a2fce109e Mon Sep 17 00:00:00 2001 From: Jeremy Sowden Date: Fri, 7 Jun 2019 09:52:24 +0100 Subject: [PATCH] wmail: refactored MakePathName: one malloc; one snprintf instead of two memcpys. --- wmail/src/common.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/wmail/src/common.c b/wmail/src/common.c index c3bb506..a4f1736 100644 --- a/wmail/src/common.c +++ b/wmail/src/common.c @@ -77,22 +77,27 @@ void WARNING( const char *fmt, ... ) char *MakePathName( const char *dir, const char *file ) { + size_t dir_len = strlen( dir ); + size_t file_len = strlen( file ); + + size_t len; + const char *fmt; + char *fullName; - size_t len1 = strlen( dir ); - size_t len2 = strlen( file ); - if( dir[len1-1] != '/' ) - fullName = malloc( len1 + len2 + 2 ); - else - fullName = malloc( len1 + len2 + 1 ); - - if( fullName != NULL) + if( dir[dir_len - 1] != '/' ) { - memcpy( fullName, dir, len1 ); - if( dir[len1-1] != '/' ) - fullName[len1++] = '/'; - memcpy( fullName + len1, file, len2 + 1 ); + len = dir_len + 1 + file_len + 1; + fmt = "%s/%s"; + } + else + { + len = dir_len + file_len + 1; + fmt = "%s%s"; } + fullName = malloc( len ); + if( fullName != NULL ) + snprintf( fullName, len, fmt, dir, file ); return fullName; }