Skip to contentSkip to conversation

Avoid using GNU basename

PATCH New Keith Mok 4 participants 9 messages Feb 28, 2012 – Mar 26, 2012 1 message not in the archive

lore
Share & tools
lore

Keith Mok

author
#0 lore raw

PATCH 1/1 Avoid using GNU basename

There is no GNU basename support in MacOS.
Use generic POSIX basename defined in libgen.h instead.

---
 tools/mkenvimage.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
tools/mkenvimage.c +4 -4
index f781731..8b49723 100644
@@ -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;
Signature · 1 line
1.7.7.4
#1 lore raw

PATCH 1/1 Avoid using GNU basename

Quoted 2 lines · 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120229/c9d26b8c/attachment.pgp>

#2 lore raw

PATCH 1/1 Avoid using GNU basename

In reply to Keith Mok

Dear Keith Mok,

Quoted 2 lines · In message <CAHjoi4eR+n=qQ8vZAKwwgLiPSFfrBWHRHwYNvfJn4y0F9EqO7g@mail.gmail.com>…

There is no GNU basename support in MacOS.
Use generic POSIX basename defined in libgen.h instead.

...

tools/mkenvimage.c int main(int argc, char **argv) · line 89

Quoted 7 lines
        int fp, ep;
        const char *prg;
tools/mkenvimage.c +3 -1
+       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

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Drun'? 'm not drun'! You woudn' dare call m' drun' if I was sober!
                                     - Terry Pratchett, _Men at Arms_
author
#3 lore raw

PATCH 1/1 Avoid using GNU basename

Hi Wolfgang,

Quoted 2 lines

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 '/'

Regards,
Keith

#4 lore raw

PATCH 1/1 Avoid using GNU basename

Dear Keith Mok,

Quoted 5 lines · In message <CAHjoi4dV3gP1XqoH5vUy_NJAFouAx3HbTt-bHqfP2nrt-2o39w@mail.gmail.com>…
Quoted 3 lines

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

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Whom the gods would destroy, they first teach BASIC.

A message that is not in the archive (sent off-list or not yet received): CAHjoi4eRTxXJKwg_XsEvtELNQmDzfqHzZiRO=MY-jfVYvDUrgA@mail.gmail.com

#6 lore raw

PATCH 1/1 Avoid using GNU basename

Dear Keith,

please keep the mailing list on Cc:

Quoted 7 lines · In message <CAHjoi4eRTxXJKwg_XsEvtELNQmDzfqHzZiRO=MY-jfVYvDUrgA@mail.gmail.com>…
Quoted 5 lines

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).

So should we stick on using libgen provided basename to maximize the compatibility of different platforms ?

I don't think so. As the comment in "/usr/include/libgen.h" suggests, we should use "the weird XPG version of this function" only when really needed. And we definitely don;t need to do that on GNU/Linux systems.

Best regards,

Wolfgang Denk

Signature · 4 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Hindsight is an exact science.
#7 lore raw

PATCH 1/1 mkenvimage: fix usage message

In reply to Keith Mok

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.

---
 tools/mkenvimage.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/mkenvimage.c +1 -1
index f781731..64f5340 100644
@@ -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);
Signature · 1 line
1.7.7.6
#8 lore raw

PATCH 1/1 mkenvimage: fix usage message

Quoted 1 line · 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

Quoted 2 lines

by the previous call to basename(). Use "prg" instead as it is done in the rest of the code.

-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120302/2371121d/attachment.pgp>

#9 lore raw

PATCH 1/1 mkenvimage: fix usage message

In reply to Wolfgang Denk

Hello Wolfgang,

Quoted 8 lines · On Fri, 2 Mar 2012 08:19:40 +0100 Wolfgang Denk <[email protected]> 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.

---
 tools/mkenvimage.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Applied to u-boot-staging.

Thanks,
Anatolij