sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 1059782af84c435899826436898dd2c2f48a0978
parent e14e0beccef66be52166f27979fedc19283d62be
Author: sin <sin@2f30.org>
Date:   Mon,  8 Dec 2014 12:14:13 +0000

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

Diffstat:
Mbasename.c | 19+++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)

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