Fix basename suffix treatment

Explicitly use "." instead of the result of basename(3) when argv[0] is
an empty string in order to avoid a segfault.

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

Fix the suffix check, which was previously checking for a match at any
location in the string.
This commit is contained in:
Brandon Mulcahy 2014-12-03 18:37:34 -05:00 committed by sin
parent f141da6190
commit 757cf0651a

View File

@ -18,6 +18,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *s, *p; char *s, *p;
size_t d;
ARGBEGIN { ARGBEGIN {
default: default:
@ -27,12 +28,15 @@ main(int argc, char *argv[])
if (argc < 1) if (argc < 1)
usage(); usage();
s = basename(argv[0]); s = strlen(argv[0]) ? basename(argv[0]) : ".";
if (argc == 2) { if (argc == 2 && *s != '/') {
p = strstr(s, argv[1]); d = strlen(s) - strlen(argv[1]);
if (p && p[strlen(p)] == '\0') if (d >= 0) {
p = s + d;
if (strcmp(p, argv[1]) == 0)
*p = '\0'; *p = '\0';
} }
}
puts(s); puts(s);
return 0; return 0;
} }