complained = 0;
for (t = argv; *t;) {
- if ((p = strrchr(*t, '/')) != NULL)
- ++p;
- else
+ size_t len = strlen(*t);
+ char truncated[len];
+
+ if ((p = strrchr(*t, '/')) != NULL) {
+ if (p[1] == '\0') { // one or more trailing / -- treat as if not present
+ for (; (p > *t) && (p[-1] == '/');) {
+ len--;
+ p--;
+ }
+ strlcpy(truncated, *t, len);
+ p = strrchr(truncated, '/');
+ if (p) {
+ ++p;
+ } else {
+ p = truncated;
+ }
+ } else {
+ ++p;
+ }
+ } else {
p = *t;
+ }
if (ISDOT(p)) {
if (!complained++)
warnx("\".\" and \"..\" may not be removed");