我正在尝试获取/dev
给定路径名的相应文件,有点像df
命令
我目前使用这个命令,但我宁愿不使用system()
函数或系统特定的命令。
这是当前代码:
const string PathStr (Path); // Path is input value
// get the /dev/* files
string Cmd ("df 2> /dev/null | grep /dev | awk '{print $1, $6}'");
FILE * Pipe = popen (Cmd.c_str(), "r");
if (!Pipe)
{
errorLog("Cannot execute command : \"" + Cmd + '"');
return string();
}
char Buf [BufSz];
stringstream sstr;
while (!feof(Pipe))
{
if (fgets(Buf, BufSz, Pipe))
sstr << Buf;
}
pclose(Pipe);
// get the actual /dev file
pair<string, string> DiskPaths;
while (!sstr.eof())
{
string Dev, MountPoint;
sstr >> Dev >> MountPoint;
if (string::npos != PathStr.find(MountPoint) &&
MountPoint.size() > DiskPaths.second.size())
DiskPaths = make_pair(Dev, MountPoint);
}