Blame SOURCES/0046-code-clean-up.patch

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