LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-15-2012, 12:44 AM   #1
mudassar192
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Rep: Reputation: Disabled
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
 
Old 04-15-2012, 01:19 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
A solution is more likely if you post your code.
 
Old 04-15-2012, 08:55 AM   #3
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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.
 
Old 04-15-2012, 10:14 AM   #4
mudassar192
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks But no thanks on your advice
 
Old 04-15-2012, 03:00 PM   #5
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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.
 
Old 04-16-2012, 04:20 PM   #6
mudassar192
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 04-30-2012, 03:45 PM   #7
mudassar192
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Exclamation 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";
}
}
}

}
}


}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script Searching file in a directory pratapsingh Programming 15 02-25-2011 02:42 PM
[SOLVED] Searching a directory and pulling out filenames with a certain pattern LadyAnne Linux - Newbie 5 05-17-2010 09:30 AM
What else besides POOLING a directory searching for files? richikiki Programming 1 08-19-2006 11:43 AM
setting bash to look in current directory before searching the path muhkuhmasta Linux - Newbie 4 09-21-2004 02:08 AM
Quicky: searching for files in directory hierarchy leeman_s Linux - General 1 01-25-2003 10:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration