2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #if defined(__MINGW32__) || defined(__CYGWIN__)
27 static const char dir_separators
[] = "/\\:";
29 static const char dir_separators
[] = "/";
32 char **find_files(const char *original_file
, const char *suffix
,
35 void *tmpmem
= talloc_new(NULL
);
36 char *fname
= talloc_strdup(tmpmem
, original_file
);
37 char *basename
= NULL
;
40 next
= strpbrk(next
, dir_separators
);
55 char **results
= talloc_size(NULL
, 0);
56 DIR *dp
= opendir(directory
);
58 char ***names_by_matchlen
= talloc_array(tmpmem
, char **,
59 strlen(basename
) + 1);
60 memset(names_by_matchlen
, 0, talloc_get_size(names_by_matchlen
));
62 while ((ep
= readdir(dp
))) {
63 int suffix_offset
= strlen(ep
->d_name
) - strlen(suffix
);
64 // name must end with suffix
65 if (suffix_offset
< 0 || strcmp(ep
->d_name
+ suffix_offset
, suffix
))
67 // don't list the original name
68 if (!strcmp(ep
->d_name
, basename
))
71 char *name
= talloc_asprintf(results
, "%s/%s", directory
, ep
->d_name
);
72 char *s1
= ep
->d_name
;
75 while (*s1
&& *s1
++ == *s2
++)
77 int oldcount
= talloc_get_size(names_by_matchlen
[matchlen
]) /
79 names_by_matchlen
[matchlen
] = talloc_realloc(names_by_matchlen
,
80 names_by_matchlen
[matchlen
],
81 char *, oldcount
+ 1);
82 names_by_matchlen
[matchlen
][oldcount
] = name
;
86 results
= talloc_realloc(NULL
, results
, char *, num_results
);
87 char **resptr
= results
;
88 for (int i
= strlen(basename
); i
>= 0; i
--) {
89 char **p
= names_by_matchlen
[i
];
90 for (int j
= 0; j
< talloc_get_size(p
) / sizeof(char *); j
++)
93 assert(resptr
== results
+ num_results
);
95 *num_results_ptr
= num_results
;