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