remove use of MAX_PATH_CHARACTERS in For_each_file()

This commit is contained in:
Thomas Bernard 2019-01-18 13:49:23 +01:00
parent a95b7504a6
commit cb66bfcde0
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -560,56 +560,54 @@ unsigned long File_length_file(FILE * file)
void For_each_file(const char * directory_name, void Callback(const char *, const char *)) void For_each_file(const char * directory_name, void Callback(const char *, const char *))
{ {
char full_filename[MAX_PATH_CHARACTERS];
#if defined(WIN32) #if defined(WIN32)
WIN32_FIND_DATAA fd; WIN32_FIND_DATAA fd;
char search_string[MAX_PATH_CHARACTERS]; char * full_filename;
char * search_string;
HANDLE h; HANDLE h;
if (Realpath(directory_name, full_filename)) full_filename = Realpath(directory_name, NULL);
_snprintf(search_string, sizeof(search_string), "%s\\*", full_filename); search_string = Filepath_append_to_dir((full_filename != NULL) ? full_filename : directory_name, "*");
else free(full_filename);
_snprintf(search_string, sizeof(search_string), "%s\\*", directory_name);
h = FindFirstFileA(search_string, &fd); h = FindFirstFileA(search_string, &fd);
free(search_string);
if (h != INVALID_HANDLE_VALUE) if (h != INVALID_HANDLE_VALUE)
{ {
do do
{ {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue; continue;
_snprintf(full_filename, sizeof(full_filename), "%s\\%s", directory_name, fd.cFileName); full_filename = Filepath_append_to_dir(directory_name, fd.cFileName);
Callback(full_filename, fd.cFileName); Callback(full_filename, fd.cFileName);
free(full_filename);
} }
while (FindNextFileA(h, &fd)); while (FindNextFileA(h, &fd));
FindClose(h); FindClose(h);
} }
#else #else
// Pour scan de répertoire // directory traversal
DIR* current_directory; //Répertoire courant DIR* current_directory;
struct dirent* entry; // Structure de lecture des éléments struct dirent* entry; // directory entry
int filename_position;
strcpy(full_filename, directory_name); current_directory = opendir(directory_name);
current_directory=opendir(directory_name); if(current_directory == NULL)
if(current_directory == NULL) return; // Répertoire invalide ... return; // Invalid directory
filename_position = strlen(full_filename); while ((entry = readdir(current_directory)) != NULL)
#if defined(__AROS__)
if (filename_position==0 || (strcmp(full_filename+filename_position-1,PATH_SEPARATOR) && strcmp(full_filename+filename_position-1,":")))
#else
if (filename_position==0 || strcmp(full_filename+filename_position-1,PATH_SEPARATOR))
#endif
{ {
strcat(full_filename, PATH_SEPARATOR); char * full_filename;
filename_position = strlen(full_filename); struct stat st;
}
while ((entry=readdir(current_directory))) full_filename = Filepath_append_to_dir(directory_name, entry->d_name);
{ // d_name is the only field you can count on in all POSIX systems.
struct stat Infos_enreg; // Also we need to call stat() in order to follow symbolic links
strcpy(&full_filename[filename_position], entry->d_name); if (stat(full_filename, &st) < 0)
stat(full_filename,&Infos_enreg); GFX2_Log(GFX2_WARNING, "stat(\"%s\") failed\n", full_filename);
if (S_ISREG(Infos_enreg.st_mode)) else
{ {
Callback(full_filename, entry->d_name); if (S_ISREG(st.st_mode))
Callback(full_filename, entry->d_name);
} }
free(full_filename);
} }
closedir(current_directory); closedir(current_directory);
#endif #endif