Re: [dev] [PATCH] [sbase] Fix basename suffix treatment

From: Brandon Mulcahy <brandon_AT_jangler.info>
Date: Wed, 3 Dec 2014 18:17:29 -0500

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
> _AT_@ -18,6 +18,7 @@ int
> main(int argc, char *argv[])
> {
> char *s, *p;
> + size_t d;
>
> ARGBEGIN {
> default:
> _AT_@ -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
>
>
Received on Thu Dec 04 2014 - 00:17:29 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 04 2014 - 00:24:07 CET