Move strend() to lib/lib.c
diff --git a/lib/lib.c b/lib/lib.c
index f677614..ceced26 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -392,6 +392,15 @@
   return (idx == -1) ? 0 : to[idx];
 }
 
+char *strend(char *str, char *suffix)
+{
+  long a = strlen(str), b = strlen(suffix);
+
+  if (a>b && !strcmp(str += a-b, suffix)) return str;
+
+  return 0;
+}
+
 // If *a starts with b, advance *a past it and return 1, else return 0;
 int strstart(char **a, char *b)
 {
diff --git a/lib/lib.h b/lib/lib.h
index d3f9b1d..efdc1fa 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -206,6 +206,7 @@
 char *strafter(char *haystack, char *needle);
 char *chomp(char *s);
 int unescape(char c);
+char *strend(char *str, char *suffix);
 int strstart(char **a, char *b);
 off_t fdlength(int fd);
 void loopfiles_rw(char **argv, int flags, int permissions,
diff --git a/toys/posix/basename.c b/toys/posix/basename.c
index c123cc7..0436bfe 100644
--- a/toys/posix/basename.c
+++ b/toys/posix/basename.c
@@ -23,12 +23,7 @@
   char *base = basename(*toys.optargs), *suffix = toys.optargs[1];
 
   // chop off the suffix if provided
-  if (suffix && *suffix) {
-    long bl = strlen(base), sl = strlen(suffix);
-    char *s = base + bl - sl;
-
-    if (bl > sl && !strcmp(s, suffix)) *s = 0;
-  }
+  if (suffix && *suffix && (suffix = strend(base, suffix))) *suffix = 0;
 
   puts(base);
 }