From 49a2c103c7d1127045ced8e8c887279a36a3f357 Mon Sep 17 00:00:00 2001 From: Felix Janda Date: Wed, 12 Apr 2017 21:29:11 -0400 Subject: [PATCH 1/2] Add wrapper for basename basename comes in two variants. A GNU and a POSIX version. Currently, the GNU version is mostly used, but this breaks compilation on systems without glibc. Switch to the portable version. Because this variant modifies its argument, similarly to dirname, a wrapper is needed. --- src/core/dasd.cc | 4 ++-- src/core/osutils.cc | 10 ++++++++++ src/core/osutils.h | 1 + src/core/pci.cc | 4 ++-- src/core/sysfs.cc | 8 ++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/core/dasd.cc b/src/core/dasd.cc index b62a7ee..3a716ac 100644 --- a/src/core/dasd.cc +++ b/src/core/dasd.cc @@ -19,7 +19,7 @@ using namespace std; bool scan_dasd(hwNode & n) { size_t dev_num; - char *dev_name; + std::string dev_name; glob_t devices; uint64_t capacity; @@ -42,7 +42,7 @@ bool scan_dasd(hwNode & n) { for(dev_num=0;dev_num::iterator it = sysfs_attribs.begin(); it != sysfs_attribs.end(); ++it) { std::string attrib_fname = std::string(SYSFS_PREFIX) + dev_name + "/device/" + *it; diff --git a/src/core/osutils.cc b/src/core/osutils.cc index e93b79e..1624ab1 100644 --- a/src/core/osutils.cc +++ b/src/core/osutils.cc @@ -426,6 +426,16 @@ string dirname(const string & path) return result; } +string basename(const string & path) +{ + size_t len = path.length(); + char *buffer = new char[len + 1]; + path.copy(buffer, len); + buffer[len] = '\0'; + string result = basename(buffer); + delete[] buffer; + return result; +} string spaces(unsigned int count, const string & space) { diff --git a/src/core/osutils.h b/src/core/osutils.h index 549258e..55f5548 100644 --- a/src/core/osutils.h +++ b/src/core/osutils.h @@ -15,6 +15,7 @@ bool samefile(const std::string & path1, const std::string & path2); std::string readlink(const std::string & path); std::string realpath(const std::string & path); std::string dirname(const std::string & path); +std::string basename(const std::string & path); bool loadfile(const std::string & file, std::vector < std::string > &lines); size_t splitlines(const std::string & s, diff --git a/src/core/pci.cc b/src/core/pci.cc index d1625cf..1163ad2 100644 --- a/src/core/pci.cc +++ b/src/core/pci.cc @@ -1131,9 +1131,9 @@ bool scan_pci(hwNode & n) string drivername = readlink(string(devices[i]->d_name)+"/driver"); string modulename = readlink(string(devices[i]->d_name)+"/driver/module"); - device->setConfig("driver", basename(const_cast(drivername.c_str()))); + device->setConfig("driver", basename(drivername)); if(exists(modulename)) - device->setConfig("module", basename(const_cast(modulename.c_str()))); + device->setConfig("module", basename(modulename)); if(exists(string(devices[i]->d_name)+"/rom")) { diff --git a/src/core/sysfs.cc b/src/core/sysfs.cc index 97dbab5..0fc4855 100644 --- a/src/core/sysfs.cc +++ b/src/core/sysfs.cc @@ -99,7 +99,7 @@ static string sysfs_getbustype(const string & path) { devname = string(fs.path + "/bus/") + string(namelist[i]->d_name) + - "/devices/" + basename(path.c_str()); + "/devices/" + basename(path); if (samefile(devname, path)) return string(namelist[i]->d_name); @@ -139,7 +139,7 @@ static string sysfstobusinfo(const string & path) if (bustype == "virtio") { - string name = basename(path.c_str()); + string name = basename(path); if (name.compare(0, 6, "virtio") == 0) return "virtio@" + name.substr(6); else @@ -207,7 +207,7 @@ string entry::driver() const string driverlink = This->devpath + "/driver"; if (!exists(driverlink)) return ""; - return basename(readlink(driverlink).c_str()); + return basename(readlink(driverlink)); } @@ -288,7 +288,7 @@ string entry::name_in_class(const string & classname) const string entry::name() const { - return basename(This->devpath.c_str()); + return basename(This->devpath); }