int dir_contents(const char *folderName, char ** result) {
struct dirent *entry;
DIR *dir = NULL;
dir = opendir(folderName);
if (!dir) {
printf("ERROR open dir:\n\tLine:%d\n\tFunction:%s\n",__LINE__,__func__);
return 0;
}
*result = (char*)malloc(256 * sizeof(char));
int i = 0;
while ((entry = readdir(dir))) {
i++;
char *line = NULL;
line = (char*)malloc(256 * sizeof(char));
sprintf(line, "%s:%d\n", entry->d_name, entry->d_type);
printf("%d:%s", i, line);
strncat(*result, line, strlen(line) + 1);
free(line);
}
closedir(dir);
dir = NULL;
return 1;
}
This code is a basic function to read a directory and returns by reference a string with all the contents of a directory