fork download
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <string>
  6. #include <unistd.h>
  7. #include <cstring>
  8. #include <vector>
  9. #include <sys/stat.h>
  10. #include <stdlib.h>
  11. using namespace std;
  12.  
  13. class directories
  14. {
  15. string presentDir;
  16. vector<string> dirToCheck;
  17. bool checkAll;
  18. public:
  19. directories()
  20. {
  21. checkAll=0;
  22. string startPoint;
  23. cout<<"Whats your start point? example:1) \"/\" 2) \"/piotr\""<<endl;
  24. cin>>startPoint;
  25. cout<<endl;
  26. dirToCheck.push_back(startPoint);
  27. }
  28. ~directories()
  29. {
  30. ;
  31. }
  32. bool isSearchDone();
  33. bool canSearch();
  34. string searchPos();
  35. void addDir(string newDir);
  36. };
  37.  
  38. bool directories::isSearchDone()
  39. {
  40. return(!checkAll);
  41. }
  42.  
  43. bool directories::canSearch()
  44. {
  45. if (dirToCheck.empty())
  46. {
  47. checkAll=1;
  48. return(0);
  49. }
  50. else
  51. {
  52. presentDir=dirToCheck[dirToCheck.size()-1];
  53. //cout<<system(("file '"+presentDir+"'").c_str())<<endl;
  54. //cout<<presentDir<<endl;
  55. dirToCheck.pop_back();
  56. return(1);
  57. }
  58. }
  59.  
  60. string directories::searchPos()
  61. {
  62. return(presentDir);
  63. }
  64.  
  65. void directories::addDir(string newDir)
  66. {
  67. dirToCheck.push_back(newDir);
  68. }
  69.  
  70. int main()
  71. {
  72. DIR *dir;
  73. struct dirent *dp;
  74. struct stat statbuf;
  75. string strFile, findName;
  76.  
  77. cout<<"what are you searching for?"<<endl;
  78. cin>>findName;
  79. cout<<endl<<"Found:"<<endl;
  80.  
  81. directories doSearch;
  82.  
  83.  
  84. while(doSearch.isSearchDone())
  85. {
  86. if(doSearch.canSearch())
  87. {
  88. dir= opendir(doSearch.searchPos().c_str());
  89.  
  90. while ((dp = readdir(dir))!=NULL)
  91. {
  92. strFile=dp->d_name;
  93. int pos = strFile.find(findName);
  94. if (pos!=(string::npos))
  95. {
  96. if(doSearch.searchPos()=="/")
  97. {
  98. cout<<doSearch.searchPos()+strFile<<endl;
  99. }
  100. else
  101. {
  102. cout<<doSearch.searchPos()+"/"+strFile<<endl;
  103. }
  104. }
  105.  
  106. if ((strFile!=".")&&(strFile!="..")&&(strFile!="proc")&&(strFile!="sys")&&(strFile!="dev")&&(strFile!="Trash"))
  107. {
  108. if(doSearch.searchPos()=="/")
  109. {
  110. stat((doSearch.searchPos()+strFile).c_str(),&statbuf);
  111. }
  112. else
  113. {
  114. stat((doSearch.searchPos()+"/"+strFile).c_str(),&statbuf);
  115. }
  116. //cout<<stat(dp->d_name,&statbuf)<<endl;
  117. ////////////////////////////////////// PROBLEM /////////////////////////////////////////////
  118. //cout<<S_ISDIR(statbuf.st_mode); // it says its zero tho its a directory wth??? (similary build program works)
  119. if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
  120. {
  121. if(doSearch.searchPos()=="/")
  122. {
  123. doSearch.addDir(doSearch.searchPos()+strFile);
  124. }
  125. else
  126. {
  127. doSearch.addDir(doSearch.searchPos()+"/"+strFile);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. cout<<endl;
  135. }
Runtime error #stdin #stdout 0s 3484KB
stdin
Standard input is empty
stdout
what are you searching for?

Found:
Whats your start point? example:1) "/" 2) "/piotr"