0
votes

I wanna traverse inside the file system by using threads and processes.My program has to assume the first parameter is either given as "-p" which offers a multi-process application or "-t" which runs in a multi-threaded way. The second parameter is the pathname of a file or directory. If my program gets the path of a file, it should print out the size of the file in bytes. If my program gets the path of a directory, it should, in the same way, print out the directory name, then process all the entries in the directory except the directory itself and the parent directory. If my program is given a directory, it must display the entire hierarchy rooted at the specified directory. I wrote something but I got stuck in.I can not improve my code.Please help me.

My code is as following:

include

include

include

include

include

include

include

int funcThread(DIR *D);

int main(int argc, char * argv[]) { pthread_t thread[100]; DIR *dirPointer; struct stat object_file; struct dirent *object_dir; int counter;

    if(opendir(argv[1])==NULL)
{
    printf("\n\nERROR !\n\n Please enter -p or -t \n\n");
    return 0;
}

if((dirPointer=opendir(argv[1]))=="-t") 
{
    if ((object_dir = opendir(argv[2])) == NULL) 
        {
                printf("\n\nERROR !\n\nPlease enter the third argument\n\n");
                return 0;.
        }
    else
    {   
        counter=0;
        while ((object_dir = readdir(object_dir)) != NULL)
        {
            pthread_create(&thread[counter],NULL,funcThread,(void *) object_dir);
            counter++;
        }

    }

}

return 0; }

int funcThread(DIR *dPtr) { DIR *ptr; struct stat oFile; struct dirent *oDir; int num;

if(ptr=readdir(dPtr)==NULL)
    rewinddir(ptr); 

if(S_ISDIR(oFile.st_mode)) 
{
    ptr=readdir(dPtr);
    printf("\t%s\n",ptr);
    return funcThread(ptr);
}
else
{
    while(ptr=readdir(dPtr)!=NULL) 
    {
        printf("\n%s\n",oDir->d_name);
        stat(oDir->d_name,&oFile);
        printf("\n%f\n",oFile.st_size);
    }
    rewinddir(ptr); 
}

}

3

3 Answers

1
votes

This line:

if((dirPointer=opendir(argv[1]))=="-t") 

dirPointer is a pointer DIR* so how can it be equal to a literal string pointer?

0
votes

I spotted a few errors:

  1. Why are you using opendir() to check your arguments? You should use something like strcmp for that.

  2. You're passing struct dirent* to funcThread() but funcThread() takes a DIR*.

  3. You're using oFile on funcThread() before you initialize it (by calling stat()).

  4. What is the purpose of calling rewinddir()? I guess you're blindly trying to get readdir() to work with a struct dirent*.

  5. You're using oDir but it's never initialized.

  6. You're calling printf() from multiple threads with no means to synchronize the output so it would be completelly out of order or garbled.

I suggest you read and understand the documentation of all those functions before using them (google "posix function_name") and get familiar with the basics of C. And before bringing threads into the equation try to get it working on a single threaded program. Also you won't see an improvement in performance by using that many threads unless you have close to that many cores, it will actually decrease performance and increase resource usage.

0
votes
if(ptr=readdir(dPtr)==NULL){}

The = operator has lower precedence than ==

[this error is repeated several times]