Skip to content

Commit 0140f7f

Browse files
committed
code clean-up
get rid of the basename() mess
1 parent d3c66a6 commit 0140f7f

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

src/core/hw.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ void hwNode::setLogicalName(const string & name)
10831083
This->logicalnames.push_back("/dev/" + n);
10841084
}
10851085
else
1086-
This->logicalnames.push_back((n[0]=='/')?n:basename(n.c_str()));
1086+
This->logicalnames.push_back((n[0]=='/')?n:shortname(n));
10871087

10881088
if(This->dev == "")
10891089
This->dev = get_devid(n);

src/core/osutils.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,16 @@ string dirname(const string & path)
455455
return result;
456456
}
457457

458+
string shortname(const string & path)
459+
{
460+
size_t len = path.length();
461+
char *buffer = new char[len + 1];
462+
path.copy(buffer, len);
463+
buffer[len] = '\0';
464+
string result = basename(buffer);
465+
delete[] buffer;
466+
return result;
467+
}
458468

459469
string spaces(unsigned int count, const string & space)
460470
{

src/core/osutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bool samefile(const std::string & path1, const std::string & path2);
1515
std::string readlink(const std::string & path);
1616
std::string realpath(const std::string & path);
1717
std::string dirname(const std::string & path);
18+
std::string shortname(const std::string & path);
1819
bool loadfile(const std::string & file, std::vector < std::string > &lines);
1920

2021
size_t splitlines(const std::string & s,

src/core/pci.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <sys/stat.h>
1010
#include <fcntl.h>
1111
#include <stdint.h>
12-
#include <libgen.h>
1312
#include <unistd.h>
1413
#include <stdio.h>
1514
#include <string.h>
@@ -1172,9 +1171,9 @@ bool scan_pci(hwNode & n)
11721171
string drivername = readlink(string(devices[i]->d_name)+"/driver");
11731172
string modulename = readlink(string(devices[i]->d_name)+"/driver/module");
11741173

1175-
device->setConfig("driver", basename(const_cast<char *>(drivername.c_str())));
1174+
device->setConfig("driver", shortname(drivername));
11761175
if(exists(modulename))
1177-
device->setConfig("module", basename(const_cast<char *>(modulename.c_str())));
1176+
device->setConfig("module", shortname(modulename));
11781177

11791178
if(exists(string(devices[i]->d_name)+"/rom"))
11801179
{

src/core/sysfs.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <sys/stat.h>
1717
#include <sys/types.h>
1818
#include <sys/mount.h>
19-
#include <libgen.h>
2019

2120

2221
__ID("@(#) $Id$");
@@ -104,7 +103,7 @@ static string sysfs_getbustype(const string & path)
104103
{
105104
string devname =
106105
string(fs.path + "/bus/") + string(namelist[i]->d_name) +
107-
"/devices/" + basename(const_cast<char*>(path.c_str()));
106+
"/devices/" + shortname(path);
108107

109108
if (samefile(devname, path))
110109
{
@@ -152,7 +151,7 @@ static string sysfstobusinfo(const string & path)
152151

153152
if (bustype == "usb")
154153
{
155-
string name = basename(const_cast<char*>(path.c_str()));
154+
string name = shortname(path);
156155
if (matches(name, "^[0-9]+-[0-9]+(\\.[0-9]+)*:[0-9]+\\.[0-9]+$"))
157156
{
158157
size_t colon = name.rfind(":");
@@ -163,18 +162,18 @@ static string sysfstobusinfo(const string & path)
163162

164163
if (bustype == "virtio")
165164
{
166-
string name = basename(const_cast<char*>(path.c_str()));
165+
string name = shortname(path);
167166
if (name.compare(0, 6, "virtio") == 0)
168167
return "virtio@" + name.substr(6);
169168
else
170169
return "virtio@" + name;
171170
}
172171

173172
if (bustype == "vio")
174-
return string("vio@") + basename(const_cast<char*>(path.c_str()));
173+
return string("vio@") + shortname(path);
175174

176175
if (bustype == "ccw")
177-
return string("ccw@") + basename(const_cast<char*>(path.c_str()));
176+
return string("ccw@") + shortname(path);
178177

179178
if (bustype == "ccwgroup")
180179
{
@@ -252,7 +251,7 @@ string entry::driver() const
252251
string driverlink = This->devpath + "/driver";
253252
if (!exists(driverlink))
254253
return "";
255-
return basename(const_cast<char*>(readlink(driverlink).c_str()));
254+
return shortname(readlink(driverlink));
256255
}
257256

258257

@@ -340,7 +339,7 @@ string entry::name_in_class(const string & classname) const
340339

341340
string entry::name() const
342341
{
343-
return basename(const_cast<char*>(This->devpath.c_str()));
342+
return shortname(This->devpath);
344343
}
345344

346345

@@ -352,17 +351,17 @@ entry entry::parent() const
352351

353352
string entry::classname() const
354353
{
355-
return basename(const_cast<char*>(dirname(This->devpath).c_str()));
354+
return shortname(dirname(This->devpath));
356355
}
357356

358357
string entry::subsystem() const
359358
{
360-
return basename(const_cast<char*>(realpath(This->devpath+"/subsystem").c_str()));
359+
return shortname(realpath(This->devpath+"/subsystem"));
361360
}
362361

363362
bool entry::isvirtual() const
364363
{
365-
return string(basename(const_cast<char*>(dirname(dirname(This->devpath)).c_str()))) == "virtual";
364+
return shortname(dirname(dirname(This->devpath))) == "virtual";
366365
}
367366

368367
string entry::string_attr(const string & name, const string & def) const

0 commit comments

Comments
 (0)