[U-Boot] [PATCH] Avoid using GNU basename

There is no GNU basename support in MacOS. Use generic POSIX basename defined in libgen.h instead.
Signed-off-by: Keith Mok ek9852@gmail.com --- tools/mkenvimage.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index f781731..8b49723 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -25,9 +25,6 @@ * MA 02111-1307 USA */
-/* We want the GNU version of basename() */ -#define _GNU_SOURCE - #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -35,6 +32,7 @@ #include <string.h> #include <unistd.h> #include <compiler.h> +#include <libgen.h> #include <sys/types.h> #include <sys/stat.h>
@@ -85,8 +83,10 @@ int main(int argc, char **argv)
int fp, ep; const char *prg; + char *prog_pathname;
- prg = basename(argv[0]); + prog_pathname = strdup(argv[0]); + prg = basename(prog_pathname);
/* Turn off getopt()'s internal error message */ opterr = 0;

On Tuesday 28 February 2012 10:36:22 Keith Mok wrote:
There is no GNU basename support in MacOS. Use generic POSIX basename defined in libgen.h instead.
alternative: define a non-braindead version in compiler.h:
static const char *_basename(const char *filename) { const char *p = strrchr(filename, '/'); return p ? p + 1 : filename; }
/* Avoid issues with clobbering C library def */ #undef basename #define basename(x) _basename(x) -mike

Dear Keith Mok,
In message CAHjoi4eR+n=qQ8vZAKwwgLiPSFfrBWHRHwYNvfJn4y0F9EqO7g@mail.gmail.com you wrote:
There is no GNU basename support in MacOS. Use generic POSIX basename defined in libgen.h instead.
...
int fp, ep; const char *prg;
char *prog_pathname;
prg = basename(argv[0]);
prog_pathname = strdup(argv[0]);
prg = basename(prog_pathname);
free() missing. Actually the strdup should not be needed at all, as we don't use argv[0] after that - at least we shouldn't. The remaining "usage(argv[0]);" should be fixed instead.
Besides - I agree with Mike's comment how your MacOS issue should be fixed.
Best regards,
Wolfgang Denk

Dear Keith Mok,
In message CAHjoi4dV3gP1XqoH5vUy_NJAFouAx3HbTt-bHqfP2nrt-2o39w@mail.gmail.com you wrote:
Besides - I agree with Mike's comment how your MacOS issue should be fixed.
But will Mike's fix break Windows user which cross-compile u-boot ? The path separator is '' instead of '/'
I'm not a windows expert (actually I'm proud of never having worked with Windows in my whole life), but I think it is only the command line interface that insists of using '\' (because DOS used '/' as option switch).
Best regards,
Wolfgang Denk

Don;t use argv[0] for usage() because it may or may not be clobbered by the previous call to basename(). Use "prg" instead as it is done in the rest of the code.
Signed-off-by: Wolfgang Denk wd@denx.de --- tools/mkenvimage.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index f781731..64f5340 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -123,7 +123,7 @@ int main(int argc, char **argv) case ':': fprintf(stderr, "Missing argument for option -%c\n", optopt); - usage(argv[0]); + usage(prg); return EXIT_FAILURE; default: fprintf(stderr, "Wrong option -%c\n", optopt);

On Friday 02 March 2012 02:19:40 Wolfgang Denk wrote:
Don;t use argv[0] for usage() because it may or may not be clobbered
Don't
by the previous call to basename(). Use "prg" instead as it is done in the rest of the code.
Acked-by: Mike Frysinger vapier@gentoo.org -mike

Hello Wolfgang,
On Fri, 2 Mar 2012 08:19:40 +0100 Wolfgang Denk wd@denx.de wrote:
Don;t use argv[0] for usage() because it may or may not be clobbered by the previous call to basename(). Use "prg" instead as it is done in the rest of the code.
Signed-off-by: Wolfgang Denk wd@denx.de
tools/mkenvimage.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
Applied to u-boot-staging.
Thanks, Anatolij
participants (4)
-
Anatolij Gustschin
-
Keith Mok
-
Mike Frysinger
-
Wolfgang Denk