Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <dirent.h>
- #include <errno.h>
- #include <vector>
- #include <string>
- #include <iostream>
- #include <fstream>
- using namespace std;
- bool is_number(const string& s)
- {
- std::string::const_iterator it = s.begin();
- while (it != s.end() && std::isdigit(*it)) ++it;
- return !s.empty() && it == s.end();
- }
- int main()
- {
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir ("/proc")) != NULL)
- {
- /* print all the files and directories within directory */
- while ((ent = readdir (dir)) != NULL)
- {
- char *s = ent->d_name;
- // Check to see if the directory is for a process
- if (is_number(s))
- {
- // Read from cmdline and print to console
- char *cmdLinePath = "/proc/";
- strcat(cmdLinePath, s);
- strcat(cmdLinePath, "/cmdline");
- ifstream myfile (cmdLinePath);
- string line;
- if (myfile.is_open())
- {
- cout << "File is open" <<endl;
- while ( getline (myfile,line) )
- {
- cout << line << '\n';
- }
- }
- myfile.close();
- cout << "A Process" << endl;
- }
- cout << s << endl;
- }
- closedir (dir);
- } else {
- /* could not open directory */
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement