[dev] [PATCH] [sbase] Fix basename suffix treatment

From: Brandon Mulcahy <brandon_AT_jangler.info>
Date: Wed, 3 Dec 2014 17:37:16 -0500

Skipped suffix treatment if the result of basename(3) is "/", per POSIX.

Fixed the suffix check, which was previously checking for a match
at any location in the string. Also, strstr used to segfault on:

        basename '' .

---
 basename.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/basename.c b/basename.c
index 402be17..b4c451f 100644
--- a/basename.c
+++ b/basename.c
_AT_@ -18,6 +18,7 @@ int
 main(int argc, char *argv[])
 {
 	char *s, *p;
+	size_t d;
 
 	ARGBEGIN {
 	default:
_AT_@ -28,10 +29,13 @@ main(int argc, char *argv[])
 		usage();
 
 	s = basename(argv[0]);
-	if (argc == 2) {
-		p = strstr(s, argv[1]);
-		if (p && p[strlen(p)] == '\0')
-			*p = '\0';
+	if (argc == 2 && *s != '/') {
+		d = strlen(argv[0]) - strlen(argv[1]);
+		if (d >= 0) {
+			p = argv[0] + d;
+			if (strcmp(p, argv[1]) == 0)
+				*p = '\0';
+		}
 	}
 	puts(s);
 	return 0;
-- 
2.1.3
Received on Wed Dec 03 2014 - 23:37:16 CET

This archive was generated by hypermail 2.3.0 : Wed Dec 03 2014 - 23:48:07 CET