Whoops, I wrote argv[0] instead of s a couple of times. Ignore this patch. I'll submit another momentarily.
On Wed, Dec 03, 2014 at 05:37:16PM -0500, Brandon Mulcahy wrote: > Skipped suffix treatment if the result of basename(3) is "/", per POSIX. > > Fixed the suffix check, which was previously checking for a match > at any location in the string. Also, strstr used to segfault on: > > basename '' . > > --- > basename.c | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/basename.c b/basename.c > index 402be17..b4c451f 100644 > --- a/basename.c > +++ b/basename.c > @@ -18,6 +18,7 @@ int > main(int argc, char *argv[]) > { > char *s, *p; > + size_t d; > > ARGBEGIN { > default: > @@ -28,10 +29,13 @@ main(int argc, char *argv[]) > usage(); > > s = basename(argv[0]); > - if (argc == 2) { > - p = strstr(s, argv[1]); > - if (p && p[strlen(p)] == '\0') > - *p = '\0'; > + if (argc == 2 && *s != '/') { > + d = strlen(argv[0]) - strlen(argv[1]); > + if (d >= 0) { > + p = argv[0] + d; > + if (strcmp(p, argv[1]) == 0) > + *p = '\0'; > + } > } > puts(s); > return 0; > -- > 2.1.3 > >