From 0140f7f28a09d33dc46531212d095da5e021b027 Mon Sep 17 00:00:00 2001 From: Lyonel Vincent Date: Fri, 15 Jan 2021 00:30:13 +0100 Subject: [PATCH 46/65] code clean-up get rid of the basename() mess --- src/core/hw.cc | 2 +- src/core/osutils.cc | 10 ++++++++++ src/core/osutils.h | 1 + src/core/pci.cc | 5 ++--- src/core/sysfs.cc | 21 ++++++++++----------- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/core/hw.cc b/src/core/hw.cc index 6aea7cf..1c1dad4 100644 --- a/src/core/hw.cc +++ b/src/core/hw.cc @@ -1083,7 +1083,7 @@ void hwNode::setLogicalName(const string & name) This->logicalnames.push_back("/dev/" + n); } else - This->logicalnames.push_back((n[0]=='/')?n:basename(n.c_str())); + This->logicalnames.push_back((n[0]=='/')?n:shortname(n)); if(This->dev == "") This->dev = get_devid(n); diff --git a/src/core/osutils.cc b/src/core/osutils.cc index a53ed89..8cce54c 100644 --- a/src/core/osutils.cc +++ b/src/core/osutils.cc @@ -455,6 +455,16 @@ string dirname(const string & path) return result; } +string shortname(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..75c42e3 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 shortname(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 21b9033..a1dd5c7 100644 --- a/src/core/pci.cc +++ b/src/core/pci.cc @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -1172,9 +1171,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", shortname(drivername)); if(exists(modulename)) - device->setConfig("module", basename(const_cast(modulename.c_str()))); + device->setConfig("module", shortname(modulename)); if(exists(string(devices[i]->d_name)+"/rom")) { diff --git a/src/core/sysfs.cc b/src/core/sysfs.cc index d7a20c9..fda1e9b 100644 --- a/src/core/sysfs.cc +++ b/src/core/sysfs.cc @@ -16,7 +16,6 @@ #include #include #include -#include __ID("@(#) $Id$"); @@ -104,7 +103,7 @@ static string sysfs_getbustype(const string & path) { string devname = string(fs.path + "/bus/") + string(namelist[i]->d_name) + - "/devices/" + basename(const_cast(path.c_str())); + "/devices/" + shortname(path); if (samefile(devname, path)) { @@ -152,7 +151,7 @@ static string sysfstobusinfo(const string & path) if (bustype == "usb") { - string name = basename(const_cast(path.c_str())); + string name = shortname(path); if (matches(name, "^[0-9]+-[0-9]+(\\.[0-9]+)*:[0-9]+\\.[0-9]+$")) { size_t colon = name.rfind(":"); @@ -163,7 +162,7 @@ static string sysfstobusinfo(const string & path) if (bustype == "virtio") { - string name = basename(const_cast(path.c_str())); + string name = shortname(path); if (name.compare(0, 6, "virtio") == 0) return "virtio@" + name.substr(6); else @@ -171,10 +170,10 @@ static string sysfstobusinfo(const string & path) } if (bustype == "vio") - return string("vio@") + basename(const_cast(path.c_str())); + return string("vio@") + shortname(path); if (bustype == "ccw") - return string("ccw@") + basename(const_cast(path.c_str())); + return string("ccw@") + shortname(path); if (bustype == "ccwgroup") { @@ -252,7 +251,7 @@ string entry::driver() const string driverlink = This->devpath + "/driver"; if (!exists(driverlink)) return ""; - return basename(const_cast(readlink(driverlink).c_str())); + return shortname(readlink(driverlink)); } @@ -340,7 +339,7 @@ string entry::name_in_class(const string & classname) const string entry::name() const { - return basename(const_cast(This->devpath.c_str())); + return shortname(This->devpath); } @@ -352,17 +351,17 @@ entry entry::parent() const string entry::classname() const { - return basename(const_cast(dirname(This->devpath).c_str())); + return shortname(dirname(This->devpath)); } string entry::subsystem() const { - return basename(const_cast(realpath(This->devpath+"/subsystem").c_str())); + return shortname(realpath(This->devpath+"/subsystem")); } bool entry::isvirtual() const { - return string(basename(const_cast(dirname(dirname(This->devpath)).c_str()))) == "virtual"; + return shortname(dirname(dirname(This->devpath))) == "virtual"; } string entry::string_attr(const string & name, const string & def) const -- 2.33.1