Fix comparison >= 0 is always true in basename(1)

This commit is contained in:
sin 2014-12-08 12:14:13 +00:00
parent e14e0becce
commit 1059782af8

View File

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