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