1 /* libSoX minimal glob for MS-Windows: (c) 2009 SoX contributors
3 * This library is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or (at
6 * your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11 * General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "win32-glob.h"
22 #define WIN32_LEAN_AND_MEAN 1
25 typedef struct file_entry
28 struct file_entry
*next
;
38 file_entry
* cur
= malloc(sizeof(file_entry
));
44 len
= _snprintf(cur
->name
, MAX_PATH
, "%s%s", path
, name
);
45 cur
->name
[MAX_PATH
- 1] = 0;
49 return len
< 0 || len
>= MAX_PATH
? ENAMETOOLONG
: 0;
57 const file_entry
* const * pe1
= pv1
;
58 const file_entry
* const * pe2
= pv2
;
59 return _stricmp((*pe1
)->name
, (*pe2
)->name
);
70 file_entry
*head
= NULL
;
74 WIN32_FIND_DATAA finddata
;
77 if (!pattern
|| flags
!= (flags
& GLOB_FLAGS
) || unused
|| !pglob
)
83 path
[MAX_PATH
- 1] = 0;
84 strncpy(path
, pattern
, MAX_PATH
);
85 if (path
[MAX_PATH
- 1] != 0)
92 while (len
> 0 && path
[len
- 1] != '/' && path
[len
- 1] != '\\')
96 hfindfile
= FindFirstFileA(pattern
, &finddata
);
97 if (hfindfile
== INVALID_HANDLE_VALUE
)
99 if (flags
& GLOB_NOCHECK
)
101 err
= insert("", pattern
, &head
);
109 err
= insert(path
, finddata
.cFileName
, &head
);
111 } while (!err
&& FindNextFileA(hfindfile
, &finddata
));
113 FindClose(hfindfile
);
118 pglob
->gl_pathv
= malloc((entries
+ 1) * sizeof(char*));
121 pglob
->gl_pathc
= entries
;
122 pglob
->gl_pathv
[entries
] = NULL
;
123 for (; head
; head
= head
->next
, entries
--)
124 pglob
->gl_pathv
[entries
- 1] = (char*)head
;
125 qsort(pglob
->gl_pathv
, pglob
->gl_pathc
, sizeof(char*), entry_comparer
);
136 pglob
->gl_pathv
= NULL
;
162 for (cur
= pglob
->gl_pathv
; *cur
; cur
++)
168 pglob
->gl_pathv
= NULL
;