简体   繁体   中英

getting segmentation fault during execution

I am trying to make a tree structure for a specific directory. the path of the directory is entered by the user and is passed to opendir func . the readdir func reads the current directory and is recursively reading subdirectories. I am unable to dubug this program.

#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<malloc.h>
#include<sys/stat.h>
#include<limits.h> 
#include<stdlib.h>
#define _GNU_SOURCE
struct tree{
    char dname[100];
    char fname[200];
    int i;
    struct tree *openf[100];

};

void getinto(char [],char [],struct tree*);
struct dirent *dpointer;
int found=0;
int k=0;

_Bool is_dir(const char* path) {
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

int main() {
char path[100]=".";
DIR *dr;
struct tree *rootnode;
rootnode=(struct tree*)malloc(sizeof(struct tree));
printf("enter path :: ");
scanf("%s",path);
//printf("helllo\n");
rootnode->i=0;
dr=opendir(path);
//printf("helllo\n");
strcpy(rootnode->dname,path);
if((dpointer=readdir(dr))==NULL){
    printf("current directory is empty !!");


}
while ((dpointer=readdir(dr))!=NULL){
    struct tree *rootchild;
    rootchild=(struct tree*)malloc(sizeof(struct tree));
    rootnode->openf[rootnode->i++]=rootchild;
    if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
    continue;
    //printf("helllo\n");
    if(is_dir(dpointer->d_name)){
        printf("%s is directory \n",dpointer->d_name);
        getinto(dpointer->d_name,path,rootchild);
        //printf("helllo loop\n");
        printf("%s is directory \n",dpointer->d_name);

    }
    else{
        strcpy(rootchild->dname,dpointer->d_name);
        //printf("helllo loop\n");
        printf("%s is file \n",dpointer->d_name);
    }
}
closedir(dr);
return 0;
}

void getinto(char sfilename[],char spath[],struct tree* this){
char filename[100],currentpath[100],temp[100];
DIR *d=opendir(currentpath);
strcpy(filename,sfilename);
strcpy(currentpath,spath);
strcat(currentpath,"/");
strcat(currentpath,filename);
printf("helllo function\n");
d=opendir(currentpath);
//printf("helllo function\n");
this->i=0;
while((dpointer=readdir(d))!=NULL){
    struct tree *child;
    child=(struct tree*)malloc(sizeof(struct tree));
    this->openf[this->i++]=child;
    if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
    continue;
    //printf("helllo function loop\n");
    if(is_dir(dpointer->d_name)){
        printf("%s is directory \n",dpointer->d_name);
        getinto(dpointer->d_name,currentpath,child);
        //printf("helllo loop\n");
        printf("%s is directory \n",dpointer->d_name);

    }
    else{
        strcpy(child->dname,dpointer->d_name);
        //printf("helllo loop\n");
        printf("%s is file \n",dpointer->d_name);
    }
}
closedir(d);

}

Every time I execute it ends up with a segmentation fault: Segmentation fault (core dumped)

I am expecting it to cleanly create a dynamic tree with data of each node as name of file or directory.

In

_Bool is_dir(const char* path) {
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

stat needs pathname of type char* .

Syntax:

int stat(const char *pathname, struct stat *statbuf);

but you are passing

if(is_dir((const char*)dr))

dr of type DIR .

Simply change the is_dir calls as below.

is_dir(dpointer->d_name) //in main as well as in getinto function

Also readdir will return the entries including

.   //current dir
..  //parent dir

Hence you need to skip these two entries in main otherwise your opendir in getinfo will fail causing readdir in getinfo crash.

So skip these entries in main as below.

while ((dpointer=readdir(dr))!=NULL){
    if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
    continue;
   .....
   .....
  }

this code finally worked perfectly for me '

#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<malloc.h>
#include<sys/stat.h>
#include<limits.h> 
#include<stdlib.h>
#define _GNU_SOURCE
struct tree{
    char dname[1000];
    char fname[2000];
    int i;
    struct tree *openf[1000];

};

void getinto(char [],char [],struct tree*);
struct dirent *dpointer;
int found=0;
int k=0;

_Bool is_dir(const char* path) {
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

int main() {
char path[1000]=".";
DIR *dr;
struct tree *rootnode;
rootnode=(struct tree*)malloc(sizeof(struct tree));
printf("enter path :: ");
scanf("%s",path);
//printf("helllo\n");
rootnode->i=0;
dr=opendir(path);
//printf("helllo\n");
strcpy(rootnode->dname,path);
if((dpointer=readdir(dr))==NULL){
    printf("current directory is empty !!");


}
while ((dpointer=readdir(dr))!=NULL){
    struct tree *rootchild;
    rootchild=(struct tree*)malloc(sizeof(struct tree));
    rootnode->openf[rootnode->i++]=rootchild;
    if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
    continue;
    //printf("helllo\n");
    if(is_dir(dpointer->d_name)){
        printf("%s is directory \n",dpointer->d_name);
        getinto(dpointer->d_name,path,rootchild);
        //printf("helllo loop\n");
        //printf("%s is directory \n",dpointer->d_name);

    }
    else{
        strcpy(rootchild->dname,dpointer->d_name);
        //printf("helllo loop\n");
        printf("%s is file \n",dpointer->d_name);
    }
}
closedir(dr);
return 0;
}

void getinto(char sfilename[],char spath[],struct tree* this){
char filename[1000],currentpath[1000],temp[1000];
DIR *d=opendir(currentpath);
strcpy(filename,sfilename);
strcpy(currentpath,spath);
strcat(currentpath,"/");
strcat(currentpath,filename);
printf("%s",currentpath);
printf("\nhelllo function\n");
d=opendir(currentpath);
//printf("helllo function\n");
this->i=0;
while((dpointer=readdir(d))!=NULL){
    struct tree *child;
    child=(struct tree*)malloc(sizeof(struct tree));
    this->openf[this->i++]=child;
     if (strcmp(dpointer->d_name,"..")==0 || strcmp(dpointer->d_name,".")==0 ) 
     continue;
    //printf("helllo function loop\n");
    if(is_dir(dpointer->d_name)){
        printf("%s is directory \n",dpointer->d_name);
        //getinto(dpointer->d_name,currentpath,child);
        //printf("helllo loop\n");
        printf("%s is directory \n",dpointer->d_name);

    }
    else{
        strcpy(child->dname,dpointer->d_name);
        //printf("helllo loop\n");
        printf("%s is file \n",dpointer->d_name);
    }
}
printf("\niits over!!");
closedir(d);
return ;

}

' thanks for the help y'all !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM