This code give back a segmentatin fault when I try to run it Nothing appears wihle building the file with gcc
If you have any Idea about where to search to fix it give your advise thanks.
necessarly its in the function readrep() that must happen,especialy with opendir() and readdir() but I don't see something wrong, thanks to help
#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<limits.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX 256
FILE* f;
void gowrite(char * chemin){
f=fopen("locate.db.tmp","a+");
int size=strlen(chemin);
char str[MAX];
fwrite(&size,sizeof(int),1,f);
fwrite(str,sizeof(char),size,f);
fclose(f);
}
void readRep(char* str){
struct dirent repStruct;
struct dirent * prepStruct;
struct stat structStat;
struct stat *buff;
buff=&structStat;
prepStruct=&repStruct;
DIR * d=opendir(str);
while((prepStruct=readdir(d))!=NULL){
int x;
char* fi=malloc(sizeof(char)*MAX);
fi=(prepStruct->d_name);
x=stat(fi,buff);
mode_t m;
m=structStat.st_mode;
int test;
test=S_ISREG(m);
if(test==0){
strcat(str,"/");
strcat(str,fi);
readRep(str);
}
else{
char *chemin=realpath(fi,NULL);
gowrite(chemin);
free(chemin);
chemin=NULL;
}
}
}
int main(int argN,char** argv){
char* path=getenv("HOME");
chdir(path);
readRep(".");
rename("locate.db.tmp","locate.db");
exit(0);
}
If you may help me thanks