Advertisement
Guest User

Untitled

a guest
Sep 14th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <vector>
  5. #include <string>
  6. #include <iostream>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. bool is_number(const string& s)
  13. {
  14. std::string::const_iterator it = s.begin();
  15. while (it != s.end() && std::isdigit(*it)) ++it;
  16. return !s.empty() && it == s.end();
  17. }
  18.  
  19. int main()
  20. {
  21. DIR *dir;
  22. struct dirent *ent;
  23.  
  24. if ((dir = opendir ("/proc")) != NULL)
  25. {
  26. /* print all the files and directories within directory */
  27. while ((ent = readdir (dir)) != NULL)
  28. {
  29. char *s = ent->d_name;
  30. // Check to see if the directory is for a process
  31. if (is_number(s))
  32. {
  33. // Read from cmdline and print to console
  34. char *cmdLinePath = "/proc/";
  35. strcat(cmdLinePath, s);
  36. strcat(cmdLinePath, "/cmdline");
  37. ifstream myfile (cmdLinePath);
  38. string line;
  39. if (myfile.is_open())
  40. {
  41. cout << "File is open" <<endl;
  42. while ( getline (myfile,line) )
  43. {
  44. cout << line << '\n';
  45. }
  46. }
  47. myfile.close();
  48. cout << "A Process" << endl;
  49. }
  50. cout << s << endl;
  51. }
  52. closedir (dir);
  53. } else {
  54. /* could not open directory */
  55.  
  56. }
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement