-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetalldevicenames.cpp
More file actions
33 lines (30 loc) · 908 Bytes
/
Copy pathgetalldevicenames.cpp
File metadata and controls
33 lines (30 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <fstream>
#include <sstream>
#include <limits>
#include <iostream>
//Overwrites standart IO template
std::istream& skip_line(std::istream& in) {
return in.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
int main (int argc, const char* argv[]) {
std::ifstream partitions("/proc/partitions", std::ios::binary);
if (!partitions.is_open()) {
return false;
}
std::string line;
std::string dev_name;
std::stringstream ss;
std::size_t pos = 0;
partitions >> skip_line;
partitions >> skip_line;
ss << partitions.rdbuf();
while (std::getline(ss, line, '\n')) {
if ((pos = line.find("sd")) != std::string::npos || (pos = line.find("hd")) != std::string::npos) {
dev_name = line.substr(pos, (line.length() - 1));
if (dev_name.length() == 3) {
std::cerr << dev_name << '\n'; // do what you want with device name
}
}
}
return 0;
}