LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Searching ' / ' and ' /user ' directory (https://www.linuxquestions.org/questions/linux-general-1/searching-%27-%27-and-%27-user-%27-directory-939860/)

mudassar192 04-15-2012 12:44 AM

Searching ' / ' and ' /user ' directory
 
Hi
I am working on a project in which I have to search '/ ' and ' /user ' directory . I have written a c++ code which works well on any other directory but when i give search directory to ' / ' or ' /user ' it displays an error "SEGMENTATION FAULT"

Any Solution

catkin 04-15-2012 01:19 AM

A solution is more likely if you post your code.

salasi 04-15-2012 08:55 AM

I think the original post is almost humorous - if you want detailed advice, how could we do anything with that level of information?

If you want the non-detailed advice:
  • Find the problem and fix it.

Perhaps something to think about is why a program could work on some directories and not others. Possibly, your program doesn't cope very well when directory (or file) permissions do not let it access the files/directories that are there.

mudassar192 04-15-2012 10:14 AM

Thanks But no thanks on your advice

salasi 04-15-2012 03:00 PM

Well, good luck.

You'll need it, if you aren't going to cheat by either finding the cause and putting it right, or posting a sufficient level of detail that someone else can understand what exactly what you are doing.

mudassar192 04-16-2012 04:20 PM

Reply
 
I will post the code on site on 27 of this month as its my term project and its deadline is on 26th of April .if I post the code now it may create many complications for me .But meanwhile I will try to find out the reason and solution to this problem .

mudassar192 04-30-2012 03:45 PM

CODE
 
// This Code Runs on Some Computers on '/' and '/home' and on some it DOESNOT




// the Program takes Command line Arguments
pattern = fileName No_OF_THREAD PATH SEARCH_WoRD
e.g
./a.out 3 /home/user word






#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <pthread.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
using namespace std;


// Global Veriables
int counter=0;
int thread=0;
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
char word[20];
pthread_t *array;



class linklist
{
private:
// This Linklist for
struct file
{
char filePath[100];
file * nextFile;
};
struct folder
{
char folderPath[100];
file *fileHead;
folder * nextFolder;
bool threadAllocated;
};
static folder* head;
static folder* tail;



public:
linklist(char path[])
{
head =NULL;
tail = NULL;
DIR *Test = opendir(path);
if (Test == NULL)
{
cout<<"Invalid PATH Given\n";
}
else
{


// Add Node for the Main directory
folder *temp = new folder;
strcpy(temp->folderPath,path);
temp->fileHead=NULL;
temp->nextFolder=NULL;
temp->threadAllocated = false;
head = temp;
tail = temp;

folder * current = head;

do
{
Test = opendir(current->folderPath);
// checking that if Directory specified exists
//dirent *dirTest = readdir(Test);

if (Test == NULL)
{
cout<<"End Of Directories\n";
}
else
{
char dummyPath[200];
struct dirent *selection;
while ( (selection = readdir(Test) ) != NULL)
{
strcpy(dummyPath,current->folderPath);
struct stat fstat;
if (dummyPath[ strlen(dummyPath)-1 ] != '/')
{
strcat(dummyPath,"/");
}
strcat(dummyPath,selection->d_name);

if (!(strcmp(selection->d_name,".") == 0 || strcmp(selection->d_name,"..") == 0 || stat (dummyPath,&fstat) <0 ))
{
if (S_ISDIR(fstat.st_mode) )
{
counter++;
// Add node for folder

folder *temp = new folder;
strcpy(temp->folderPath,dummyPath);
temp->fileHead=NULL;
temp->nextFolder=NULL;
temp->threadAllocated = false;

if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->nextFolder = temp;
tail = temp;
}

}
else
{
// Add node for file
file * temp = new file;
strcpy(temp->filePath,dummyPath);
temp->nextFile = NULL;

if (current->fileHead == NULL)
current->fileHead = temp;
else
{
file * temp2 = current->fileHead;
while (temp2->nextFile != NULL)
temp2=temp2->nextFile;

temp2->nextFile = temp;
}


}

}
}
closedir(Test);
}
current = current->nextFolder;
}while (current != NULL );
}

}


//Display list

void display()
{
cout<<"\n\n\nDisplay\n";
if (head == NULL)
cout<<"LinkList Empty\n";
else
{
folder * node = head;
while (node)
{
cout<<"\n\nFOlder :\t"<<node->folderPath<<endl;
if (node->fileHead == NULL)
cout<<"No files in this Folder\n";
else
{
file * temp = node->fileHead;
while (temp)
{
cout<<"\t"<<temp->filePath<<endl;
temp = temp->nextFile;
}
}
node = node->nextFolder;
}
}
cout<<"Count = \t"<<counter<<endl;
}



static void* function(void * )
{
if (head == NULL)
{
cout<<"LinkList EMPTY NO SEARCH\n";
}
else
{
int i=0;
for (i=0;i<thread;i++)
{
if (array[i] == pthread_self())
break;
}
folder*temp = head;

while (temp)
{
pthread_mutex_lock(&lock);
while (temp && temp->threadAllocated == true)
temp = temp->nextFolder;
if (temp)
{
//cout<<"\nNode Allocated =\t"<<temp->folderPath<<endl;
temp->threadAllocated = true;
}
pthread_mutex_unlock(&lock);

if (!(temp))
{
pthread_mutex_lock(&lock);
cout<<"\n\nThread [ "<<i<<" ] has finished Searching\n\n";
pthread_mutex_unlock(&lock);
}
else
{
pthread_mutex_lock(&lock);
cout<<"\nThread [ "<<i<<" ]\tFolder Allocated =\t"<<temp->folderPath<<endl;
pthread_mutex_unlock(&lock);
// cout<<"File head \t"<<temp->fileHead<<"\t"<<(temp->fileHead == NULL)<<endl;
if (temp->fileHead == NULL)
{
cout<<"Empty Folder\n";
}
else
{
// cout<<"Inner Loop\n";
file * currentFile = temp->fileHead;
//cout<<"Thread [ "<<i<<" ]file is\t"<<currentFile<<endl;
while (currentFile != 0)
{
cout<<"file is\t[ "<<i<<" ]\t"<<currentFile->filePath<<endl;
// Checking File Extension (.cpp or .txt ...)
if( strstr(currentFile->filePath,".cpp")!= NULL || strstr(currentFile->filePath,".txt")!= NULL )
{
int linecount=0;
ifstream input;
input.open(currentFile->filePath);
string data;
char * line;

while ( !(input.eof()) )
{
getline(input,data);
linecount++;
line = &data[0];
line =strstr(line,word);

if (line)
{
pthread_mutex_lock(&lock);
cout<<currentFile->filePath<<"\t";
cout<<linecount<<" \t"<<data<<endl;
pthread_mutex_unlock(&lock);
}

}
input.close();

}
currentFile = currentFile->nextFile;
cout<<"File Incremented\n";
}
//cout<<"Hello\n";
}
//cout<<"Temp[1] is \t"<<temp<<(temp == NULL)<<endl;
}
//sleep(1);
//cout<<"Temp is (Sleep)\t"<<temp<<(temp == NULL)<<endl;
}
}

}






void search()
{
array =new pthread_t[thread];
for (int i = 0 ; i<thread;i++)
{
// cout<<"Loop Iteration\t"<<i<<endl;
pthread_create(&array[i],NULL,function,NULL);
}

sleep(2);
cout<<"Number Of SubDirectories are\t"<<counter<<endl;
}

};

linklist::folder * linklist::head = NULL;
linklist::folder * linklist::tail = NULL;
int main(int arg , char *argv[])
{
char *inputPath = argv[2];

// Checking if Input Path Exists;

if (! (inputPath) )
{
cout<<"No Path Given\n";
}
else
{
DIR *pathTest = opendir(inputPath);
if (pathTest == NULL)
{
cout<<"PAth DOes Not EXist\n";
}
else
{
if (argv[1])
{
thread = atoi((char *)argv[1]);
if (thread <0)
{
cout<<"Invalid Threads\n";
}
else
{
//cout<<"Argc 3 is\t"<<argv[3]<<endl;
if (argv[3])
{
strcpy(word,argv[3]);
linklist c1(inputPath);
system("clear");
// c1.display();
c1.search();
}
else
{
cout<<"Nothin Given for search\n";
}
}
}

}
}


}


All times are GMT -5. The time now is 10:56 PM.