From 8fa3366615f197e5c2b66b16029d42817965fa52 Mon Sep 17 00:00:00 2001
From: Ron Yorston <rmy@pobox.com>
Date: Sat, 31 Mar 2018 13:14:05 +0100
Subject: libbb: improve stripping of extension from argv[0]

Use library routines to reduce the amount of code needed to strip
the extension from argv[0] in the BusyBox main function.

The name of the executable can now have any of the standard
extensions, none at all, or a trailing dot.

Related to GitHub issue #20.
---
 libbb/appletlib.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

(limited to 'libbb')

diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 491eb4208..210f9ff4d 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -1142,24 +1142,20 @@ int main(int argc UNUSED_PARAM, char **argv)
 	applet_name = argv[0];
 	if (applet_name[0] == '-')
 		applet_name++;
-	if (ENABLE_PLATFORM_MINGW32) {
-		if ( argv[1] && argv[2] && strcmp(argv[1], "--busybox") == 0 ) {
-			argv += 2;
-			applet_name = argv[0];
-		}
-		else {
-			char *s = argv[0];
-			int i, len = strlen(s);
+# if ENABLE_PLATFORM_MINGW32
+	if ( argv[1] && argv[2] && strcmp(argv[1], "--busybox") == 0 ) {
+		argv += 2;
+		applet_name = argv[0];
+	}
+	else {
+		char *s;
 
-			for ( i=0; i < len; ++i ) {
-				s[i] = tolower(s[i]);
-			}
-			if (len > 4 && !strcmp(s+len-4, ".exe")) {
-				len -= 4;
-				s[len] = '\0';
-			}
+		str_tolower(argv[0]);
+		if (has_exe_suffix_or_dot(argv[0]) && !(s=strrchr(argv[0], '.'))) {
+			*s = '\0';
 		}
 	}
+# endif
 	applet_name = bb_basename(applet_name);
 
 	/* If we are a result of execv("/proc/self/exe"), fix ugly comm of "exe" */
-- 
cgit v1.2.3-55-g6feb