]> wimlib.net Git - wimlib/blob - programs/imagex-win32.c
3c449a7733b108129503a32b4fbad682e2c2eeae
[wimlib] / programs / imagex-win32.c
1
2 /* Replacements for functions needed specifically by the 'imagex' program in
3  * Windows native builds */
4
5 #ifndef __WIN32__
6 #  error "This file contains Windows code"
7 #endif
8
9 #include "imagex-win32.h"
10 #include <windows.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <assert.h>
14
15
16 /* Replacement for glob() in Windows native builds. */
17 int glob(const char *pattern, int flags,
18          int (*errfunc)(const char *epath, int eerrno),
19          glob_t *pglob)
20 {
21         WIN32_FIND_DATA dat;
22         DWORD err;
23         HANDLE hFind;
24         int ret;
25         size_t nspaces;
26
27         /* This function does not support all functionality of the POSIX glob(),
28          * so make sure the parameters are consistent with supported
29          * functionality. */
30         assert(errfunc == NULL);
31         assert((flags & GLOB_ERR) == GLOB_ERR);
32         assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
33
34         hFind = FindFirstFileA(pattern, &dat);
35         if (hFind == INVALID_HANDLE_VALUE) {
36                 err = GetLastError();
37                 if (err == ERROR_FILE_NOT_FOUND) {
38                         errno = 0;
39                         return GLOB_NOMATCH;
40                 } else {
41                         /* The other possible error codes for FindFirstFile()
42                          * are undocumented. */
43                         errno = EIO;
44                         return GLOB_ABORTED;
45                 }
46         }
47         pglob->gl_pathc = 0;
48         pglob->gl_pathv = NULL;
49         nspaces = 0;
50         do {
51                 char *filename;
52                 if (pglob->gl_pathc == nspaces) {
53                         size_t new_nspaces;
54                         char **pathv;
55
56                         new_nspaces = nspaces * 2 + 1;  
57                         pathv = realloc(pglob->gl_pathv,
58                                         new_nspaces * sizeof(pglob->gl_pathv[0]));
59                         if (!pathv)
60                                 goto oom;
61                         pglob->gl_pathv = pathv;
62                         nspaces = new_nspaces;
63                 }
64                 filename = strdup(dat.cFileName);
65                 if (!filename)
66                         goto oom;
67                 pglob->gl_pathv[pglob->gl_pathc++] = filename;
68         } while (FindNextFileA(hFind, &dat));
69         err = GetLastError();
70         CloseHandle(hFind);
71         if (err == ERROR_NO_MORE_FILES) {
72                 errno = 0;
73                 return 0;
74         } else {
75                 /* Other possible error codes for FindNextFile() are
76                  * undocumented */
77                 errno = EIO;
78                 ret = GLOB_ABORTED;
79                 goto fail_globfree;
80         }
81 oom:
82         CloseHandle(hFind);
83         errno = ENOMEM;
84         ret = GLOB_NOSPACE;
85 fail_globfree:
86         globfree(pglob);
87         return ret;
88 }
89
90 void globfree(glob_t *pglob)
91 {
92         size_t i;
93         for (i = 0; i < pglob->gl_pathc; i++)
94                 free(pglob->gl_pathv[i]);
95         free(pglob->gl_pathv[i]);
96 }