Release 960928
[wine/multimedia.git] / win32 / findfile.c
blob8f9bf5230d1c7e3ffb76b2de2b243b290b0a1ef5
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <malloc.h>
6 #include "xmalloc.h"
7 #include "windows.h"
8 #include "dos_fs.h"
9 #include "heap.h"
10 #include "string32.h"
12 #define PATH_LEN 260
14 struct FindFileContext32 {
15 DIR * dir;
16 char mask[PATH_LEN];
17 char path[PATH_LEN];
20 typedef struct FindFileContext32 FindFileContext32;
22 const char *DOSFS_Hash(const char *, int, int);
24 /* example D:\*.dbs */
26 static BOOL32 MatchWildCard(LPCSTR file, LPCSTR mask)
28 int len;
30 /* We should check volume information to see if long filenames possible.
33 len = strlen(file);
35 while (*file) {
36 if (*mask == '*') {
37 if (*(mask+1)) {
38 while (*file && (toupper(*file) != *(mask+1))) file++;
39 if (!*file)
40 return FALSE;
42 else
43 break;
45 else {
46 if (*mask != '?' && *mask != toupper(*file)) {
47 return FALSE;
49 file++;
51 mask++;
53 return (TRUE);
56 /*************************************************************************
57 * FindNextFile32A (KERNEL32.126)
59 BOOL32 FindNextFile32A(HANDLE32 handle, LPWIN32_FIND_DATA32A data)
61 FindFileContext32 *context;
62 struct dirent *dirent;
63 char dosname[14];
65 memset(data, 0, sizeof(WIN32_FIND_DATA32A));
66 context = (FindFileContext32 *) handle;
68 while ((dirent = readdir(context->dir)) != NULL) {
69 if (strcmp(dirent->d_name, "..") == 0 ||
70 strcmp(dirent->d_name, ".") == 0)
71 continue;
73 strcpy(dosname, DOSFS_Hash(dirent->d_name, FALSE, FALSE));
75 if (MatchWildCard(dirent->d_name, context->mask)) {
76 /* Full file name - is this a long file name?
77 * If it is, we should probably use the dirent
78 * instead of the dos hashed name.
80 strcpy(data->cFileName, dosname);
82 /* file name expressed in 8.3 format */
83 strcpy(data->cAlternateFileName, dosname);
84 return (TRUE);
88 return (FALSE);
91 /*************************************************************************
92 * FindNextFile32W (KERNEL32.127)
94 BOOL32 FindNextFile32W(HANDLE32 handle, LPWIN32_FIND_DATA32W data)
96 WIN32_FIND_DATA32A adata;
98 adata.dwFileAttributes = data->dwFileAttributes;
99 adata.ftCreationTime = data->ftCreationTime;
100 adata.ftLastAccessTime = data->ftLastAccessTime;
101 adata.ftLastWriteTime = data->ftLastWriteTime;
102 adata.nFileSizeHigh = data->nFileSizeHigh;
103 adata.nFileSizeLow = data->nFileSizeLow;
104 adata.dwReserved0 = data->dwReserved0;
105 adata.dwReserved1 = data->dwReserved1;
106 STRING32_UniToAnsi(adata.cFileName,data->cFileName);
107 STRING32_UniToAnsi(adata.cAlternateFileName,data->cAlternateFileName);
108 return FindNextFile32A(handle,&adata);
111 /*************************************************************************
112 * FindFirstFile32A (KERNEL32.123)
115 HANDLE32 FindFirstFile32A(LPCSTR lpfilename,
116 LPWIN32_FIND_DATA32A lpFindFileData)
118 const char *unixpath;
119 char *slash, *p;
120 FindFileContext32 *context;
122 context = HeapAlloc(SystemHeap, 0, sizeof(FindFileContext32));
123 if (!context)
124 return (INVALID_HANDLE_VALUE);
126 slash = strrchr(lpfilename, '\\');
127 if (slash) {
128 lstrcpyn32A(context->path, lpfilename, slash - lpfilename + 1);
129 context->path[slash - lpfilename + 1] = '\0';
130 unixpath = DOSFS_GetUnixFileName(context->path, FALSE);
131 if (!unixpath) {
132 /* FIXME: SetLastError(??) */
133 HeapFree(SystemHeap, 0, context);
134 return INVALID_HANDLE_VALUE;
136 lstrcpy32A(context->mask, slash+1);
138 else {
139 context->path[0] = '\0';
140 unixpath = ".";
141 lstrcpy32A(context->mask, lpfilename);
144 context->dir = opendir(unixpath);
145 if (!context->dir) {
146 /* FIXME: SetLastError(??) */
147 HeapFree(SystemHeap, 0, context);
148 return INVALID_HANDLE_VALUE;
151 /* uppercase mask in place */
152 for (p = context->mask ; *p; p++)
153 *p = toupper(*p);
155 if (!FindNextFile32A((HANDLE32) context, lpFindFileData))
156 return (INVALID_HANDLE_VALUE);
157 return ((HANDLE32) context);
160 /*************************************************************************
161 * FindFirstFile32W (KERNEL32.124)
163 HANDLE32 FindFirstFile32W(LPCWSTR filename,LPWIN32_FIND_DATA32W data)
165 WIN32_FIND_DATA32A adata;
166 LPSTR afn = STRING32_DupUniToAnsi(filename);
167 HANDLE32 res;
169 adata.dwFileAttributes = data->dwFileAttributes;
170 adata.ftCreationTime = data->ftCreationTime;
171 adata.ftLastAccessTime = data->ftLastAccessTime;
172 adata.ftLastWriteTime = data->ftLastWriteTime;
173 adata.nFileSizeHigh = data->nFileSizeHigh;
174 adata.nFileSizeLow = data->nFileSizeLow;
175 adata.dwReserved0 = data->dwReserved0;
176 adata.dwReserved1 = data->dwReserved1;
177 STRING32_UniToAnsi(adata.cFileName,data->cFileName);
178 STRING32_UniToAnsi(adata.cAlternateFileName,data->cAlternateFileName);
179 res=FindFirstFile32A(afn,&adata);
180 free(afn);
181 return res;
184 /*************************************************************************
185 * FindClose32 (KERNEL32.119)
187 BOOL32 FindClose32(HANDLE32 handle)
189 FindFileContext32 *context;
191 context = (FindFileContext32 *) handle;
192 if (context->dir)
193 closedir(context->dir);
194 HeapFree(SystemHeap, 0, context);
195 return (TRUE);