#include #include #include #include using namespace std; struct deviceData{ string model; string manufacturer; short rotationRate; //in rpm short capacity; //in Gigabytes }; string removeLastNewLine(string s); deviceData getDeviceData(string path); /* * * clear && g++ -Wall -o test mainTest.cpp * */ int main(void) { cout << "Hello World!\n"; char * cLine = NULL; string path; size_t len = 0; int loop = 0; int devicesSize = 0; string devices[5]; FILE* outputfileHwinfo = popen("hwinfo --short --disk", "r"); if (outputfileHwinfo == NULL) { exit(EXIT_FAILURE); } while ((getline(&cLine, &len, outputfileHwinfo)) != -1) { if(loop > 0) { string line = string(cLine); path = line.substr (2,8); devices[devicesSize] = path; devicesSize++; } loop++; } fclose(outputfileHwinfo); cout << "Model: " << getDeviceData(devices[0]) << endl; return 0; } deviceData getDeviceData(string path) { size_t len = 0; //lenght of found line char * cLine = NULL; //found line string comand = ("smartctl -a " + path); const char *cComand = comand.c_str(); FILE* outputfileSmart = popen(cComand, "r"); while ((getline(&cLine, &len, outputfileSmart)) != -1) { string line = string(cLine); string search ("Device Model:"); size_t found = line.find(search); if (found!=string::npos) { int lenght = line.length(); //lenght of line string str3 (":"); found = line.find(str3); //cout << "Found1: " << found << endl; for(int i = (found+1); i < lenght; i++) { if(line[i] != ' ') { // cout << i << endl; found = i; break; } } string model = line.substr (found, lenght); model = removeLastNewLine(model); // cout << model << endl; return model; } } fclose(outputfileSmart); return "error"; } string removeLastNewLine(string s) { if (!s.empty() && s[s.length()-1] == '\n') { s.erase(s.length()-1); } return s; }