changeset 1632:aafd2f28245a draft

When you include the posix header libgen.h, glibc #defines basename to some random other symbol name (because gnu) and this screws up nontrivial macro expansions of NEWTOY(basename), so work around it in portability.h.
author Rob Landley <rob@landley.net>
date Wed, 31 Dec 2014 16:22:31 -0600
parents 44e86486a57d
children 556d8cbfeb08
files lib/portability.c lib/portability.h toys.h toys/posix/dirname.c
diffstat 4 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lib/portability.c	Tue Dec 30 14:49:02 2014 -0600
+++ b/lib/portability.c	Wed Dec 31 16:22:31 2014 -0600
@@ -6,6 +6,14 @@
 
 #include "toys.h"
 
+#if defined(__GLIBC__)
+#include <libgen.h>
+char *basename(char *path)
+{
+  return __xpg_basename(path);
+}
+#endif
+
 #if !defined(__uClinux__)
 pid_t xfork(void)
 {
--- a/lib/portability.h	Tue Dec 30 14:49:02 2014 -0600
+++ b/lib/portability.h	Wed Dec 31 16:22:31 2014 -0600
@@ -60,6 +60,15 @@
 #include <time.h>
 char *strptime(const char *buf, const char *format, struct tm *tm);
 
+// They didn't like posix basename so they defined another function with the
+// same name and if you include libgen.h it #defines basename to something
+// else (where they implemented the real basename), and that define breaks
+// the table entry for the basename command. They didn't make a new function
+// with a different name for their new behavior because gnu.
+//
+// Implement our own in portability.c and don't use their broken header.
+char *basename(char *path);
+
 // uClibc pretends to be glibc and copied a lot of its bugs, but has a few more
 #if defined(__UCLIBC__)
 #include <unistd.h>
--- a/toys.h	Tue Dec 30 14:49:02 2014 -0600
+++ b/toys.h	Wed Dec 31 16:22:31 2014 -0600
@@ -17,7 +17,6 @@
 #include <grp.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <libgen.h>
 #include <math.h>
 #include <pwd.h>
 #include <regex.h>
--- a/toys/posix/dirname.c	Tue Dec 30 14:49:02 2014 -0600
+++ b/toys/posix/dirname.c	Wed Dec 31 16:22:31 2014 -0600
@@ -16,6 +16,7 @@
 */
 
 #include "toys.h"
+#include <libgen.h>
 
 void dirname_main(void)
 {