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 algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to