Hey,
Remembered of my OS assignment and wanted to fix it :). Though do not
appreciate the design of the program :D, and not having the enthu to
redesign it, I have fixed the program with minimal possible changes. Please
reply in case of any issues. Thnx for posting :)
#include <iostream>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>
#define FOUND 1
#define MAX_DIR_PATH 1000
using namespace std;
int f =0;
class Search {
// data members
private :
DIR* dptr;
struct dirent* dir;
string dirname;
int count;
// instance methods
public :
Search() {
count =0;
}
char * scan_dir(string file, string str) {
struct stat sfile;
DIR * dptr = opendir(str.c_str());
if( dptr == NULL) {
cout << "Error(" << errno << ") opening " << str << endl;
return "Error";
}
while(f != FOUND && ((dir = readdir(dptr)) != NULL)) {
if (dir->d_name[0] == '.') { // hidden files and all
continue;
}
char path_[MAX_DIR_PATH];path_[0]=0;
strcpy(path_, str.c_str());
strcat(path_, "/");
strcat(path_, dir->d_name);
cout << "Abs Name :" << path_ << endl;
if ((lstat(path_, &sfile)) == 0) {
if(S_ISDIR(sfile.st_mode)) {
if ((strcmp(dir->d_name,".")==0)
|| (strcmp(dir->d_name, "..") == 0) )
continue;
char cwdir[MAX_DIR_PATH];cwdir[0]=0;
strcpy(cwdir, path_);
char * result = scan_dir(file, cwdir);
if (f == FOUND) return result;
chdir(path_);
} else if ((S_ISREG(sfile.st_mode)) &&
!strcmp(dir->d_name,file.c_str())) {
cout << endl<< "::: FOUND FILE :::";
f = FOUND;
return path_;
}
}
}// eow
char temp[MAX_DIR_PATH];temp[0]=0;
strcmp(temp, str.c_str());
return temp;
}//scan_dir
};// end of class decln
int main() {
string file, directory;
cout << "Name of file:";
cin >> file;
cout << " Name of root directory: ";
cin >> directory;
Search *S = new Search();
cout << "SEARCHING for " << file << " from root dir: " << directory <<
"\n";
string name = S->scan_dir(file, directory);
if (f != FOUND)
cout << "FILE NOT FOUND";
else
cout << "Found at: " << name << "\n";
return 0;
}
Programmers should realize their critical importance and responsibility in a
world gone digital. They are in many ways similar to the priests and monks
of Europe's Dark Ages; they are the only ones with the training and insight
to read and interpret the "scripture" of this age.
On Thu, Jan 6, 2011 at 6:33 PM, janani thiru <[email protected]> wrote:
>
>
> I have written a program to search for a file in C in UNIX.
> But it doesn't loop recursively if the starting path is "/"
> Could it be because of permissions? But I am the root while executing the
> program and I have also changed the permissions of all the files in the
> folder correspondingly.
>
> Please help.
>
> #include <iostream>
> #include <dirent.h>
> #include <errno.h>
> #include <string.h>
> #include <sys/stat.h>
> #include <sys/types.h>
> #include <vector>
> #define FOUND 1
> #define MAX_DIR_PATH 1000
> using namespace std;
> int f =0;
> class Search
> {
>
> // data members
> private :
> DIR* dptr; DIR* prior;
> struct dirent* dir;
> string dirname;
> struct stat sfile;
> int count;
>
> // instance methods
> public :
> Search()
> {
> count =0;
> }
>
> ~Search()
> {
> closedir(dptr);
> }
>
> void setdname(string str)
> {
> dirname = str;
> }
>
>
> int scan_dir(string file, string str)
> {
> cout <<endl<<":call";
> dptr = opendir(str.c_str());
> if( dptr == NULL)
> {
> cout << "Error(" << errno << ") opening " << dirname <<
> endl;
> return errno;
> }
>
> while( (dir = readdir(dptr)) != NULL)
> {
>
> cout << endl << dir->d_name<<endl;
>
> if((lstat(dir->d_name,&sfile))==0)
> {
>
> if(S_ISDIR(sfile.st_mode))
> {
> char cwdir[MAX_DIR_PATH];
>
> if ((strcmp(dir->d_name,".")==0) || (strcmp(dir->d_name,
> "..") == 0) )
> continue;
>
> cout <<endl<<"In directory"<<"\t"<< dir->d_name<<endl;
> getcwd(cwdir,MAX_DIR_PATH+1);
>
> str = cwdir;
>
> strcat(cwdir,"/");
> strcat(cwdir,dir->d_name);
>
> cout << endl << "cwdir concat"<<"\t"<<cwdir;
> scan_dir(file,cwdir);
>
> chdir(str.c_str());
> }
>
> else if ((S_ISREG(sfile.st_mode)) &&
> !strcmp(dir->d_name,file.c_str()))
> {
> cout <<endl<< "::: FOUND FILE :::";
> f = FOUND;
> break;
> }
> }
> }// eow
> }//scan_dir
> };// end of class decln
> int main()
> {
> string file;
> cout << "Name of file:"<<endl;
> cin >> file;
>
> Search *S = new Search();
>
> cout << "SEARCHING for " << file <<endl<<endl;
> cout<<"scan . dir"<<endl;
> S->scan_dir(file,"/home/janani/Desktop/Junk/ListFiles");
>
> if (f != FOUND)
> cout << "FILE NOT FOUND";
> }
>
>
> --
> Janani T
>
>
>
> --
> Janani T
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.