char* create_indices(char *dir) { // Create a pointer to a directory. DIR *dirPtr; // Create a pointer to a directory entry. struct dirent *dirEntry; // A variable to store the name of the directory. char *duplicate; // String to store the argument for the indexfile. char *finalListFiles = (char *) malloc(sizeof(char *)*40); // Open the directory. dirPtr=opendir(dir); // This loop goes through the given directory, and concatenates the // filenames, except "." and "..", to the string finalListFiles. while (dirPtr!=NULL && (dirEntry=readdir(dirPtr))!=NULL) { if (strcmp(dirEntry->d_name,".")!=0 && strcmp(dirEntry->d_name,"..")!=0) { // Create the duplicate of the string containing directory name. duplicate=strdup(dir); // Get the name of a unique file in the given directory. strncat(duplicate,dirEntry->d_name,sizeof(dirEntry->d_name)); // Concatenate the indexfile argument (i.e. finalListFiles), // directory name, filename, and a space to act as delimiter. strncat(strncat(finalListFiles," ",1), duplicate, strlen(duplicate)); } } // Close the directory. closedir(dirPtr); // Return the argument for an indexfile. return finalListFiles; }