I am coding this program to just save the directory names into a linked list. I THINK have done that successfully (because it prints the strings correctly right after it saves it". My issue now is that my function printData only prints the first element of the list. I guess the "next" element is null. I am not sure why it won't print the whole list. can anyone send me feedback what the issue is iterating through this linked list. I would really appreciate it. Code is below
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
struct path{
char *fp;
struct fs* filestats;
struct path* next;
};
struct filestats{
char *fname;
int fsize;
struct filestats* next;
};
void openSubDir(const char *dName);
void printData(struct path *end);
int tcount;
struct path *end;
struct path *start;
struct path *temp;
int main(int argc, char *argv[]) {
tcount=0;
int storeData(const char *, const struct stat *, int);
ftw(argv[1], storeData, 5);
//start = end;
printData(start);
//system("PAUSE");
return 0;
} /* End of main. */
int storeData(const char *name, const struct stat *sbuf, int ftype){
end = malloc(sizeof(struct path)); //allocate space for the record pointer
memset(end, 0, sizeof(struct path)); //sets up pointer so rec has some space that can be filled up
if(ftype == FTW_D){ //if the object is a directory
if(start == NULL){
start = end;
start->fp = malloc(strlen(name));
strcpy(start->fp, name);
end = start->next;
}
else{
//end->next = NULL;
end->fp = malloc(strlen(name));
strcpy(end->fp, name);
//printf("%s\n", end->fp);
end= end->next;
}
}
tcount++;
return 0;
}
void printData(struct path *endF){
struct path *temp;
temp = endF;
while(temp != NULL){
if(temp->fp != NULL){
printf("%s\n", temp->fp);
}
temp = temp-> next;
}
}