#include "fs.h" int countFiles(char *path){ DIR *dp; int i = 0; struct dirent *ep; dp = opendir (path); if (dp != NULL){ while (ep = readdir (dp)) i++; (void) closedir (dp); } else{ fprintf(stderr, "Unable to open directory \"%s\".\n", path); } return i - 2; } char **listFiles(char *path){ int nfiles = countFiles(path); if(nfiles < 0){ fprintf(stderr, "There are no files in the specified assets directory.\n"); return 0; } DIR *dp; struct dirent *ep; dp = opendir(path); if (dp != NULL) { char **strings = (char **) malloc(sizeof(char **) * nfiles); int i = 0; // For assigning the string to the string array /* print all the files and directories within directory */ while ((ep = readdir (dp)) != NULL) { if(strcmp(ep->d_name,".") && strcmp(ep->d_name,"..")){ // All this means is allocate space for the length of the string // plus the \0 character having in mind the size of each character strings[i] = malloc((strlen(ep->d_name) + strlen(path) + 1) * sizeof(char)); //strcpy(strings[i],path); strcpy(strings[i],path); strcat(strings[i],ep->d_name); i++; // if not inside it will give size error since it counts . and .. if(i == nfiles) return strings; } } closedir (dp); } else { /* could not open directory */ perror ("could not open directory\n"); return 0; } }