Jump to content

search for executable files

- - - - -

  • Please log in to reply
2 replies to this topic

#1
zhenya

zhenya

    Newbie

  • Members
  • PipPip
  • 13 posts
I have an assignment:

Quote

Write a program that is in a given directory and all subdirectories of all executable files. Range (min. max.) File size specified by the user as the first and second command line argument. The name of the directory specified by the user as a third command line argument. The program displays the search results to a file (the fourth argument to the command line) as a full path, file name, size. The console displays the total number of viewed files. Search procedure for each subdirectory must be run in a separate process. Each process displays a pid, the full path name and size of the scanned file, the total number of scanned files in a subdirectory. Number of running processes at any one time shall not exceed N (entered by the user). Check the program for the directory / usr / Size 31000 31500 N = 6 ..

Like all searches and all subdirectories climbs, but in the end, after the withdrawal of the result, the message "Segmentation fault". What's wrong?


 
char s1[255];char s2[255];
char s4[255];
int z=0;
 
void *func_thread(void *param)
{
DIR *dp,*pdp;
struct dirent *d;
struct stat file;
int x=0;
dp=opendir((char*)param);
while(d=readdir(dp))
{
char path[255];
strcpy(path,(char*)param);
strcat(path,"/");
strcat(path,d->d_name);
stat(path,&file);
pdp=opendir(path);
if(pdp!=0) { if((strcmp(d->d_name,"..")!=0) && (strcmp(d->d_name,".")!=0)) func_thread(path);}
 
if((!pdp)&&(file.st_size>atoi(s1))&&(file.st_size<atoi(s2))&&(((file.st_mode&S_IXUSR)!=0)))
{
printf("%u %s %s %d\n",pthread_self(),(char*)param,d->d_name, (int)file.st_size);
FILE *fp;
fp=fopen(s4,"a");
fprintf(fp,"%s %s %d ",(char*)param,d->d_name,(int)file.st_size);
if((file.st_mode & S_IRUSR)!=0) fprintf(fp,"r"); else fprintf(fp,"-");
if((file.st_mode & S_IWUSR)!=0) fprintf(fp,"w"); else fprintf(fp,"-");
if((file.st_mode & S_IXUSR)!=0) fprintf(fp,"x"); else fprintf(fp,"-");
 
if((file.st_mode & S_IRGRP)!=0) fprintf(fp,"r"); else fprintf(fp,"-");
if((file.st_mode & S_IWGRP)!=0) fprintf(fp,"w"); else fprintf(fp,"-");
if((file.st_mode & S_IXGRP)!=0) fprintf(fp,"x"); else fprintf(fp,"-");
 
if((file.st_mode & S_IROTH)!=0) fprintf(fp,"r"); else fprintf(fp,"-");
if((file.st_mode & S_IWOTH)!=0) fprintf(fp,"w"); else fprintf(fp,"-");
if((file.st_mode & S_IXOTH)!=0) fprintf(fp,"x"); else fprintf(fp,"-");
fprintf(fp,"\n");
fclose(fp);
x++; 
}
}
z--;}int main(int argc, char **argv) {    FILE *fp;
    fp=fopen(argv[4],"w");
    fclose(fp);
    strcpy(s1,argv[1]);
    strcpy(s2,argv[2]);
    strcpy(s4,argv[4]);
    int N;
    pthread_t threads[255];
    printf("ENTER MAX THREADS: ");
    scanf("%d",&N);
    char files[255][255];
    int count=0;
    DIR *dp,*pdp;
    struct dirent *d;
    struct stat file;
    dp=opendir(argv[3]);
    while(d=readdir(dp)) {
        char path[255];
        strcpy(path,argv[3]);
        strcat(path,"/");
        strcat(path,d->d_name);
        pdp=opendir(path);
        if((pdp) && (strcmp(d->d_name,"..")==1)) {
            strcpy(files[count],path);
            count++;
        }
    }
    int i;
    for(i=0;i<N;i++) {
        pthread_create(&threads[i],NULL,func_thread,(void*)files[i]);
        z++;
    }
    for(i=N;i<count;i++) {
        while(z==N) {}
        pthread_create(&threads[i],NULL,func_thread,(void*)files[i]);
        z++;
    }
    pthread_create(&threads[i+1],NULL,func_thread,(void*)argv[3]);
    pthread_join(threads[i+1]);
 
    return 0;
}

Edited by Alexander, 13 April 2011 - 11:41 AM.
Proper quotation


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You seem to be using a lot of string concatenation and copying, can you be sure that the buffers you have defined are large enough for the strings to copy in to?

Your assignment would be to find where, or what call exactly is creating the segmentation fault.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
zhenya

zhenya

    Newbie

  • Members
  • PipPip
  • 13 posts
Yesterday the start (given by the array large values​​) and all successful, and today there is no - again, "Segmentation fault"!

Edited by zhenya, 14 April 2011 - 05:49 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users