@@ -484,6 +484,8 @@ hrmp_get_files(char* base, bool recursive, struct list* files)
484484{
485485 DIR * dir = NULL ;
486486 struct dirent * entry ;
487+ char * * names = NULL ;
488+ size_t names_size = 0 ;
487489
488490 if (base == NULL || files == NULL )
489491 {
@@ -498,19 +500,39 @@ hrmp_get_files(char* base, bool recursive, struct list* files)
498500
499501 while ((entry = readdir (dir )) != NULL )
500502 {
501- char * d = NULL ;
502-
503503 if (strcmp (entry -> d_name , "." ) == 0 || strcmp (entry -> d_name , ".." ) == 0 )
504504 {
505505 continue ;
506506 }
507507
508+ char * * tmp = realloc (names , (names_size + 1 ) * sizeof (char * ));
509+ if (tmp == NULL )
510+ {
511+ goto error ;
512+ }
513+
514+ names = tmp ;
515+ names [names_size ] = strdup (entry -> d_name );
516+ if (names [names_size ] == NULL )
517+ {
518+ goto error ;
519+ }
520+
521+ names_size ++ ;
522+ }
523+
524+ hrmp_sort (names_size , names );
525+
526+ for (size_t i = 0 ; i < names_size ; i ++ )
527+ {
528+ char * d = NULL ;
529+
508530 d = hrmp_append (d , base );
509531 if (!hrmp_ends_with (d , "/" ))
510532 {
511533 d = hrmp_append_char (d , '/' );
512534 }
513- d = hrmp_append (d , entry -> d_name );
535+ d = hrmp_append (d , names [ i ] );
514536
515537 if (hrmp_is_file (d ))
516538 {
@@ -522,8 +544,11 @@ hrmp_get_files(char* base, bool recursive, struct list* files)
522544 }
523545
524546 free (d );
547+ free (names [i ]);
525548 }
526549
550+ free (names );
551+
527552 closedir (dir );
528553
529554 return 0 ;
@@ -535,6 +560,15 @@ hrmp_get_files(char* base, bool recursive, struct list* files)
535560 closedir (dir );
536561 }
537562
563+ if (names != NULL )
564+ {
565+ for (size_t i = 0 ; i < names_size ; i ++ )
566+ {
567+ free (names [i ]);
568+ }
569+ free (names );
570+ }
571+
538572 return 1 ;
539573}
540574
0 commit comments