Mercurial > dwindows
annotate win/dirent.c @ 1952:8ce5c562348f
Mac: Attempting a better WKWebKit fix, use the WK_API_ENABLED constant to determine if the API is available.
Apparently it is only available on intel 64bit regardless of the system version. Use the old API on 32bit
or older systems.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Thu, 11 Jul 2019 21:40:33 +0000 |
parents | 3816d76835e9 |
children | 22e3e829be13 |
rev | line source |
---|---|
1617
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
1 #undef UNICODE |
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
2 #undef _UNICODE |
f8d1da63fb77
Add code to allow building DW.DLL as Unicode on Windows.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1601
diff
changeset
|
3 |
3 | 4 #include <stdio.h> |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 #include <ctype.h> | |
8 | |
9 #include <windows.h> | |
10 | |
1839
22225eb286e5
Fixes for building with MINGW64 (specifically http://tdm-gcc.tdragon.net/).
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1617
diff
changeset
|
11 #define _DW_INTERNAL |
1594
6baf177f335c
Rename compat.c/h dwcompat.c/h and configure option to --with-dwcompat.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1362
diff
changeset
|
12 #include "dwcompat.h" |
3 | 13 #include <errno.h> |
1362
412af8059331
Attempt to get it building with Mingw again... builds but crashes.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
572
diff
changeset
|
14 #include <direct.h> |
3 | 15 |
16 #define error(rc) errno = 255 | |
17 | |
18 struct _dirdescr { | |
19 HANDLE handle; /* DosFindFirst handle */ | |
20 char fstype; /* filesystem type */ | |
21 long count; /* valid entries in <ffbuf> */ | |
22 long number; /* absolute number of next entry */ | |
23 int index; /* relative number of next entry */ | |
24 char name[MAXPATHLEN+3]; /* directory name */ | |
25 unsigned attrmask; /* attribute mask for seekdir */ | |
26 struct dirent entry; /* buffer for directory entry */ | |
27 WIN32_FIND_DATA data; | |
28 }; | |
29 | |
30 /* | |
31 * Return first char of filesystem type, or 0 if unknown. | |
32 */ | |
1864
3816d76835e9
Some more MinGW build fixes.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1839
diff
changeset
|
33 static char getFSType(const char *path) |
3 | 34 { |
35 static char cache[1+26]; | |
36 char drive[3]; | |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
37 UCHAR unit; |
3 | 38 char r; |
39 | |
40 if (isalpha(path[0]) && path[1] == ':') { | |
41 unit = toupper(path[0]) - '@'; | |
42 path += 2; | |
43 } else { | |
44 return 0; | |
45 } | |
46 | |
47 if ((path[0] == '\\' || path[0] == '/') | |
48 && (path[1] == '\\' || path[1] == '/')) | |
49 return 0; | |
50 | |
51 if (cache [unit]) | |
52 return cache [unit]; | |
53 | |
54 drive[0] = '@' + unit; | |
55 drive[1] = ':'; | |
56 drive[2] = '\0'; | |
57 | |
58 r = GetDriveType(drive); | |
59 | |
60 return cache [unit] = r; | |
61 } | |
62 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
63 char * API abs_path(const char *name, char *buffer, int len) |
3 | 64 { |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
65 LPTSTR file; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
66 |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
67 if(isalpha(name[0]) && name[1] == ':' && name[2] == '\0') |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
68 { |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
69 int drive = _getdrive(); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
70 char newdrive = toupper(name[0]); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
71 |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
72 _chdrive((newdrive - 'A')+1); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
73 |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
74 if(_getcwd(buffer, len)) |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
75 { |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
76 _chdrive(drive); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
77 return buffer; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
78 } |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
79 _chdrive(drive); |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
80 return NULL; |
3 | 81 } |
260
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
82 if(GetFullPathName(name, len, buffer, &file)) |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
83 return buffer; |
e320dc29bfcd
Rewrote abs_path() so the code will work on NT 4.0.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
184
diff
changeset
|
84 return NULL; |
3 | 85 } |
86 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
87 DIR * API openxdir(const char *path, unsigned att_mask) |
3 | 88 { |
89 DIR *dir; | |
90 char name[MAXPATHLEN+3]; | |
91 | |
92 dir = malloc(sizeof(DIR)); | |
93 if (dir == NULL) { | |
94 errno = ENOMEM; | |
95 return NULL; | |
96 } | |
97 | |
98 strncpy(name, path, MAXPATHLEN); | |
99 name[MAXPATHLEN] = '\0'; | |
100 switch (name[strlen(name)-1]) { | |
101 default: | |
102 strcat(name, "\\"); | |
103 case '\\': | |
104 case '/': | |
105 case ':': | |
106 ; | |
107 } | |
108 strcat(name, "."); | |
109 if (!abs_path(name, dir->name, MAXPATHLEN+1)) | |
110 strcpy(dir->name, name); | |
111 if (dir->name[strlen(dir->name)-1] == '\\') | |
112 strcat(dir->name, "*"); | |
113 else | |
114 strcat(dir->name, "\\*"); | |
115 | |
116 dir->fstype = getFSType(dir->name); | |
117 dir->attrmask = att_mask | A_DIR; | |
118 | |
119 dir->count = 100; | |
184
4ec906d40ce2
FindFirstFile returns INVALID_HANDLE_VALUE on error, not NULL.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
3
diff
changeset
|
120 if((dir->handle = FindFirstFile(dir->name, &dir->data))==INVALID_HANDLE_VALUE) |
3 | 121 { |
122 free(dir); | |
123 error(rc); | |
124 return NULL; | |
125 } | |
126 | |
127 dir->number = 0; | |
128 dir->index = 0; | |
129 | |
130 return (DIR *)dir; | |
131 } | |
132 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
133 DIR * API opendir(const char *pathname) |
3 | 134 { |
135 return openxdir(pathname, 0); | |
136 } | |
137 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
138 struct dirent * API readdir(DIR *dir) |
3 | 139 { |
140 static int dummy_ino = 2; | |
141 | |
142 if (dir->number) | |
143 { | |
144 dir->count = 100; | |
145 if(!FindNextFile(dir->handle, &(dir->data))) | |
146 { | |
147 error(rc); | |
148 return NULL; | |
149 } | |
150 | |
151 dir->index = 0; | |
152 } | |
153 | |
154 strcpy(dir->entry.d_name, dir->data.cFileName); | |
155 dir->entry.d_ino = dummy_ino++; | |
1601
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
156 dir->entry.d_reclen = (int)strlen(dir->data.cFileName); |
71e0a3ad07f7
Enable level 3 warnings in DEBUG mode with Visual C to show deprecation warnings.
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
1594
diff
changeset
|
157 dir->entry.d_namlen = (int)strlen(dir->data.cFileName); |
3 | 158 dir->entry.d_size = dir->data.nFileSizeLow; |
159 dir->entry.d_attribute = dir->data.dwFileAttributes; | |
160 #if 0 | |
161 dir->entry.d_time = *(USHORT *)&dir->next->ftimeLastWrite; | |
162 dir->entry.d_date = *(USHORT *)&dir->next->fdateLastWrite; | |
163 #endif | |
164 | |
165 dir->number++; | |
166 dir->index++; | |
167 return &dir->entry; | |
168 } | |
169 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
170 long API telldir(DIR *dir) |
3 | 171 { |
172 return dir->number; | |
173 } | |
174 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
175 void API seekdir(DIR *dir, long off) |
3 | 176 { |
177 if (dir->number > off) { | |
178 char name[MAXPATHLEN+2]; | |
179 | |
180 FindClose(dir->handle); | |
181 | |
182 strcpy(name, dir->name); | |
183 strcat(name, "*"); | |
184 | |
185 if((dir->handle = FindFirstFile(name, &(dir->data)))==NULL) | |
186 { | |
187 error(rc); | |
188 return; | |
189 } | |
190 | |
191 dir->number = 0; | |
192 dir->index = 0; | |
193 } | |
194 | |
195 while (dir->number < off && readdir(dir)) | |
196 ; | |
197 } | |
198 | |
527
e0ea29c3d1eb
Fixed dw_window_pointer() so it works on Windows. Tried to fix the
bsmith@81767d24-ef19-dc11-ae90-00e081727c95
parents:
260
diff
changeset
|
199 void API closedir(DIR *dir) |
3 | 200 { |
201 FindClose(dir->handle); | |
202 free(dir); | |
203 } | |
204 | |
205 /*****************************************************************************/ | |
206 | |
207 #ifdef TEST | |
208 | |
209 main(int argc, char **argv) | |
210 { | |
211 int i; | |
212 DIR *dir; | |
213 struct dirent *ep; | |
214 | |
215 for (i = 1; i < argc; ++i) { | |
216 dir = opendir(argv[i]); | |
217 if (!dir) | |
218 continue; | |
219 while (ep = readdir(dir)) | |
220 if (strchr("\\/:", argv[i] [strlen(argv[i]) - 1])) | |
221 printf("%s%s\n", argv[i], ep->d_name); | |
222 else | |
223 printf("%s/%s\n", argv[i], ep->d_name); | |
224 closedir(dir); | |
225 } | |
226 | |
227 return 0; | |
228 } | |
229 | |
230 #endif |