1*4882a593SmuzhiyunFrom d667b13a87cf3207599a19eb981a893a1d7a67ee Mon Sep 17 00:00:00 2001 2*4882a593SmuzhiyunFrom: Brendan Heading <brendanheading@gmail.com> 3*4882a593SmuzhiyunDate: Mon, 14 Sep 2015 23:25:52 +0100 4*4882a593SmuzhiyunSubject: [PATCH 1/1] ibrcommon/data/File.cpp: support POSIX basename call 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunFirstly, and somewhat strangely, musl chooses not to provide a basename(3) 7*4882a593Smuzhiyunprototype within <string.h> whenever __cplusplus is defined. This can be 8*4882a593Smuzhiyunsolved by including the <libgen.h> header defined by POSIX 1003.1 whenever 9*4882a593Smuzhiyun__GLIBC__ is not defined. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunHowever, this leads to a second problem. POSIX defines the function as 12*4882a593Smuzhiyunchar* basename(char*) and this is the only version supported by musl. 13*4882a593SmuzhiyunHowever, the std::string.cstr() method returns a const char*. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunPOSIX says that the string parameter can be modified. However the GNU 16*4882a593Smuzhiyunimplementation never modifies it. glibc therefore supports an extension 17*4882a593Smuzhiyunwhen compiling under C++ by also supplying 18*4882a593Smuzhiyunconst char* basename(const char*). This extension is not present on musl 19*4882a593Smuzhiyunwhich is the cause of the failure. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunThe solution is reasonably straightforward; test if __GLIBC__ is defined 22*4882a593Smuzhiyunbefore calling basename. If not, use the fallback already provided for 23*4882a593Smuzhiyunother platforms whereby basename() is called on a temporary copy. 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunSigned-off-by: Brendan Heading <brendanheading@gmail.com> 26*4882a593SmuzhiyunUpstream-status: pending 27*4882a593Smuzhiyun--- 28*4882a593Smuzhiyun ibrcommon/data/File.cpp | 4 ++-- 29*4882a593Smuzhiyun 1 file changed, 2 insertions(+), 2 deletions(-) 30*4882a593Smuzhiyun 31*4882a593Smuzhiyundiff --git a/ibrcommon/data/File.cpp b/ibrcommon/data/File.cpp 32*4882a593Smuzhiyunindex 31af4ae..68e9b4f 100644 33*4882a593Smuzhiyun--- a/ibrcommon/data/File.cpp 34*4882a593Smuzhiyun+++ b/ibrcommon/data/File.cpp 35*4882a593Smuzhiyun@@ -35,7 +35,7 @@ 36*4882a593Smuzhiyun #include <cerrno> 37*4882a593Smuzhiyun #include <fstream> 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun-#if !defined(HAVE_FEATURES_H) || defined(ANDROID) 40*4882a593Smuzhiyun+#if !defined(HAVE_FEATURES_H) || !defined(__GLIBC__) || defined(ANDROID) 41*4882a593Smuzhiyun #include <libgen.h> 42*4882a593Smuzhiyun #endif 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun@@ -225,7 +225,7 @@ namespace ibrcommon 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun std::string File::getBasename() const 47*4882a593Smuzhiyun { 48*4882a593Smuzhiyun-#if !defined(ANDROID) && defined(HAVE_FEATURES_H) 49*4882a593Smuzhiyun+#if !defined(ANDROID) && defined(HAVE_FEATURES_H) && defined(__GLIBC__) 50*4882a593Smuzhiyun return std::string(basename(_path.c_str())); 51*4882a593Smuzhiyun #else 52*4882a593Smuzhiyun char path[_path.length()+1]; 53*4882a593Smuzhiyun-- 54*4882a593Smuzhiyun2.4.3 55*4882a593Smuzhiyun 56